Skip to content

Commit dbd4f0b

Browse files
committed
Backport PR pandas-dev#25629: Suppress incorrect warning in nargsort for timezone-aware DatetimeIndex
1 parent caca8bc commit dbd4f0b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pandas/core/sorting.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" miscellaneous sorting / groupby utilities """
2+
import warnings
23

34
import numpy as np
45

@@ -254,7 +255,13 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'):
254255
sorted_idx = np.roll(sorted_idx, cnt_null)
255256
return sorted_idx
256257

257-
items = np.asanyarray(items)
258+
with warnings.catch_warnings():
259+
# https://github.com/pandas-dev/pandas/issues/25439
260+
# can be removed once ExtensionArrays are properly handled by nargsort
261+
warnings.filterwarnings(
262+
"ignore", category=FutureWarning,
263+
message="Converting timezone-aware DatetimeArray to")
264+
items = np.asanyarray(items)
258265
idx = np.arange(len(items))
259266
mask = isna(items)
260267
non_nans = items[~mask]

pandas/tests/test_sorting.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from numpy import nan
88
import pytest
99

10-
from pandas import DataFrame, MultiIndex, Series, compat, concat, merge
10+
from pandas import (
11+
DataFrame, MultiIndex, Series, compat, concat, merge, to_datetime)
1112
from pandas.core import common as com
1213
from pandas.core.sorting import (
1314
decons_group_index, get_group_index, is_int64_overflow_possible,
@@ -181,6 +182,13 @@ def test_nargsort(self):
181182
exp = list(range(5)) + list(range(105, 110)) + list(range(104, 4, -1))
182183
tm.assert_numpy_array_equal(result, np.array(exp), check_dtype=False)
183184

185+
def test_nargsort_datetimearray_warning(self):
186+
# https://github.com/pandas-dev/pandas/issues/25439
187+
# can be removed once the FutureWarning for np.array(DTA) is removed
188+
data = to_datetime([0, 2, 0, 1]).tz_localize('Europe/Brussels')
189+
with tm.assert_produces_warning(None):
190+
nargsort(data)
191+
184192

185193
class TestMerge(object):
186194

0 commit comments

Comments
 (0)