从xarray数据集中选择日期列表

2024-04-29 09:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有包含以下信息的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 ...

现在我想知道具体日期的助教值,所以我做了如下列表:

[array(cftime.DatetimeNoLeap(0500-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0341-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(1147-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0943-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0011-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0359-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0705-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0494-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0204-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0127-01-14 00:00:00), dtype=object),
 array(cftime.DatetimeNoLeap(0386-01-14 00:00:00), dtype=object)]

然后尝试使用如下sel语句:

ds.sel(time = dates)

但它不起作用。有人能告诉我如何选择这样的具体日期吗

这种方法适用于我的时间是datetime64格式的情况


Tags: 数据信息objecttimedsarraydatasetxarray
1条回答
网友
1楼 · 发布于 2024-04-29 09:15:31

它看起来像数组中列表中的每个项目。您希望每个项目都是项目。因此,您的dates列表应该如下所示:

[cftime.DatetimeNoLeap(0500-01-14 00:00:00),
 cftime.DatetimeNoLeap(0341-01-14 00:00:00),
 ...

这样行吗

相关问题 更多 >