Skip to content

Commit 62045c3

Browse files
jbrockmendeljreback
authored andcommitted
24024 follow-up: fix incorrectly accepting iNaT in validate_fill_value (#24605)
1 parent 1574b0e commit 62045c3

File tree

5 files changed

+7
-14
lines changed

5 files changed

+7
-14
lines changed

pandas/_libs/algos_common_helper.pxi.in

-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ def ensure_object(object arr):
109109
return arr
110110
else:
111111
return arr.astype(np.object_)
112-
elif hasattr(arr, '_box_values_as_index'):
113-
return arr._box_values_as_index()
114112
else:
115113
return np.array(arr, dtype=np.object_)
116114

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def astype(self, dtype, copy=True):
588588

589589
@Appender(dtl.DatetimeLikeArrayMixin._validate_fill_value.__doc__)
590590
def _validate_fill_value(self, fill_value):
591-
if isna(fill_value) or fill_value == iNaT:
591+
if isna(fill_value):
592592
fill_value = iNaT
593593
elif isinstance(fill_value, (datetime, np.datetime64)):
594594
self._assert_tzawareness_compat(fill_value)

pandas/core/dtypes/cast.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import lib, tslib, tslibs
8-
from pandas._libs.tslibs import OutOfBoundsDatetime, Period, iNaT
8+
from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT
99
from pandas.compat import PY3, string_types, text_type, to_str
1010

1111
from .common import (
@@ -272,7 +272,7 @@ def maybe_promote(dtype, fill_value=np.nan):
272272
fill_value = tslibs.Timedelta(fill_value).value
273273
elif is_datetime64tz_dtype(dtype):
274274
if isna(fill_value):
275-
fill_value = iNaT
275+
fill_value = NaT
276276
elif is_extension_array_dtype(dtype) and isna(fill_value):
277277
fill_value = dtype.na_value
278278
elif is_float(fill_value):

pandas/core/indexes/datetimelike.py

-9
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,6 @@ def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
203203
return type(self)._simple_new(result, name=self.name)
204204
return arg
205205

206-
def _box_values_as_index(self):
207-
"""
208-
Return object Index which contains boxed values.
209-
"""
210-
# XXX: this is broken (not called) for PeriodIndex, which doesn't
211-
# define _box_values AFAICT
212-
from pandas.core.index import Index
213-
return Index(self._box_values(self.asi8), name=self.name, dtype=object)
214-
215206
def _box_values(self, values):
216207
return self._data._box_values(values)
217208

pandas/tests/arrays/test_datetimelike.py

+4
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
388388
# Timestamp with mismatched tz-awareness
389389
arr.take([-1, 1], allow_fill=True, fill_value=now)
390390

391+
with pytest.raises(ValueError):
392+
# require NaT, not iNaT, as it could be confused with an integer
393+
arr.take([-1, 1], allow_fill=True, fill_value=pd.NaT.value)
394+
391395
def test_concat_same_type_invalid(self, datetime_index):
392396
# different timezones
393397
dti = datetime_index

0 commit comments

Comments
 (0)