我尝试使用以下代码进行一些计算:
import numpy as np
import xarray as xr
import pandas as pd
ds1 = xr.open_mfdataset('HIST/*nc')
ds1 = ds1.loc[dict(time=slice('1996-01-01','2005-12-31'))]
ds1 = ds1.resample(time='24H').sum('time')
ds_yrmax1 = ds1.resample(time='AS').max('time')
我得到的完整错误是:
Traceback (most recent call last):
File "GFDL-CM3_interp_hist.py", line 29, in <module>
ds2 = ds2.resample(time='D').sum('time')
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/common.py", line 53, in wrapped_func
**kwargs)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/resample.py", line 303, in reduce
func, self._dim, keep_attrs, **kwargs)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/groupby.py", line 654, in reduce
return self.apply(reduce_dataset)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/resample.py", line 272, in apply
combined = self._combine(applied)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/groupby.py", line 619, in _combine
combined = self._maybe_restore_empty_groups(combined)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/groupby.py", line 342, in _maybe_restore_empty_groups
combined = combined.reindex(**indexers)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/dataset.py", line 1767, in reindex
tolerance, copy=copy)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/alignment.py", line 374, in reindex_variables
new_var = var._getitem_with_mask(key)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/variable.py", line 659, in _getitem_with_mask
data = duck_array_ops.where(mask, fill_value, data)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/duck_array_ops.py", line 164, in where
return _where(condition, *as_shared_dtype([x, y]))
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/duck_array_ops.py", line 107, in as_shared_dtype
out_type = dtypes.result_type(*arrays)
File "/opt/apps/python/3.6.4/lib/python3.6/site-
packages/xarray/core/dtypes.py", line 186, in result_type
return np.result_type(*arrays_and_dtypes)
TypeError: invalid type promotion
但是,当我摆脱似乎导致问题的线路时
(ds1 = ds1.resample(time='24H').sum('time')
),运行正常,尝试重新采样时我没有遇到任何其他错误。
这让我很困惑,我不确定问题出在哪里。事实上,当代码稍微改变时它工作得很好,这让我觉得有些问题特别是在获取总和而不是我的数据问题或重新采样本身时。我不确定,但不知道如何修复。
修改1:数据详情
数据为3小时降雨量数据,形状为(10,90,144)
。我试图找到每日总数,然后是每年的最大每日价值。
以下是打印时ds1
的完整信息,如果这也有帮助。(*切片前):
Dimensions: (bnds: 2, lat: 90, lon: 144, time: 61320)
Coordinates:
* lat (lat) float64 -89.0 -87.0 -85.0 -83.0 -81.0 -79.0 -77.0 ...
* lon (lon) float64 1.25 3.75 6.25 8.75 11.25 13.75 16.25 18.75 ...
* time (time) datetime64[ns] 1985-01-01T01:30:00 ...
Dimensions without coordinates: bnds
Data variables:
time_bnds (time, bnds) datetime64[ns] dask.array<shape=(61320, 2), chunksize=(14600, 2)>
lat_bnds (time, lat, bnds) float64 dask.array<shape=(61320, 90, 2), chunksize=(14600, 90, 2)>
lon_bnds (time, lon, bnds) float64 dask.array<shape=(61320, 144, 2), chunksize=(14600, 144, 2)>
pr (time, lat, lon) float32 dask.array<shape=(61320, 90, 144), chunksize=(14600, 90, 144)>
average_T1 (time) datetime64[ns] dask.array<shape=(61320,), chunksize=(14600,)>
average_T2 (time) datetime64[ns] dask.array<shape=(61320,), chunksize=(14600,)>
average_DT (time) timedelta64[ns] dask.array<shape=(61320,), chunksize=(14600,)>
Attributes:
title: NOAA GFDL GFDL-CM3, historical (run 1) experiment...
institute_id: NOAA GFDL
source: GFDL-CM3 2010 atmosphere: AM3 (AM3p9,C48L48); sea...
contact: gfdl.climate.model.info@noaa.gov
project_id: CMIP5
table_id: Table 3hr (31 Jan 2011)
experiment_id: historical
realization: 1
modeling_realm: atmos
tracking_id: e282fe4d-65a5-446f-8fc5-3cee468e6c71
Conventions: CF-1.4
references: The GFDL Data Portal (http://nomads.gfdl.noaa.gov...
comment: GFDL experiment name: CM3Z_D1_1860-2005_AllForc_H...
gfdl_experiment_name: CM3Z_D1_1860-2005_AllForc_H1
creation_date: 2012-02-21T21:23:03Z
model_id: GFDL-CM3
branch_time: 0.0
experiment: historical
forcing: GHG,SA,Oz,LU,Sl,Vl,SS,BC,MD,OC (GHG includes CO2,...
frequency: 3hr
initialization_method: 1
parent_experiment_id: piControl
physics_version: 1
product: output1
institution: NOAA GFDL(201 Forrestal Rd, Princeton, NJ, 08540)
history: File was processed by fremetar (GFDL analog of CM...
parent_experiment_rip: r1i1p1
institution_id: NOAA GFDL(201 Forrestal Rd, Princeton, NJ, 08540)