You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [19]: df = DataFrame(range(3),columns=['foo'],index=pd.date_range('20130101',periods=3,name='bar'))
In [20]: df
Out[20]:
foo
bar
2013-01-01 0
2013-01-02 1
2013-01-03 2
In [21]: df.index.name
Out[21]: 'bar'
In [22]: df.asfreq('10D')
Out[22]:
foo
2013-01-01 0
In [24]: df.asfreq('10D').index.name
This bug was originally reported on StackOverflow here.
Original question
The question was asking how to efficiently re-index one level of a multi-index DataFrame with “forward-fill” using the following input DataFrame as an example:
You have a couple of options, the easiest IMO is to simply unstack the first level and then ffill. I think this make it much clearer about what's going on than a groupby/resample solution (I suspect it will also be faster, depending on the data):
In [11]: df1['value'].unstack(0)
Out[11]:
item_uid 0F01ddgkRa 0F02BZeTr6 0F02BcIzNo 0F02F4gAMs 0F02Vwd6Ou 0F04OlAs0R 0F05GInfPa 0F05PQARFJ 0F06LFhBCK 0F06ryso80 0F07gg7Oth 0S0099v8iI
created_at
2015-03-16 NaN NaN NaN NaN NaN NaN 664.68 NaN NaN 13.73 NaN NaN
2015-03-17 NaN NaN 1230 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-18 NaN NaN NaN NaN NaN 321.44 NaN 1074.31 211.49 NaN NaN NaN
2015-03-19 NaN NaN NaN NaN 5709.33 NaN NaN NaN NaN NaN 2325.7 NaN
2015-03-20 NaN 51505.22 NaN NaN NaN NaN NaN NaN NaN 12.00 NaN NaN
2015-03-23 NaN 51837.97 1130 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-24 NaN 51578.63 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-25 1414.71 NaN NaN 1855.96 NaN NaN NaN NaN NaN NaN NaN 10652.79
2015-03-26 NaN NaN NaN NaN NaN NaN NaN 1098.31 NaN NaN NaN NaN
2015-03-27 NaN 50893.42 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
If you're missing some dates you have to reindex (assuming the start and end are present, otherwise you can do this manually e.g. with pd.date_range):
In [12]: df1['value'].unstack(0).asfreq('D')
Out[12]:
item_uid 0F01ddgkRa 0F02BZeTr6 0F02BcIzNo 0F02F4gAMs 0F02Vwd6Ou 0F04OlAs0R 0F05GInfPa 0F05PQARFJ 0F06LFhBCK 0F06ryso80 0F07gg7Oth 0S0099v8iI
2015-03-16 NaN NaN NaN NaN NaN NaN 664.68 NaN NaN 13.73 NaN NaN
2015-03-17 NaN NaN 1230 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-18 NaN NaN NaN NaN NaN 321.44 NaN 1074.31 211.49 NaN NaN NaN
2015-03-19 NaN NaN NaN NaN 5709.33 NaN NaN NaN NaN NaN 2325.7 NaN
2015-03-20 NaN 51505.22 NaN NaN NaN NaN NaN NaN NaN 12.00 NaN NaN
2015-03-21 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-22 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-23 NaN 51837.97 1130 NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-24 NaN 51578.63 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-03-25 1414.71 NaN NaN 1855.96 NaN NaN NaN NaN NaN NaN NaN 10652.79
2015-03-26 NaN NaN NaN NaN NaN NaN NaN 1098.31 NaN NaN NaN NaN
2015-03-27 NaN 50893.42 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Note: asfreq drops the name of the index (which is most likely a bug!)
The text was updated successfully, but these errors were encountered:
Here's a simple repro
This bug was originally reported on StackOverflow here.
Original question
The question was asking how to efficiently re-index one level of a multi-index DataFrame with “forward-fill” using the following input DataFrame as an example:
Andy's answer:
Note:
asfreq
drops the name of the index (which is most likely a bug!)The text was updated successfully, but these errors were encountered: