Xarray long-format to wide-format - equivalent to Pandas' pivot

Viewed 278

I'm looking for a way to make a pivot table in Xarray, similar to Pandas' .pivot or set_index([...]).unstack().

Basically, I have some tabular data with 3 columns:

Data

And I would like to (in Xarray) convert this into the wide format through pivoting or the set_index([...]).unstack() operations as shown here via Pandas:

pivot table

Of course I can do this by first converting the data from long- to wide format via Pandas and then loading it into Xarray:

df = pd.DataFrame(data)
df = df.set_index(['price','date']).unstack()
x = xr.DataArray(df.values,dims=("price","date"),
                 coords={"price":df.index,
                         "date":df.columns.get_level_values(1)},
                 attrs={"long_name":"volume","units":'BTC'})

which gives the following results:

result

But I would really like a way to do all of this without using Pandas, since I would also like to leverage Xarray's native Dask support.

0 Answers
Related