简体   繁体   English

pandas - 重新采样datetime索引并延伸到月末

[英]pandas - Resampling datetime index and extending to end of the month

I am trying to resample a datetime index into hourly data. 我正在尝试将日期时间索引重新采样为每小时数据。 I also want the resampling until the end of the month. 我也希望重新采样到月底。

So given the following df : 所以给出以下df

data = np.arange(6).reshape(3,2)
rng = ['Jan-2016', 'Feb-2016', 'Mar-2016']
df = pd.DataFrame(data, index=rng)
df.index = pd.to_datetime(df.index)

            0  1
2016-01-01  0  1
2016-02-01  2  3
2016-03-01  4  5

I know I can resample this into an hourly index by: df = df.resample('H').ffill() However, when I call the df it gets cut at 2016-03-01 . 我知道我可以通过以下方式将其重新采样为小时索引: df = df.resample('H').ffill()但是,当我调用df它会在2016-03-01被切断。 I am essentially making the index run from 1/1/2016 to 3/31/2016 with an hourly granularity. 我基本上是以每小时粒度从1/1/2016年1 3/31/20161/1/20163/31/2016运行索引。

How can I extend this to the end of the month 2015-03-31 given that the last index is the beginning of the month. 鉴于最后一个指数是月初,我怎样才能将其扩展到2015-03-31月底。

UPDATE: 更新:

In [37]: (df.set_index(df.index[:-1].union([df.index[-1] + pd.offsets.MonthEnd(0)]))
   ....:    .resample('H')
   ....:    .ffill()
   ....:    .head()
   ....: )
Out[37]:
                     0  1
2016-01-01 00:00:00  0  1
2016-01-01 01:00:00  0  1
2016-01-01 02:00:00  0  1
2016-01-01 03:00:00  0  1
2016-01-01 04:00:00  0  1

In [38]: (df.set_index(df.index[:-1].union([df.index[-1] + pd.offsets.MonthEnd(0)]))
   ....:    .resample('H')
   ....:    .ffill()
   ....:    .tail()
   ....: )
Out[38]:
                     0  1
2016-03-30 20:00:00  2  3
2016-03-30 21:00:00  2  3
2016-03-30 22:00:00  2  3
2016-03-30 23:00:00  2  3
2016-03-31 00:00:00  4  5

Explanation: 说明:

In [40]: df.index[-1] + pd.offsets.MonthEnd(0)
Out[40]: Timestamp('2016-03-31 00:00:00')

In [41]: df.index[:-1].union([df.index[-1] + pd.offsets.MonthEnd(0)])
Out[41]: DatetimeIndex(['2016-01-01', '2016-02-01', '2016-03-31'], dtype='datetime64[ns]', freq=None)

Old incorrect answer: 旧的错误答案:

In [77]: df.resample('M').ffill().resample('H').ffill().tail()
Out[77]:
                     0  1
2016-03-30 20:00:00  2  3
2016-03-30 21:00:00  2  3
2016-03-30 22:00:00  2  3
2016-03-30 23:00:00  2  3
2016-03-31 00:00:00  4  5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM