You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DARTS绘制预测结果时遇_nanquantile_dispatcher()参数错误

问题:DARTS绘图时触发TypeError: _nanquantile_dispatcher() got an unexpected keyword argument 'method'

我有一个约6000行的Pandas DataFrame,包含小时级belgium_time时间戳列与浮点型avg_value列。使用DARTS库完成时间序列预测后,调用prediction.plot(label="forecast", low_quantile=0.05, high_quantile=0.95)触发TypeError,报错栈信息如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File <command-3591413550716749>, line 4
      1 import matplotlib.pyplot as plt
      3 series.plot()
----> 4 prediction.plot(label="forecast", low_quantile=0.05, high_quantile=0.95)
      5 plt.legend()

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/darts/timeseries.py:3820, in TimeSeries.plot(self, new_plot, central_quantile, low_quantile, high_quantile, default_formatting, label, max_nr_components, ax, *args, **kwargs)
   3818         central_series = comp.mean(dim=DIMS[2])
   3819     else:
-> 3820         central_series = comp.quantile(q=central_quantile, dim=DIMS[2])
   3821 else:
   3822     central_series = comp.mean(dim=DIMS[2])

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/dataarray.py:5123, in DataArray.quantile(self, q, dim, method, keep_attrs, skipna, interpolation)
   5015 def quantile(
   5016     self: T_DataArray,
   5017     q: ArrayLike,
   (...)
   5022     interpolation: QuantileMethods | None = None,
   5023 ) -> T_DataArray:
   5024     """Compute the qth quantile of the data along the specified dimension.
   5025 
   5026     Returns the qth quantiles(s) of the array elements.
   (...)
   5120        The American Statistician, 50(4), pp. 361-365, 1996
   5121     """
-> 5123     ds = self._to_temp_dataset().quantile(
   5124         q,
   5125         dim=dim,
   5126         keep_attrs=keep_attrs,
   5127         method=method,
   5128         skipna=skipna,
   5129         interpolation=interpolation,
   5130     )
   5131     return self._from_temp_dataset(ds)

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/dataset.py:8035, in Dataset.quantile(self, q, dim, method, numeric_only, keep_attrs, skipna, interpolation)
   8029     if name not in self.coords:
   8030         if (
   8031             not numeric_only
   8032             or np.issubdtype(var.dtype, np.number)
   8033             or var.dtype == np.bool_
   8034         ):
-> 8035             variables[name] = var.quantile(
   8036                 q,
   8037                 dim=reduce_dims,
   8038                 method=method,
   8039                 keep_attrs=keep_attrs,
   8040                 skipna=skipna,
   8041             )
   8043 else:
   8044     variables[name] = var

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/variable.py:2292, in Variable.quantile(self, q, dim, method, keep_attrs, skipna, interpolation)
   2288 axis = np.arange(-1, -1 * len(dim) - 1, -1)
   2290 kwargs = {"q": q, "axis": axis, "method": method}
-> 2292 result = apply_ufunc(
   2293     _wrapper,
   2294     self,
   2295     input_core_dims=[dim],
   2296     exclude_dims=set(dim),
   2297     output_core_dims=[["quantile"]],
   2298     output_dtypes=[np.float64],
   2299     dask_gufunc_kwargs=dict(output_sizes={"quantile": len(q)}),
   2300     dask="parallelized",
   2301     kwargs=kwargs,
   2302 )
   2304 # for backward compatibility
   2305 result = result.transpose("quantile", ...)

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/computation.py:1207, in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
   1205 # feed Variables directly through apply_variable_ufunc
   1206 elif any(isinstance(a, Variable) for a in args):
-> 1207     return variables_vfunc(*args)
   1208 else:
   1209     # feed anything else through apply_array_ufunc
   1210     return apply_array_ufunc(func, *args, dask=dask)

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/computation.py:761, in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
    756     if vectorize:
    757         func = _vectorize(
    758             func, signature, output_dtypes=output_dtypes, exclude_dims=exclude_dims
    759         )
---> 761 result_data = func(*input_data)
    763 if signature.num_outputs == 1:
    764     result_data = (result_data,)

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-c1295c6b-8422-4d69-982b-173176ff6a3d/lib/python3.10/site-packages/xarray/core/variable.py:2286, in Variable.quantile.<locals>._wrapper(npa, **kwargs)
   2284 def _wrapper(npa, **kwargs):
   2285     # move quantile axis to end. required for apply_ufunc
