python-xarray - 如何处理 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。
谢谢
解决方案
该声明
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"),
)
推荐阅读
- amazon-web-services - RHEL yum 更新命令在 aws ec2 实例中失败
- java - 如何在指定的时间间隔后查找当前页面上相对于速度的页数和行数?
- rust - 如何部分反序列化 JSON 对象?
- javascript - 滚动vuejs上的导航栏不透明度
- multithreading - perl中不同线程中全局数组的使用
- python - SQLAlchemy,添加具有外键约束依赖的记录
- java - 通知未显示 - 已设置 chennel 以及小图标、标题和文本
- python - 如何使用烧瓶在文件中的表中显示输出
- python - 我正在为单词益智游戏编写代码,但不知道如何将文本行包含到循环中
- python - 使用代理连接到 Google Cloud SQL 的 SQLAlchemy 错误