1

我明白这个追溯

ValueError: Length of values does not match length of index

产生于在 中或相同操作期间一个dataframe 比另一个长或短 的事实。dataframeddf.assign(new_col=ts_colddf['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
4

1 回答 1

1

让它与lambda/map功能一起使用:

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

资源

于 2020-06-06T23:29:01.980 回答