Python:在将 np.polyfit 应用于 xarray 数据集时出现“cftime._cftime.DatetimeNoLeap”问题

问题描述 投票:0回答:0

我正在使用 Python 中的 Xarray 处理 CMIP6 NETCdf 温度数据。我能够在给定点绘制时间序列,但无法使用 np.polyfit 创建趋势线。它返回错误:TypeError: unsupported operand type(s) for +: 'cftime._cftime.DatetimeNoLeap' and 'float'。

文件链接:https://file.io/bKO0rfAp1z8K

我的脚本如下:

import numpy as np
import matplotlib.pyplot as plt
import xarray as xr

dataset = xr.load_dataset("file.nc")
airTemp = dataset['tas']
nome_temperature = airTemp.sel(lat=64.5006, lon=194.5914, method='nearest')

#plotting the timeseries works
plt.plot(nome_temperature.time, nome_temperature.values)

#error when I try to plot the polyfit tredline
z = np.polyfit(nome_temperature.time, nome_temperature.values, 1)
p = np.poly1d(z)
plt.plot(nome_temperature.time, p(nome_temperature.time))

不太确定这是否是时间格式问题,或者我是否只是错过了在趋势线中使用时间之前更改时间的步骤。我对常规时间序列图中的时间没有问题,所以我不确定趋势线有什么问题。

python typeerror netcdf python-xarray trendline
© www.soinside.com 2019 - 2024. All rights reserved.