首页 > 解决方案 > 如何处理 NaT/1970 日期以便 python-xarray ds.time.dt.season 工作?

问题描述

我有这个 python-xarray 数据集:

<xarray.Dataset>
Dimensions:       (airport: 8, profnum: 9993, level: 3)
Coordinates:
  * airport       (airport) <U9 'Frankfurt' 'Windhoek' ... 'Madras' 'Hyderabad'
  * profnum       (profnum) int64 0 1 2 3 4 5 ... 9987 9988 9989 9990 9991 9992
  * level         (level) int64 0 1 2
    time          (airport, profnum, level) datetime64[ns] 2008-01-01T10:27:0...
    yearMonthDay  (airport, profnum, level) object '08-01-01' '08-01-01' ... nan
Data variables:
    iasi          (airport, profnum, level) float64 0.5094 1.345 ... nan nan
    IM            (airport, profnum, level) float64 0.515 1.775 ... nan nan
    IMS           (airport, profnum, level) float64 0.5221 1.514 ... nan nan
    err           (airport, profnum, level) float64 0.04518 0.2714 ... nan nan
    std           (airport, profnum, level) float64 0.0324 0.1542 ... nan nan
    dfs           (airport, profnum, level) float64 1.476 nan nan ... nan nan

ds.time 显示了一些 1970-01-01 日期,如果需要,我设法将其更改为 np.datetime64("NaT") 但 ds.time.dt.season 不喜欢它们。所以我这样做:

ds = ds.where( (ds.time.dt.year >= 2008) & (ds.time.dt.year <= currentYear), drop=True)
ds = ds.where( (ds.time.dt.year >= 2008) & (ds.time.dt.year <= currentYear), other=np.nan )

我希望在此之后我看不到任何带有 ds.time 的 1970 年日期,但替换不起作用。

看起来“其他”期望浮动,因为

ds.where( (ds.time.dt.year >= 2008) & (ds.time.dt.year <= currentYear), other=np.datetime64("NaT"))

输出

TypeError: The DTypes <class 'numpy.dtype[datetime64]'> and <class 'numpy.dtype[float64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.

这很奇怪,因为 df.time 是 datetime64。

谢谢

标签: python-xarray

解决方案


该声明

ds.where(
    (ds.time.dt.year >= 2008) & (ds.time.dt.year <= currentYear),
    other=np.datetime64("NaT"),
)

可以解释为

只要 2008 ≤ year ≤ currentYear,返回 ds,否则返回NaT

这会导致问题,因为此操作是针对数据集中的每个变量执行的。因为您的所有 data_variables 都是 type float64,所以您收到此错误。要仅及时替换值,请将您的条件限制为ds.time

ds.time.where(
    (ds.time.dt.year >= 2008) & (ds.time.dt.year <= currentYear),
    other=np.datetime64("NaT"),
)

推荐阅读