【问题标题】:Workaround for Pandas FutureWarning when sorting a DateTimeIndex对 DateTimeIndex 进行排序时 Pandas FutureWarning 的解决方法
【发布时间】:2019-07-18 04:33:18
【问题描述】:

here 所述,Pandas.sort_index() 在对 DateTimeIndex 进行排序时有时会发出 FutureWarning。该问题不可操作,因为它不包含 MCVE。这是一个:

import pandas as pd
idx = pd.DatetimeIndex(['2017-07-05 07:00:00', '2018-07-05 07:15:00','2017-07-05 07:30:00'])
df = pd.DataFrame({'C1':['a','b','c']},index=idx)
df = df.tz_localize('UTC')
df.sort_index()

警告看起来像:

FutureWarning:将时区感知的 DatetimeArray 转换为 具有 'datetime64[ns]' dtype 的时区天真 ndarray

堆栈(Pandas 0.24.1)是:

__array__, datetimes.py:358
asanyarray, numeric.py:544
nargsort, sorting.py:257
sort_index, frame.py:4795

该错误是从 datetimes.py 发出的,要求使用 dtype 参数调用它。但是,没有办法通过 nargsort 一直强制执行该操作——看起来服从 datetimes.py 的请求需要同时更改 pandas 和 numpy。

报告here。同时,您能想出一个我错过的解决方法吗?

【问题讨论】:

    标签: pandas


    【解决方案1】:

    已确认 0.24.2 里程碑的问题。解决方法是过滤警告,因此:

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

    【讨论】:

    • 对此的修复有任何更新吗?我正在使用pandas 0.25.1,但在执行np.isin(df.index, df.index)df.index 时我仍然看到相同的警告是一个可识别tz 的日期时间索引。不知道是同一个问题还是类似的问题。
    • 我正在使用 pandas 0.25.2 并且也收到这些警告 - 上面发布的“警告修复”没有解决它。
    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2021-07-02
    • 2019-09-04
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多