FutureWarning的变通办法

FutureWarning的变通办法

本文介绍了对DateTimeIndex排序时 pandas FutureWarning的变通办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As described here, Pandas.sort_index() sometimes emits a FutureWarning when doing a sort on a DateTimeIndex. That question isn't actionable, since it contains no MCVE. Here's one:

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()

警告看起来像:

堆栈(Pandas 0.24.1)是:

The stack (Pandas 0.24.1) is:

__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.

The error is emitted from datetimes.py, requesting that it be called with a dtype argument. However, there's no way to force that all the way up through nargsort -- it looks like obeying datetimes.py's request would require changes to both pandas and numpy.

此处报告.同时,您能想到我错过的解决方法吗?

Reported here. In the meantime, can you think of a workaround that I've missed?

推荐答案

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

Issue confirmed for the 0.24.2 milestone. Workaround is to filter the warning, thus:

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

这篇关于对DateTimeIndex排序时 pandas FutureWarning的变通办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:48