为何出现TypeError: Grouper.__init__()收到意外关键字参数'base'错误?
问题:xarray resample报错TypeError: Grouper.init() got an unexpected keyword argument 'base'
环境版本
- Jupyter Notebook:6.5.4
- Python:3.11.3
- Pandas:2.2.2
- xarray:2022.11.0
问题背景
我正在完成一项作业,使用降水数据集进行传感器选址(确定最佳降雨预测传感器位置),操作中在计算月度总量时执行代码报错。
操作步骤
- 读取NC文件:
import xarray as xr import pandas as pd nc_file_path = './pnwrain.50km.daily.4994.nc' dataset = xr.open_dataset(nc_file_path,decode_times=False) dataset
- 将time变量转换为datetime类型:
ref_date = pd.to_datetime('1949-01-01') # Convert 'time' variable to datetimes dataset['time'] = ref_date + pd.to_timedelta(dataset['time'], unit='D')
- 计算月度总量时执行代码:
monthly_totals = dataset['data'].resample(time='ME').sum()
报错信息
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 1 ----> 1 monthly_totals = dataset['data'].resample(time='ME').sum() 3 print("Monthly totals: ", monthly_totals) File ~\anaconda3\Lib\site-packages\xarray\core\dataarray.py:6636, in DataArray.resample(self, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs) 6540 """Returns a Resample object for performing resampling operations. 6541 6542 Handles both downsampling and upsampling. The resampled (...) 6632 .. [1] http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases 6633 """ 6634 from .resample import DataArrayResample -> 6636 return self._resample( 6637 resample_cls=DataArrayResample, 6638 indexer=indexer, 6639 skipna=skipna, 6640 closed=closed, 6641 label=label, 6642 base=base, 6643 keep_attrs=keep_attrs, 6644 loffset=loffset, 6645 restore_coord_dims=restore_coord_dims, 6646 **indexer_kwargs, 6647 ) File ~\anaconda3\Lib\site-packages\xarray\core\common.py:965, in DataWithCoords._resample(self, resample_cls, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs) 963 grouper = CFTimeGrouper(freq, closed, label, base, loffset) 964 else: -> 965 grouper = pd.Grouper( 966 freq=freq, closed=closed, label=label, base=base, loffset=loffset 967 ) 968 group = DataArray( 969 dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=RESAMPLE_DIM 970 ) 971 return resample_cls( 972 self, 973 group=group, (...) 977 restore_coord_dims=restore_coord_dims, 978 ) File ~\anaconda3\Lib\site-packages\pandas\core\resample.py:2208, in TimeGrouper.__init__(self, obj, freq, key, closed, label, how, axis, fill_method, limit, kind, convention, origin, offset, group_keys, **kwargs) 2205 # always sort time groupers 2206 kwargs["sort"] = True -> 2208 super().__init__(freq=freq, key=key, axis=axis, **kwargs) TypeError: Grouper.__init__() got an unexpected keyword argument 'base'
错误原因
这是版本兼容性问题:
- Pandas 2.0及以后版本移除了
pd.Grouper的base参数,但你使用的xarray 2022.11.0属于旧版本,在调用resample时仍会默认传递这个已被废弃的参数,两者版本不匹配导致报错。
内容的提问来源于stack exchange,提问作者Zi Xuan Chng
相关产品推荐
相关产品推荐

