reshaping an xarray

126 views
Skip to first unread message

Andrew Dessler

unread,
Aug 21, 2020, 12:12:02 PM8/21/20
to xarray

I have a lat by lon by time xarray data set and I want to reshape it.  I'd like to break the time series into a set of equal length segments.  So if the original array is 90 by 180 by 8000, I would like to reshape it into, say, 80 segments of 100 time steps, so the output data set would be 90 by 180 by 80 by 100. 

I'm not sure how to do this in xarray.  I tried using stack/unstack with code that looks like this (x2 is the data set to be reshaped):

flatten_list = lambda l: [item for sublist in l for item in sublist]
new_time=flatten_list([[ii for ii in range(216)] for jj in range(38)])
new_segment=flatten_list([np.zeros(216)+ii for ii in range(38)])

index = pd.MultiIndex.from_arrays([new_segment, new_time], names=['segment', 'seg_time'])

x2['values'] = index

reshaped = x2.unstack('values')

I could brute force this, of course, and loop over the variables, convert each one to a numpy array, reshape it, then recombine them into a xarray data set, but it seems like I shouldn't have to do that.

Andy suggestions are appreciated!

Deepak Cherian

unread,
Aug 21, 2020, 12:29:33 PM8/21/20
to xar...@googlegroups.com

This seems to be working:

ds2 = ds.copy(deep=True)
ds2["time"] = pd.MultiIndex.from_arrays(np.divmod(np.arange(ds.sizes["time"]), 10))
ds2.unstack("time")

I think we should add `.coarsen.construct()` because this does exactly what you want:

reshaped, _ = ds.air.variable._coarsen_reshape(boundary="trim", side="left", windows={"time": 10})

and it would nicely mirror the `rolling.construct()` API. Can you open an issue for this please?

Deepak

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/88fe43c8-6fac-4995-8d9b-7fd8a0ad9f8an%40googlegroups.com.

Andrew Dessler

unread,
Aug 21, 2020, 1:33:29 PM8/21/20
to xarray
That works!  Thanks!  I see the problem my code had: I was creating a new variable 'value' instead of overwriting the 'time' variable.
Reply all
Reply to author
Forward
0 new messages