Subtracting monthly averages from daily data - best way?

1,759 views
Skip to first unread message

Robin Wilson

unread,
Feb 13, 2017, 4:11:51 AM2/13/17
to xarray
Hi,

I have a set of monthly averages of some daily data (three dimensions: x, y and time):

monthly = data.groupby('time.month').mean(dim='time')

I would like to subtract the relevant monthly average from each day of the daily data, to get a sort of 'anomaly compared to monthly average' measure. I've had a look through the documentation, but I'm not sure how best to go about this.

I've tried:

result = data - monthly

which actually runs fine (I thought it would probably give an error), but produces a DataArray with an extra 'month' dimension:

<xarray.DataArray 'data' (time: 60, y: 1162, x: 1240, month: 2)>

I've also tried to 'upsample' the monthly data to produce daily data with each day's value in the month being constant, but running:

monthly_daily = monthly.resample('1D', dim='month')

gives an error as the month dimension is just an integer (1-12) not a datetime.

What is the best way to go about this?

Cheers,

Robin



Ryan Abernathey

unread,
Feb 13, 2017, 8:38:43 AM2/13/17
to xar...@googlegroups.com
You're almost there.

result = data.groupby('time.month') - monthly

This is covered in the toy weather data example:

But perhaps that example is not featured prominently enough in the documentation.

-Ryan


--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/2ca50b93-6bdc-4b04-8373-92db1f6b6b7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robin Wilson

unread,
Feb 14, 2017, 3:14:31 AM2/14/17
to xar...@googlegroups.com
Thanks - I didn't think to look at the examples.

Is there anything about this in the groupby section of the docs? I didn't think about actually subtracting from a groupby object.

Also, while we're on the topic: is there a way to do this using the resample method instead? Presumably that would allow me to look at anomalies from the mean of that specific month, as opposed to the mean of all Jans, all Febs etc.

Thanks,

Robin
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+un...@googlegroups.com.

To post to this group, send email to xar...@googlegroups.com.

Stephan Hoyer

unread,
Feb 14, 2017, 12:49:11 PM2/14/17
to xarray
On Tue, Feb 14, 2017 at 12:14 AM, Robin Wilson <ro...@rtwilson.com> wrote:
Thanks - I didn't think to look at the examples.

Is there anything about this in the groupby section of the docs? I didn't think about actually subtracting from a groupby object.

 
Also, while we're on the topic: is there a way to do this using the resample method instead? Presumably that would allow me to look at anomalies from the mean of that specific month, as opposed to the mean of all Jans, all Febs etc.

Unfortunately, this isn't current exposed, but would not be hard to do. Under the covers, resample just does a groupby operation with groups constructed on the fly.

This could be done either by adding upsampling with filling support to resample (would be pretty easy, even for a new contributor):

Or by refactoring resample to expose an API more like GroupBy (more like medium difficulty):

Reply all
Reply to author
Forward
0 new messages