Brian Bien Brian Bien - 4 days ago 6
Python Question

Upsampling Dataframe in Pandas

Given a dataframe indexed by month, I'd like to reindex by day (upsample). Values that were previously indexed by month should now be divided by the number of days in the month.

Answer

try:

tidx_m = pd.date_range('2011-01-31', periods=2, freq='M')
tidx_d = pd.date_range('2011-01-01', '2011-02-28', freq='D')

d = pd.Series(100, tidx_m)
d.reindex(tidx_d, fill_value=0).groupby(pd.TimeGrouper('M')).transform('mean')

yields

2011-01-01    3.225806
2011-01-02    3.225806
2011-01-03    3.225806
2011-01-04    3.225806
2011-01-05    3.225806
2011-01-06    3.225806
2011-01-07    3.225806
2011-01-08    3.225806
2011-01-09    3.225806
2011-01-10    3.225806
2011-01-11    3.225806
2011-01-12    3.225806
2011-01-13    3.225806
2011-01-14    3.225806
2011-01-15    3.225806
2011-01-16    3.225806
2011-01-17    3.225806
2011-01-18    3.225806
2011-01-19    3.225806
2011-01-20    3.225806
2011-01-21    3.225806
2011-01-22    3.225806
2011-01-23    3.225806
2011-01-24    3.225806
2011-01-25    3.225806
2011-01-26    3.225806
2011-01-27    3.225806
2011-01-28    3.225806
2011-01-29    3.225806
2011-01-30    3.225806
2011-01-31    3.225806
2011-02-01    3.571429
2011-02-02    3.571429
2011-02-03    3.571429
2011-02-04    3.571429
2011-02-05    3.571429
2011-02-06    3.571429
2011-02-07    3.571429
2011-02-08    3.571429
2011-02-09    3.571429
2011-02-10    3.571429
2011-02-11    3.571429
2011-02-12    3.571429
2011-02-13    3.571429
2011-02-14    3.571429
2011-02-15    3.571429
2011-02-16    3.571429
2011-02-17    3.571429
2011-02-18    3.571429
2011-02-19    3.571429
2011-02-20    3.571429
2011-02-21    3.571429
2011-02-22    3.571429
2011-02-23    3.571429
2011-02-24    3.571429
2011-02-25    3.571429
2011-02-26    3.571429
2011-02-27    3.571429
2011-02-28    3.571429
Freq: D, dtype: float64
Comments