From 3ee563d06950322706e4ccdbdfdb9e826821967b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 4 Jan 2019 04:12:36 -0800 Subject: [PATCH] 24024 follow-up: fix incorrectly accepting iNaT in validate_fill_value (#24605) --- pandas/_libs/algos_common_helper.pxi.in | 2 -- pandas/core/arrays/datetimes.py | 2 +- pandas/core/dtypes/cast.py | 4 ++-- pandas/core/indexes/datetimelike.py | 9 --------- pandas/tests/arrays/test_datetimelike.py | 4 ++++ 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pandas/_libs/algos_common_helper.pxi.in b/pandas/_libs/algos_common_helper.pxi.in index 3708deb1a4b768..7d9ba420525c80 100644 --- a/pandas/_libs/algos_common_helper.pxi.in +++ b/pandas/_libs/algos_common_helper.pxi.in @@ -109,8 +109,6 @@ def ensure_object(object arr): return arr else: return arr.astype(np.object_) - elif hasattr(arr, '_box_values_as_index'): - return arr._box_values_as_index() else: return np.array(arr, dtype=np.object_) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 9f1491bd686843..a55e8759deedb6 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -588,7 +588,7 @@ def astype(self, dtype, copy=True): @Appender(dtl.DatetimeLikeArrayMixin._validate_fill_value.__doc__) def _validate_fill_value(self, fill_value): - if isna(fill_value) or fill_value == iNaT: + if isna(fill_value): fill_value = iNaT elif isinstance(fill_value, (datetime, np.datetime64)): self._assert_tzawareness_compat(fill_value) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index b55bad46580fe3..6696d6d4ca83e1 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -5,7 +5,7 @@ import numpy as np from pandas._libs import lib, tslib, tslibs -from pandas._libs.tslibs import OutOfBoundsDatetime, Period, iNaT +from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT from pandas.compat import PY3, string_types, text_type, to_str from .common import ( @@ -272,7 +272,7 @@ def maybe_promote(dtype, fill_value=np.nan): fill_value = tslibs.Timedelta(fill_value).value elif is_datetime64tz_dtype(dtype): if isna(fill_value): - fill_value = iNaT + fill_value = NaT elif is_extension_array_dtype(dtype) and isna(fill_value): fill_value = dtype.na_value elif is_float(fill_value): diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index cfca5d1b7d2cc3..082a314facdd67 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -203,15 +203,6 @@ def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise', return type(self)._simple_new(result, name=self.name) return arg - def _box_values_as_index(self): - """ - Return object Index which contains boxed values. - """ - # XXX: this is broken (not called) for PeriodIndex, which doesn't - # define _box_values AFAICT - from pandas.core.index import Index - return Index(self._box_values(self.asi8), name=self.name, dtype=object) - def _box_values(self, values): return self._data._box_values(values) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index f76999a0dbc323..db88d94be1cab7 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -388,6 +388,10 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture): # Timestamp with mismatched tz-awareness arr.take([-1, 1], allow_fill=True, fill_value=now) + with pytest.raises(ValueError): + # require NaT, not iNaT, as it could be confused with an integer + arr.take([-1, 1], allow_fill=True, fill_value=pd.NaT.value) + def test_concat_same_type_invalid(self, datetime_index): # different timezones dti = datetime_index