当对DateTimeIndex进行排序时,Pandas出现FutureWarning的解决方法

4

此处所述,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转换为时区无关的ndarray,使用“datetime64 [ns]”dtype

堆栈信息(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。
这里报告。与此同时,您能否想到我错过的解决方法?
1个回答

0

已确认问题属于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-aware DateTimeIndex。不确定是否是相同的问题还是类似的。 - Ken Williams
我正在使用 pandas 0.25.2,也遇到了这些警告 - 上面发布的“警告修复”并没有解决它。 - Molecool

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接