我执行以下操作:
import dask.dataframe as dd
from dask.distributed import Client
client = Client()
raw_data_df = dd.read_csv('dataset/nyctaxi/nyctaxi/*.csv', assume_missing=True, parse_dates=['tpep_pickup_datetime', 'tpep_dropoff_datetime'])
该数据集摘自Mathew Rocklin所做的演示,并用作dask数据框演示。然后我尝试用pyarrow将其写入镶木地板
raw_data_df.to_parquet(path='dataset/parquet/2015.parquet/') # only pyarrow is installed
尝试回读:
raw_data_df = dd.read_parquet(path='dataset/parquet/2015.parquet/')
我收到以下错误:
ValueError: Schema in dataset/parquet/2015.parquet//part.192.parquet was different.
VendorID: double
tpep_pickup_datetime: timestamp[us]
tpep_dropoff_datetime: timestamp[us]
passenger_count: double
trip_distance: double
pickup_longitude: double
pickup_latitude: double
RateCodeID: int64
store_and_fwd_flag: binary
dropoff_longitude: double
dropoff_latitude: double
payment_type: double
fare_amount: double
extra: double
mta_tax: double
tip_amount: double
tolls_amount: double
improvement_surcharge: double
total_amount: double
metadata
--------
{'pandas': '{"pandas_version": "0.22.0", "index_columns": [], "columns": [{"metadata": null, "field_name": "VendorID", "name": "VendorID", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tpep_pickup_datetime", "name": "tpep_pickup_datetime", "numpy_type": "datetime64[ns]", "pandas_type": "datetime"}, {"metadata": null, "field_name": "tpep_dropoff_datetime", "name": "tpep_dropoff_datetime", "numpy_type": "datetime64[ns]", "pandas_type": "datetime"}, {"metadata": null, "field_name": "passenger_count", "name": "passenger_count", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "trip_distance", "name": "trip_distance", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "pickup_longitude", "name": "pickup_longitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "pickup_latitude", "name": "pickup_latitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "RateCodeID", "name": "RateCodeID", "numpy_type": "int64", "pandas_type": "int64"}, {"metadata": null, "field_name": "store_and_fwd_flag", "name": "store_and_fwd_flag", "numpy_type": "object", "pandas_type": "bytes"}, {"metadata": null, "field_name": "dropoff_longitude", "name": "dropoff_longitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "dropoff_latitude", "name": "dropoff_latitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "payment_type", "name": "payment_type", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "fare_amount", "name": "fare_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "extra", "name": "extra", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "mta_tax", "name": "mta_tax", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tip_amount", "name": "tip_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tolls_amount", "name": "tolls_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "improvement_surcharge", "name": "improvement_surcharge", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "total_amount", "name": "total_amount", "numpy_type": "float64", "pandas_type": "float64"}], "column_indexes": []}'}
vs
VendorID: double
tpep_pickup_datetime: timestamp[us]
tpep_dropoff_datetime: timestamp[us]
passenger_count: double
trip_distance: double
pickup_longitude: double
pickup_latitude: double
RateCodeID: double
store_and_fwd_flag: binary
dropoff_longitude: double
dropoff_latitude: double
payment_type: double
fare_amount: double
extra: double
mta_tax: double
tip_amount: double
tolls_amount: double
improvement_surcharge: double
total_amount: double
metadata
--------
{'pandas': '{"pandas_version": "0.22.0", "index_columns": [], "columns": [{"metadata": null, "field_name": "VendorID", "name": "VendorID", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tpep_pickup_datetime", "name": "tpep_pickup_datetime", "numpy_type": "datetime64[ns]", "pandas_type": "datetime"}, {"metadata": null, "field_name": "tpep_dropoff_datetime", "name": "tpep_dropoff_datetime", "numpy_type": "datetime64[ns]", "pandas_type": "datetime"}, {"metadata": null, "field_name": "passenger_count", "name": "passenger_count", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "trip_distance", "name": "trip_distance", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "pickup_longitude", "name": "pickup_longitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "pickup_latitude", "name": "pickup_latitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "RateCodeID", "name": "RateCodeID", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "store_and_fwd_flag", "name": "store_and_fwd_flag", "numpy_type": "object", "pandas_type": "bytes"}, {"metadata": null, "field_name": "dropoff_longitude", "name": "dropoff_longitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "dropoff_latitude", "name": "dropoff_latitude", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "payment_type", "name": "payment_type", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "fare_amount", "name": "fare_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "extra", "name": "extra", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "mta_tax", "name": "mta_tax", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tip_amount", "name": "tip_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "tolls_amount", "name": "tolls_amount", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "improvement_surcharge", "name": "improvement_surcharge", "numpy_type": "float64", "pandas_type": "float64"}, {"metadata": null, "field_name": "total_amount", "name": "total_amount", "numpy_type": "float64", "pandas_type": "float64"}], "column_indexes": []}'}
但是看着它们,它们看起来一样。对确定原因有帮助吗?
答案 0 :(得分:3)
以下两个numpy规范不同意
{'metadata': None, 'field_name': 'RateCodeID', 'name': 'RateCodeID', 'numpy_type': 'int64', 'pandas_type': 'int64'}
RateCodeID: int64
{'metadata': None, 'field_name': 'RateCodeID', 'name': 'RateCodeID', 'numpy_type': 'float64', 'pandas_type': 'float64'}
RateCodeID: double
(仔细看!)
我建议您在加载时为此列提供dtype,或者在写入之前使用astype
将其强制为浮点数。
答案 1 :(得分:3)
这个问题涉及到Pandas和Dask中最棘手的问题之一,即数据类型的可为空性或缺乏数据类型。因此,丢失数据会引起问题,尤其是对于整数等数据类型而言,不会缺少数据名称。
浮点数和日期时间还算不错,因为它们指定了空值或缺少值的占位符(NaN表示numpy的浮点值,NaT表示熊猫的日期时间),因此可以为空。但是,即使是那些dtype在某些情况下也存在问题。
当您读取多个CSV文件(如您的情况),或者从数据库中提取数据,或者将一个小的数据框合并为一个较大的数据框时,可能会出现此问题。您可能会遇到缺少给定字段的某些或所有值的分区。对于这些分区,Dask和Pandas将为该字段分配一个dtype,以适应丢失的数据指示符。对于整数,新的dtype将为float。写入实木复合地板时,这种情况会进一步转变为两倍。
Dask会很乐意为该字段列出一个误导性的dtype。但是,当您写入实木复合地板时,数据丢失的分区将被写入其他内容。与您的情况一样,“ int64”在至少一个实木复合地板文件中被写入为“ double”。然后,当您尝试读取整个Dask数据帧时,由于不匹配,出现了上面显示的ValueError。
在可以解决这些问题之前,您需要确保所有Dask字段的每一行都具有适当的数据。例如,如果您有一个int64字段,则NaN值或缺少值的某些其他非整数表示将不起作用。
您的int64字段可能需要分几步进行固定:
导入熊猫:
import pandas as pd
将字段数据清除为float64,并将缺失值强制转换为NaN:
df['myint64'] = df['myint64'].map_partitions(
pd.to_numeric,
meta='f8',
errors='coerce'
)
选择一个警戒值(例如-1.0)来代替NaN,以便int64可以正常工作:
df['myint64'] = df['myint64'].where(
~df['myint64'].isna(),
-1.0
)
将您的字段设置为int64并将其全部保留:
df['myint64'] = df['myint64'].astype('i8')
df = client.persist(df)
然后尝试保存并重新读取往返行程。
注意:步骤1-2对于修复float64字段很有用。
最后,要修复日期时间字段,请尝试以下操作:
df['mydateime'] = df['mydateime'].map_partitions(
pd.to_datetime,
meta='M8',
infer_datetime_format=True,
errors='coerce'
).persist()