简体   繁体   English

从 xarray DataArray 中删除坐标

[英]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.我有一个 xarray DataArray,如下所示,形状为 (1,5,73,144,17),我正在尝试删除或删除“级别”坐标。 So, ultimately, i need the variable to have shape = (1,5,73,144).所以,最终,我需要变量的形状=(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.我查看了 xarray 文档,但没有帮助。 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!谢谢您的帮助!

Initially, I looked at the behaviour of drop and found that it does not delete the dimension.最初,我查看了 drop 的行为,发现它并没有删除维度。 It can be used to delete data variables as such.它可用于删除数据变量。

Then I tried this:然后我尝试了这个:

del stdna['level']

I would say the best way would be to try:我想说最好的方法是尝试:

stdna.drop_dims('level')

There is one more thing I tried:我还尝试了另一件事:

stdna = stdna.drop([i for i in stdna.coords if i not in stdna.dims])

to see if I can generalize this issue.看看我是否可以概括这个问题。 But don't think this would work well.但不要认为这会很好。 From docs: http://xarray.pydata.org/en/stable/generated/xarray.Dataset.drop_dims.html来自文档: http://xarray.pydata.org/en/stable/generated/xarray.Dataset.drop_dims.html

You can use the drop_vars method:您可以使用drop_vars方法:

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或者reset_coords('level', drop=True也可以

Late to the party, but in case anyone else comes across this...派对迟到了,但万一其他人遇到这个......

The reason you can't drop a dimension like this is because your data is actually indexed by level .您不能像这样删除维度的原因是因为您的数据实际上是按level索引的。 In order to "remove" the the level dimension from your data, you need to decide how you would like to reduce the information along this dimension.为了从数据中“删除”级别维度,您需要决定如何减少沿此维度的信息。

You could do this in all kinds of ways.你可以通过各种方式做到这一点。 If you want to select a single level from the array, then da.sel is what you're looking for, eg:如果你想select从阵列中单层,那么da.sel就是你要找的,例如:

stdna.sel(level=1000)

On the other hand, maybe you're looking to aggregate the data across the level dimension?另一方面,也许您正在寻找跨级别维度聚合数据? For example, you could take the average value across all levels:例如,您可以取所有级别的平均值:

stdna.mean(dim='level')

But without knowing how you want to go from a DataArray that is indexed by level to one that is not, there's no way for xarray to simply "drop" it from the data - the array will still be shaped (1,5,73,144,17) .但是,在不知道您想如何从按级别索引的 DataArray 到未按级别索引的 DataArray 中的 go 的情况下,xarray 无法简单地从数据中“删除”它 - 数组仍然会被整形(1,5,73,144,17)

See the docs on indexing and selecting data orcomputation: aggregation for more info on these topics.有关这些主题的更多信息,请参阅有关索引和选择数据计算的文档:聚合

If you have levels nested within the events of your DataArray like so:如果您有嵌套在 DataArray 的事件中的级别,如下所示:

Example da示例 da

you need to use.droplevel() within the MultiIndex.您需要在 MultiIndex 中使用.droplevel()。 For this example, to drop the Coordinate 'answer_correct':对于此示例,要删除坐标“answer_correct”:

da.indexes['event'].droplevel('answer_correct')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM