【发布时间】:2020-10-19 18:07:51
【问题描述】:
当将时间序列重新采样为月度序列时,pandas 将我的时间序列的初始日期更改为月初。来自:
2020-01-12 0.730439
2020-01-13 0.559328
...
2021-06-29 0.188461
2021-06-30 0.750668
收件人:
2020-01-01 8.613978
2020-02-01 14.614601
... ...
2021-05-01 11.936765
2021-06-01 13.758198
而不是期望的结果,在第一个月,日期是我的时间序列的第一个日期:
2020-01-12 8.613978
2020-02-01 14.614601
... ...
2021-05-01 11.936765
2021-06-01 13.758198
有没有办法在不丢失初始日期的情况下执行每月重新采样?
目前我会在之后更正它。我在问是否有办法即时进行。我尝试了所有resample 的参数,但没有达到预期的结果。我看了一下pd.Grouper,但也没有成功。
谢谢你, 公知
PS:复制问题的小脚本。
import pandas as pd
from numpy.random import random
index = pd.date_range('20200112', '20210630')
df = pd.Series(random(len(index)), index=index)
df.resample('MS').sum()
【问题讨论】:
标签: python pandas dataframe resampling