【问题标题】:FutureWarning: Converting timezone-aware DatetimeArray to timezone-naive ndarray with 'datetime64[ns]' dtypeFutureWarning:使用 'datetime64[ns]' dtype 将时区感知 DatetimeArray 转换为时区朴素 ndarray
【发布时间】:2020-09-26 07:28:47
【问题描述】:

我从 pandas 0.20.3 升级到了 pandas 0.24.1。运行命令ts.sort_index(inplace=True) 时,我在测试输出中得到FutureWarning,如下所示。我可以更改方法调用以抑制以下警告吗?我很高兴保持旧的行为。

/lib/python3.6/site-packages/pandas/core/sorting.py:257: FutureWarning: Converting timezone-aware DatetimeArray to timezone-naive ndarray with 'datetime64[ns]' dtype. In the future, this will return an ndarray with 'object' dtype where each element is a 'pandas.Timestamp' with the correct 'tz'.
    To accept the future behavior, pass 'dtype=object'.
    To keep the old behavior, pass 'dtype="datetime64[ns]"'.
  items = np.asanyarray(items)

在运行 sort_index 之前,我的索引如下所示:

ts.index
DatetimeIndex(['2017-07-05 07:00:00+00:00', '2017-07-05 07:15:00+00:00',
               '2017-07-05 07:30:00+00:00', '2017-07-05 07:45:00+00:00',
               ...
               '2017-07-05 08:00:00+00:00'],
              dtype='datetime64[ns, UTC]', name='start', freq=None)

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    我重写了您的问题here,包括一个 MCVE。过了一段时间没有回复后,我发布了一个针对 Pandas 的问题。

    这是我的解决方法:

    with warnings.catch_warnings():
        # Bug in Pandas emits useless warning when sorting tz-aware index
        warnings.simplefilter("ignore")
        ds = df.sort_index()
    

    【讨论】:

      【解决方案2】:

      如果我是你,我会使用 pip 进行降级并设置以前的版本。这是更懒惰的答案。 但是如果你真的想保持升级,那么在 pandas 数据框中会有一个参数调用已弃用的警告。只需根据您的需要进行调整。您可以使用 pandas 的文档进行检查。 祝你有个愉快的夜晚

      【讨论】:

      • 感谢您的建议,但我需要升级才能访问更新的方法 to_parquet。我会阅读已弃用的警告。
      • 发现了什么? -W ignore::DeprecationWarning 应该在 Windows 上工作
      猜你喜欢
      • 2019-03-29
      • 2022-08-15
      • 2019-05-22
      • 2021-09-27
      • 2022-01-11
      • 2020-04-15
      • 2020-11-20
      • 2014-01-14
      • 2020-11-05
      相关资源
      最近更新 更多