python - 向 dask 数据帧添加新列会引发 ValueError : Length of values does not match length of index

标签 python dask dask-distributed dask-delayed dask-dataframe

我知道这个回溯

ValueError:值的长度与索引的长度不匹配

源于这样一个事实:一个dataframe is longer or shorter than the other ddf.assign(new_col=ts_col) 期间的 dataframeddf['ts_col'] = ts_col 中的相同操作。

问题是,我看不出长度有何不同 - 用代码解释:

from dask import dataframe as dd

# Read data
ddf = dd.read_csv(csv_path)
ddf.persist()


# Convert to unixtimestamp to pandas timestamp
ts_col = pd.to_datetime(ddf.ts_unixtime_sec_prec, unit='s', errors='coerce')
ts_col.fillna()

# Check data
> ts_col[0:2]
< DatetimeIndex(['2019-05-23 09:09:56', '2019-05-23 09:09:56'], dtype='datetime64[ns]', freq=None)

# Checking length
> len(ddf.index) 
< 11227296

> len(ts_col)
< 11227296

# Try to assign it to dataframe.
> ddf['ts_col'] = ts_col
< ValueError: Length of values does not match length of index <<< Error

最佳答案

让它与 lambda/map 函数一起使用:

df['ts'] = df['ts'].map(lambda x: pd.to_datetime(x, errors='coerce'))

Source

关于python - 向 dask 数据帧添加新列会引发 ValueError : Length of values does not match length of index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62238990/

相关文章:

python - DLL 加载失败 : The specified module could not be found PYTHON

python - 在 python 中读取 15 M 行 csv 文件的有效方法

python - 在Python中读取巨大的csv文件、处理然后写入处理后的csv的最快方法

dask--预加载 'Unknown argument specifier'

python - 我正在尝试使用插入方法在列表中已经存在的每个元素之后将相同的元素添加到我的列表中

c++ - Cython 中的抽象类(带有纯虚方法)

python - 将 dask 数据帧转换为矩阵或二维数组

hadoop - Dask:从 HDFS 读取时,pyarrow/hdfs.py 返回 OSError: Getting symbol hdfsNewBuilder failed

python - 将数据分散到 dask 集群工作人员 : unknown address scheme 'gateway'

python - Numpy.unique - 获得深度一致的输出