【问题标题】:Resampling Pandas Timeseries so that the Date indicated the 1st of each Month重新采样 Pandas 时间序列,以便日期表示每个月的 1 日
【发布时间】:2019-01-30 21:25:28
【问题描述】:

我有一个 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()

原封不动地返回 toy_data。

【问题讨论】:

    标签: python pandas time-series resampling


    【解决方案1】:

    将M更改为MS,检查offset aliases:

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

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2022-01-22
      • 2018-05-22
      • 1970-01-01
      • 2018-02-05
      • 2020-09-29
      • 2021-12-31
      • 2018-12-24
      相关资源
      最近更新 更多