【发布时间】:2020-03-09 14:46:13
【问题描述】:
我有一个数据框df,其值为:
df.iloc[1:4, 7:9]
Year Month
38 2020 4
65 2021 4
92 2022 4
我正在尝试创建一个新的MonthIdx 列:
df['MonthIdx'] = pd.to_timedelta(df['Year'], unit='Y') + pd.to_timedelta(df['Month'], unit='M') + pd.to_timedelta(1, unit='D')
但我得到了错误:
ValueError: Units 'M' and 'Y' are no longer supported, as they do not represent unambiguous timedelta values durations.
以下是所需的输出:
df['MonthIdx']
MonthIdx
38 2020/04/01
65 2021/04/01
92 2022/04/01
【问题讨论】:
标签: python-3.8 pandas-1.0