从 xarray 数据集中删除一个坐标值

Delete one coordinate value from xarray dataset

我有包含以下信息的 xarray 数据集:

print(ds)
>><xarray.Dataset>
Dimensions:        (lat: 90, lon: 720, time: 1200)
Coordinates:
* lon            array([-90.      , -89.057592, -88.115183, -87.172775, -86.230366, -85.287958,
       -84.34555 , -83.403141, -82.460733, -81.518325, -80.575916, -79.633508,
       -78.691099, -77.748691, -76.806283, -75.863874, -74.921466, -73.979058,
       -73.036649, -72.094241, -71.151832, -70.209424, -69.267016, -68.324607,
       -67.382199, -66.439791, -65.497382, -64.554974, -63.612565, -62.670157,
       -61.727749, -60.78534 , -59.842932, -58.900524, -57.958115, -57.015707,
       -56.073298, -55.13089 , -54.188482, -53.246073, -52.303665, -51.361257,
       -50.418848, -49.47644 , -48.534031, -47.591623, -46.649215, -45.706806,
       -44.764398, -43.82199 , -42.879581, -41.937173, -40.994764, -40.052356,
       -39.109948, -38.167539, -37.225131, -36.282723, -35.340314, -34.397906,
       -33.455497, -32.513089, -31.570681, -30.628272, -29.685864, -28.743455,
       -27.801047, -26.858639, -25.91623 , -24.973822, -24.031414, -23.089005,
       -22.146597, -21.204188, -20.26178 , -19.319372, -18.376963, -17.434555,
       -16.492147, -15.549738, -14.60733 , -13.664921, -12.722513, -11.780105,
       -10.837696,  -9.895288,  -8.95288 ,  -8.010471,  -7.068063,  -6.125654,
        -5.183246,  -4.240838,  -3.298429,  -2.356021,  -1.413613,  -0.471204,
         0.471204,   1.413613,   2.356021,   3.298429,   4.240838,   5.183246,
         6.125654,   7.068063,   8.010471,   8.95288 ,   9.895288,  10.837696,
        11.780105,  12.722513,  13.664921,  14.60733 ,  15.549738,  16.492147,
        17.434555,  18.376963,  19.319372,  20.26178 ,  21.204188,  22.146597,
        23.089005,  24.031414,  24.973822,  25.91623 ,  26.858639,  27.801047,
        28.743455,  29.685864,  30.628272,  31.570681,  32.513089,  33.455497,
        34.397906,  35.340314,  36.282723,  37.225131,  38.167539,  39.109948,
        40.052356,  40.994764,  41.937173,  42.879581,  43.82199 ,  44.764398,
        45.706806,  46.649215,  47.591623,  48.534031,  49.47644 ,  50.418848,
        51.361257,  52.303665,  53.246073,  54.188482,  55.13089 ,  56.073298,
        57.015707,  57.958115,  58.900524,  59.842932,  60.78534 ,  61.727749,
        62.670157,  63.612565,  64.554974,  65.497382,  66.439791,  67.382199,
        68.324607,  69.267016,  70.209424,  71.151832,  72.094241,  73.036649,
        73.979058,  74.921466,  75.863874,  76.806283,  77.748691,  78.691099,
        79.633508,  80.575916,  81.518325,  82.460733,  83.403141,  84.34555 ,
        85.287958,  86.230366,  87.172775,  88.115183,  89.057592,  90.      ])

* lat            array([  0.  ,   1.25,   2.5 , ..., 356.25, 357.5 , 358.75])

* time           array([cftime.DatetimeNoLeap(0001-01-15 12:00:00),
       cftime.DatetimeNoLeap(0001-02-14 00:00:00),
       cftime.DatetimeNoLeap(0001-03-15 12:00:00), ...,
       cftime.DatetimeNoLeap(1200-10-15 12:00:00),
       cftime.DatetimeNoLeap(1200-11-15 00:00:00),
       cftime.DatetimeNoLeap(1200-12-15 12:00:00)], dtype=object)

Data variables:
tas              (time, lat, lon) float32 ...

现在我想从变量tas中删除一个月的数据,我尝试了很多方法,但我做不到: 预期形状

ds.tas.shape:
(1199, 90, 70111) 

这是我的方法:

data.tas.sel(time = '0001-01-15', drop = True)
Giving me unexpected output

data.sel(time = '0001-01-15').drop(['tas'])
Giving me unexpected output

data.tas.drop_vars(time = '0001-01-15')
Giving Error

我知道这是一个简单的解决方案,但我找不到。请帮我解决这个问题。

更新整个数据集

data.tas = data.tas.where(data.tas.time!=cftime.DatetimeNoLeap(1,1,15,12,0,0), drop=True)

我以为这会更新整个数据集,但它给了我错误。

AttributeError: cannot set attribute 'tas' on a 'Dataset' object. Use __setitem__ styleassignment (e.g., `ds['name'] = ...`) instead of assigning variables.

您可以使用da.where()方法将您要保留的所有时间点隔离出来,然后丢弃其余的。

使用 cftime 建立索引:

import cftime 

# Extract DataArray
tas = data.tas

tas.where(tas.time!=cftime.DatetimeNoLeap(1,1,15,12,0,0), drop=True)

或者,因为在你的情况下你只想摆脱第一次索引,你可以这样做(而不用担心 cftime...)

# Extract DataArray
tas = data.tas

tas.where(tas.time!=tas.time[0], drop=True)

如果有帮助请告诉我! :)