Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop coordinate from an xarray DataArray

I have an xarray DataArray that looks like this below with shape (1,5,73,144,17) and I'm trying to drop or delete the "level" coordinates. So, ultimately, i need the variable to have shape = (1,5,73,144).

stdna
Out[717]: 
<xarray.DataArray 'stack-6e9b86fc65e3f0fda2008a339e235bc7' (variable: 1, week: 5, lat: 73, lon: 144, 
level: 17)>
dask.array<stack, shape=(1, 5, 73, 144, 17), dtype=float32, chunksize=(1, 1, 73, 144, 17), 
chunktype=numpy.ndarray>
Coordinates:
* lon       (lon) float32 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
* lat       (lat) float32 90.0 87.5 85.0 82.5 80.0 ... -82.5 -85.0 -87.5 -90.0
* level     (level) float32 1000.0 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0
* week      (week) int64 5 6 7 8 9
* variable  (variable) <U3 'hgt'

I've taken a look to the xarray documentation and it's not helping. I've tried different combinations around this idea but i usually get the statement below and the coordinate has not been removed:

s = stdna.drop('level', dim=None)

Dimensions without coordinates: level

Thank you for your help!

like image 203
user2100039 Avatar asked Apr 03 '20 00:04

user2100039


People also ask

How do I drop a dimension in Xarray?

In future versions of xarray (v0. 9 and later), you will be able to drop coordinates when indexing by writing drop=True , e.g., ds['bar']. sel(x=1, drop=True) .

What is Python Xarray?

xarray (formerly xray) is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!


1 Answers

You can use the drop_vars method:

In [10]: da
Out[10]:
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[0.15928504, 0.47081089, 0.50490985],
       [0.6151981 , 0.41735643, 0.2576089 ]])
Coordinates:
    x        (dim_0, dim_1) float64 0.1593 0.4708 0.5049 0.6152 0.4174 0.2576
Dimensions without coordinates: dim_0, dim_1

In [11]: da.drop_vars('x')
Out[11]:
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[0.15928504, 0.47081089, 0.50490985],
       [0.6151981 , 0.41735643, 0.2576089 ]])
Dimensions without coordinates: dim_0, dim_1

Alternatively reset_coords('level', drop=True will also work

like image 175
Maximilian Avatar answered Sep 24 '22 09:09

Maximilian