Pandas - Re-select the date and time index and extend to the end of the month

I am trying to recalculate the datetime index into hourly data. I also want oversampling before the end of the month.

So the following is given 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 that I can convert this to an hourly index: df = df.resample('H').ffill()However, when I call df, it is truncated to 2016-03-01. I essentially do an index from 1/1/2016to 3/31/2016with hourly granularity.

How can I extend this until the end of the month 2015-03-31, given that the last index is the beginning of the month.

+4
source share
1 answer

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 wrong 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
+4
source

Source: https://habr.com/ru/post/1648452/


All Articles