gpt4 book ai didi

python - 向 Pandas Period 对象添加 n 个月?

转载 作者:行者123 更新时间:2023-11-28 22:13:03 24 4
gpt4 key购买 nike

这看起来应该很容易,但我在谷歌搜索时找不到答案。假设我有一些类型为 pandas.Period 的变量 k,其值为:

Period('2018-11', 'M')

如何向该变量添加 n 个月。例如,如果 n 是 3,我希望 k 是

Period('2019-02', 'M')

我尝试了以下方法:

k.month = k.month + 12

但这没有说明:

AttributeError: attribute 'month' of 'pandas._libs.tslibs.period._Period' objects is not writable

最佳答案

添加 pd.offsets .MonthEnd 对象:

pd.Period('2018-11', 'M') + pd.offsets.MonthEnd(3)

关于python - 向 Pandas Period 对象添加 n 个月?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54320684/

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