Python, Pandas: Use The Groupby.groups Description To Apply It To Another Grouping
Let's consider a DataFrame that contains 1 row of 2 values per each day of the month of Jan 2010: date_range = pd.date_range(dt(2010,1,1), dt(2010,1,31), freq='1D') df = pd.DataFra
Solution 1:
One simple solution to your problem would be to make sure the observations dataframe contains all the dates that the df dataframe does. You can do this with the reindex
method. You will then have the exact same groups. You can also use resample('W')
instead of groupby(pd.Timegrouper('W'))
obs2 = observations.reindex(df.index)
obs2.resample('W').groups
{Timestamp('2010-01-03 00:00:00', freq='W-SUN'): 3,
Timestamp('2010-01-10 00:00:00', freq='W-SUN'): 10,
Timestamp('2010-01-17 00:00:00', freq='W-SUN'): 17,
Timestamp('2010-01-24 00:00:00', freq='W-SUN'): 24,
Timestamp('2010-01-31 00:00:00', freq='W-SUN'): 31}
And if we do a simple aggregation like sum we can see the results of both frames
df.resample('W').sum()012010-01-03 1.9905582.5551912010-01-10 2.7077773.7717562010-01-17 2.7998973.3533632010-01-24 3.1654792.7788702010-01-31 4.9465773.394211
And now with obs2
which has 2 missing groups
obs2.resample('W').sum()012010-01-03 NaNNaN2010-01-10 NaNNaN2010-01-17 0.1723410.1371362010-01-24 1.7524722.3753062010-01-31 0.7115250.124271
Post a Comment for "Python, Pandas: Use The Groupby.groups Description To Apply It To Another Grouping"