From 1836e4bb21521098ad54135990cf63d01670e575 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 16 Jan 2018 22:29:19 -0800 Subject: [PATCH 1/2] Fix tzawareness_compat for DatetimeIndex comparisons with NaT --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/core/indexes/datetimes.py | 5 ++++- pandas/tests/indexes/datetimes/test_datetime.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 853d5cee11cd1..7816a86becddf 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -431,7 +431,7 @@ Conversion - - Bug in ``.astype()`` to non-ns timedelta units would hold the incorrect dtype (:issue:`19176`, :issue:`19223`, :issue:`12425`) - Bug in subtracting :class:`Series` from ``NaT`` incorrectly returning ``NaT`` (:issue:`19158`) - +- Bug in comparison of timezone-aware :class:`DatetimeIndex` against ``NaT`` incorrectly raising ``TypeError`` (:issue:``) Indexing ^^^^^^^^ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index d83d2d2c93ec8..978674b9d2a8d 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -666,7 +666,10 @@ def _assert_tzawareness_compat(self, other): if is_datetime64tz_dtype(other): # Get tzinfo from Series dtype other_tz = other.dtype.tz - if self.tz is None: + if other is libts.NaT: + # pd.NaT quacks both aware and naive + pass + elif self.tz is None: if other_tz is not None: raise TypeError('Cannot compare tz-naive and tz-aware ' 'datetime-like objects.') diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index 41cd654cf22b9..bea5c5fc4754b 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -286,6 +286,20 @@ def test_comparison_tzawareness_compat(self, op): with pytest.raises(TypeError): op(dz, ts) + @pytest.mark.parametrize('op', [operator.eq, operator.ne, + operator.gt, operator.ge, + operator.lt, operator.le]) + def test_nat_comparison_tzawareness(self, op): + # tzaware DatetimeIndex should not raise when compared to NaT + dti = pd.DatetimeIndex(['2014-01-01', pd.NaT, '2014-03-01', pd.NaT, + '2014-05-01', '2014-07-01']) + expected = np.array([op == operator.ne] * len(dti)) + result = op(dti, pd.NaT) + tm.assert_numpy_array_equal(result, expected) + + result = op(dti.tz_localize('US/Pacific'), pd.NaT) + tm.assert_numpy_array_equal(result, expected) + def test_comparisons_coverage(self): rng = date_range('1/1/2000', periods=10) From c97e166d81a47b258b7fa8d8672b6c8aebb18818 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 16 Jan 2018 22:30:55 -0800 Subject: [PATCH 2/2] add gh issue refernece --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/tests/indexes/datetimes/test_datetime.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 7816a86becddf..e8923a4c0bc20 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -431,7 +431,7 @@ Conversion - - Bug in ``.astype()`` to non-ns timedelta units would hold the incorrect dtype (:issue:`19176`, :issue:`19223`, :issue:`12425`) - Bug in subtracting :class:`Series` from ``NaT`` incorrectly returning ``NaT`` (:issue:`19158`) -- Bug in comparison of timezone-aware :class:`DatetimeIndex` against ``NaT`` incorrectly raising ``TypeError`` (:issue:``) +- Bug in comparison of timezone-aware :class:`DatetimeIndex` against ``NaT`` incorrectly raising ``TypeError`` (:issue:`19276`) Indexing ^^^^^^^^ diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index bea5c5fc4754b..e3ebb8769db02 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -290,6 +290,7 @@ def test_comparison_tzawareness_compat(self, op): operator.gt, operator.ge, operator.lt, operator.le]) def test_nat_comparison_tzawareness(self, op): + # GH#19276 # tzaware DatetimeIndex should not raise when compared to NaT dti = pd.DatetimeIndex(['2014-01-01', pd.NaT, '2014-03-01', pd.NaT, '2014-05-01', '2014-07-01'])