我想在熊猫的日期指数中增加数年,但一直未能做到:
ts是一个包含一系列数据和相关日期的数据。我想将这些日期再延长几年,以便增加其他系列的绘图/分析。
ts.head()
date
2014-12-31 NaN
2015-12-31 0.617876
2016-12-31 0.472640
2017-12-31 0.426240
2018-12-31 0.297176
Name: BL-US, dtype: float64我试过了
ts.index.union([ts.index[-1] + datetime.timedelta(years=x) for x in range(7)])并收到以下错误:
TypeError:'years‘是新()的无效关键字参数
有什么建议吗?谢谢!
发布于 2022-09-09 15:25:16
你应该使用date_range
ts.index.union(pd.date_range(ts.index[-1], periods=5, freq='Y'))产出:
DatetimeIndex(['2014-12-31', '2015-12-31', '2016-12-31', '2017-12-31',
'2018-12-31', '2019-12-31', '2020-12-31', '2021-12-31',
'2022-12-31'],
dtype='datetime64[ns]', freq='A-DEC')发布于 2022-09-09 15:19:29
这可能是可行的:
ts.index.union([ts.index[-1] + pd.offsets.DateOffset(years=x) for x in range(5)])https://stackoverflow.com/questions/73664228
复制相似问题