重新采样 Pandas 时间序列,以便日期表示每个月的第一天

Resampling Pandas Timeseries so that the Date indicated the 1st of each Month

我有一个 Pandas 时间序列,其中日期表示每个月的最后一天。我想更改它,以便它包含每个月的第一天。例如,将所有日期从“2018-08-31”变为“2018-08-01”,依此类推。

为此,我尝试使用值为 'start' 的 'convention' 参数重新采样,但该方法返回了完整的时间序列。

一个可重现的例子:

toy_data.to_json()
'{"GDP_Quarterly_Growth_Rate":{"-710294400000":-0.266691,"-707616000000":-0.266691,"-704937600000":-0.266691,"-702345600000":-0.206496,"-699667200000":-0.206496,"-697075200000":-0.206496,"-694396800000":1.564208,"-691718400000":1.564208,"-689212800000":1.564208,"-686534400000":1.504256}}'

toy_data.resample('M', convention = 'start').mean()

Returns toy_data完好。

M更改为MS,勾选offset aliases

toy_data.resample('MS', convention = 'start').mean()