-> 2286     return np.moveaxis(_quantile_func(npa, **kwargs), 0, -1)

File <__array_function__ internals>:4, in nanquantile(*args, **kwargs)

TypeError: _nanquantile_dispatcher() got an unexpected keyword argument 'method'

环境依赖版本:

  • darts 0.26.0
  • pandas 2.0.3
  • numpy 1.26.0

部分示例数据:

----+-----+-----+-------------------+------------------+--------------------+----------+------------------+------------------+------------------+------------------+------+-------+------------------+-------------------+-------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+---------+----------------------+
|  kp|  min|  max|     timestamp_hour|         avg_value|          value_diff|percentage|     moving_avg_7h|    moving_avg_30h|              5sma|              mean|stddev|z_score|    smoothed_value|             dt_iso|rain_1h|       belgium_time| Rain_cumulative_2h| Rain_cumulative_3h| Rain_cumulative_4h| Rain_cumulative_5h| Rain_cumulative_6h|dry_group|dry_hours_within_group|
+----+-----+-----+-------------------+------------------+--------------------+----------+------------------+------------------+------------------+------------------+------+-------+------------------+-------------------+-------+-------------------+-------------------+-------------------+-------------------+-------------------+-------------------+---------+----------------------+
|0090|150.0|848.0|2022-07-13 10:00:00|112.41159858703614|                 0.0|       0.0|112.41159858703614|112.41159858703614|112.41159858703614|112.41159858703614|   0.0|    0.0|112.41159858703614|2022-07-13 12:00:00|    0.0|2022-07-13 10:00:00|                0.0|                0.0|                0.0|                0.0|                0.0|     NULL|                     0|
|0090|150.0|848.0|2022-07-13 11:00:00|108.46443377043072| -3.9471648166054223|       0.0|110.43801617873342|110.43801617873342|110.43801617873342|110.43801617873342|   0.0|    0.0|110.43801617873342|2022-07-13 13:00:00|    0.0|2022-07-13 11:00:00|                0.0|                0.0|                0.0|                0.0|                0.0|     NULL|                     1|
|0090|150.0|848.0|2022-07-13 12:00:00|109.19111235245414|  0.7266785820234247|       0.0|110.02238156997366|110.02238156997366|110.02238156997366|110.02238156997366|   0.0|    0.0|110.02238156997366|2022-07-13 14:00:00|    0.0|2022-07-13 12:00:00|                0.0|                0.0|                0.0|                0.0|                0.0|     NULL|                     2|
|0090|150.0|848.0|2022-07-13 13:00:00|112.27772883365029|  3.0866164811961454|       0.0| 110.5862183858928| 110.5862183858928| 110.5862183858928| 110.5862183858928|   0.0|    0.0| 110.5862183858928|2022-07-13 15:00:00|    0.0|2022-07-13 13:00:00|                0.0|                0.0|                0.0|                0.0|                0.0|     NULL|                     3|
|0090|150.0|848.0|2022-07-13 14:00:00|111.91993414837381|-0.35779468527647396|       0.0|110.85296153838901|110.85296153838901|110.85296153838901|110.85296153838901|   0.0|    0.0|110.85296153838901|2022-07-13 16:00:00|    0.0|2022-07-13 14:00:00|                0.0|                0.0|                0.0|                0.0|                0.0|     NULL|                     4|

报错原因

这是版本兼容性冲突:

  • numpy 1.26.0对nanquantile函数做了参数调整,移除了method参数,改用interpolation参数替代
  • darts 0.26.0依赖的xarray版本仍在向numpy传递method参数,导致参数不匹配触发报错

解决方法

方案1:降级numpy版本

将numpy降级到1.25.x版本,该版本仍支持method参数,与darts 0.26.0兼容:

pip install numpy==1.25.2

方案2:升级darts版本

darts 0.27.0及以后版本修复了该兼容性问题,直接升级darts即可:

pip install --upgrade darts

临时替代方案(无需修改依赖)

如果暂时不想调整依赖版本,可以手动计算分位数并绘图,绕过DARTS的plot方法:

import matplotlib.pyplot as plt
import numpy as np

# 提取预测数据和时间索引
pred_array = prediction.values()
time_index = prediction.time_index

# 计算所需分位数
median = np.nanquantile(pred_array, 0.5, axis=1)
q05 = np.nanquantile(pred_array, 0.05, axis=1)
q95 = np.nanquantile(pred_array, 0.95, axis=1)

# 手动绘制序列和预测区间
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:00:53