From 56d336966ef4a8fafcac947aa84c7e13ba4c8b6b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 26 Nov 2019 18:42:20 -0800 Subject: [PATCH 1/2] TST: fix test broken by np 1.18 sort change --- pandas/tests/indexes/datetimes/test_ops.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_ops.py b/pandas/tests/indexes/datetimes/test_ops.py index c9c5963e5590c..cf1d9433db64a 100644 --- a/pandas/tests/indexes/datetimes/test_ops.py +++ b/pandas/tests/indexes/datetimes/test_ops.py @@ -13,6 +13,7 @@ PeriodIndex, Series, Timestamp, + _np_version_under1p18, bdate_range, date_range, ) @@ -254,9 +255,20 @@ def test_order_with_freq(self, idx): def test_order_without_freq(self, index_dates, expected_dates, tz_naive_fixture): tz = tz_naive_fixture + # FIXME: kludge since sort_values wih return_indexer==True uses the + # i8 values instead of the M8 values, so the np 1.18 behavior change + # does not affect it + orig_expected = expected_dates + + if not _np_version_under1p18 and expected_dates[0] is pd.NaT: + # numpy 1.18.0 sorts NaT to the end instead of the beginning, + # which matches float treatment of NaN + expected_dates = expected_dates[2:] + [pd.NaT, pd.NaT] + # without freq index = DatetimeIndex(index_dates, tz=tz, name="idx") expected = DatetimeIndex(expected_dates, tz=tz, name="idx") + expected_with_indexer = DatetimeIndex(orig_expected, tz=tz, name="idx") ordered = index.sort_values() tm.assert_index_equal(ordered, expected) @@ -267,14 +279,14 @@ def test_order_without_freq(self, index_dates, expected_dates, tz_naive_fixture) assert ordered.freq is None ordered, indexer = index.sort_values(return_indexer=True) - tm.assert_index_equal(ordered, expected) + tm.assert_index_equal(ordered, expected_with_indexer) exp = np.array([0, 4, 3, 1, 2]) tm.assert_numpy_array_equal(indexer, exp, check_dtype=False) assert ordered.freq is None ordered, indexer = index.sort_values(return_indexer=True, ascending=False) - tm.assert_index_equal(ordered, expected[::-1]) + tm.assert_index_equal(ordered, expected_with_indexer[::-1]) exp = np.array([2, 1, 3, 4, 0]) tm.assert_numpy_array_equal(indexer, exp, check_dtype=False) From 79845fbf0794265c397e32435103303db42b14d7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 26 Nov 2019 18:46:17 -0800 Subject: [PATCH 2/2] nicer fix --- pandas/core/indexes/datetimelike.py | 5 ++++- pandas/tests/indexes/datetimes/test_ops.py | 16 ++-------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b41227871ae03..9dcf62d472481 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -284,7 +284,10 @@ def sort_values(self, return_indexer=False, ascending=True): sorted_index = self.take(_as) return sorted_index, _as else: - sorted_values = np.sort(self._ndarray_values) + # NB: using asi8 instead of _ndarray_values matters in numpy 1.18 + # because the treatment of NaT has been changed to put NaT last + # instead of first. + sorted_values = np.sort(self.asi8) attribs = self._get_attributes_dict() freq = attribs["freq"] diff --git a/pandas/tests/indexes/datetimes/test_ops.py b/pandas/tests/indexes/datetimes/test_ops.py index cf1d9433db64a..c9c5963e5590c 100644 --- a/pandas/tests/indexes/datetimes/test_ops.py +++ b/pandas/tests/indexes/datetimes/test_ops.py @@ -13,7 +13,6 @@ PeriodIndex, Series, Timestamp, - _np_version_under1p18, bdate_range, date_range, ) @@ -255,20 +254,9 @@ def test_order_with_freq(self, idx): def test_order_without_freq(self, index_dates, expected_dates, tz_naive_fixture): tz = tz_naive_fixture - # FIXME: kludge since sort_values wih return_indexer==True uses the - # i8 values instead of the M8 values, so the np 1.18 behavior change - # does not affect it - orig_expected = expected_dates - - if not _np_version_under1p18 and expected_dates[0] is pd.NaT: - # numpy 1.18.0 sorts NaT to the end instead of the beginning, - # which matches float treatment of NaN - expected_dates = expected_dates[2:] + [pd.NaT, pd.NaT] - # without freq index = DatetimeIndex(index_dates, tz=tz, name="idx") expected = DatetimeIndex(expected_dates, tz=tz, name="idx") - expected_with_indexer = DatetimeIndex(orig_expected, tz=tz, name="idx") ordered = index.sort_values() tm.assert_index_equal(ordered, expected) @@ -279,14 +267,14 @@ def test_order_without_freq(self, index_dates, expected_dates, tz_naive_fixture) assert ordered.freq is None ordered, indexer = index.sort_values(return_indexer=True) - tm.assert_index_equal(ordered, expected_with_indexer) + tm.assert_index_equal(ordered, expected) exp = np.array([0, 4, 3, 1, 2]) tm.assert_numpy_array_equal(indexer, exp, check_dtype=False) assert ordered.freq is None ordered, indexer = index.sort_values(return_indexer=True, ascending=False) - tm.assert_index_equal(ordered, expected_with_indexer[::-1]) + tm.assert_index_equal(ordered, expected[::-1]) exp = np.array([2, 1, 3, 4, 0]) tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)