【发布时间】:2021-06-24 19:15:30
【问题描述】:
在此示例中,我尝试使用从 date 字段中提取的 balance 和 year 两列对 Dask 数据帧进行排序。我正在尝试将余额作为字符串与年份作为字符串连接到一个新字段中,但出现错误:
pdf = pd.DataFrame({
'id': [1, 1, 1, 2, 2],
'balance': [350, 340, 130, 280, 260],
'date' : [datetime(2021,3,1), datetime(2021,2,7), datetime(2021,7,1),
datetime(2021,2,6), datetime(2021,3,18)]
})
ddf = dd.from_pandas(pdf, npartitions=100)
ddf['newIndex'] = str(ddf['balance']) + (ddf['date']).year # <-- this throws the error
ddf = ddf.set_index(['newIndex'])
我得到的错误是:
AttributeError: 'Series' 对象没有属性 'year'
日期字段是一个系列,我明白了,但是如何从日期中提取年份并连接余额以按此新字段排序?
【问题讨论】:
标签: python python-3.x pandas dask