gpt4 book ai didi

python - pd.date_range 在月底返回起始值,然后继续每月日期?

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:14 25 4
gpt4 key购买 nike

val.index[-1] = '2019-08-01'

pd.date_range(start = val.index[-1], periods = 6,freq='M').tolist()

[Timestamp('2019-08-31 00:00:00', freq='M'),
Timestamp('2019-09-30 00:00:00', freq='M'),
Timestamp('2019-10-31 00:00:00', freq='M'),
Timestamp('2019-11-30 00:00:00', freq='M'),
Timestamp('2019-12-31 00:00:00', freq='M'),
Timestamp('2020-01-31 00:00:00', freq='M')]

我正在尝试从预测索引创建一个列表,但是该列表填充了该月的最后一天,然后继续填充每月日期。我的目标是获得这样的列表:

[Timestamp('2019-09-01 00:00:00', freq='M'),
Timestamp('2019-10-01 00:00:00', freq='M'),
Timestamp('2019-11-01 00:00:00', freq='M'),
Timestamp('2019-12-01 00:00:00', freq='M'),
Timestamp('2020-01-01 00:00:00', freq='M'),
Timestamp('2020-02-01 00:00:00', freq='M')]

最佳答案

如果您知道数据帧的长度,您可以这样做:

this_date = pd.date_range(start = '2019-08-31',periods = 6, freq = "M")
A = [1,2,3,4,5,6]
df = pd.DataFrame({'A':A}, index = this_date)

df.reset_index(drop = True, inplace = True)
df.set_index(pd.date_range(start = '2019-08-31',periods = 6, freq = "MS"))

这适用于您的问题吗?

关于python - pd.date_range 在月底返回起始值,然后继续每月日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58651035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com