Skip to content

Commit 1fbc16d

Browse files
jbrockmendeljreback
authored andcommitted
disallow np.timedelta64 in is_integer (#27401)
1 parent 2ed1f28 commit 1fbc16d

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

pandas/_libs/tslibs/util.pxd

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ cdef inline bint is_integer_object(object obj) nogil:
7676
-----
7777
This counts np.timedelta64 objects as integers.
7878
"""
79-
return not PyBool_Check(obj) and PyArray_IsIntegerScalar(obj)
79+
return (not PyBool_Check(obj) and PyArray_IsIntegerScalar(obj)
80+
and not is_timedelta64_object(obj))
8081

8182

8283
cdef inline bint is_float_object(object obj) nogil:

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
593593
return lbound
594594
else:
595595
return lbound + to_offset(parsed.resolution_string) - Timedelta(1, "ns")
596-
elif (is_integer(label) or is_float(label)) and not is_timedelta64_dtype(label):
596+
elif is_integer(label) or is_float(label):
597597
self._invalid_indexer("slice", label)
598598

599599
return label

pandas/core/internals/blocks.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2609,15 +2609,13 @@ def _can_hold_element(self, element):
26092609
return issubclass(tipo.type, (np.timedelta64, np.int64))
26102610
elif element is NaT:
26112611
return True
2612-
return is_integer(element) or isinstance(
2613-
element, (timedelta, np.timedelta64, np.int64)
2614-
)
2612+
return is_integer(element) or isinstance(element, (timedelta, np.timedelta64))
26152613

26162614
def fillna(self, value, **kwargs):
26172615

26182616
# allow filling with integers to be
26192617
# interpreted as nanoseconds
2620-
if is_integer(value) and not isinstance(value, np.timedelta64):
2618+
if is_integer(value):
26212619
# Deprecation GH#24694, GH#19233
26222620
warnings.warn(
26232621
"Passing integers to fillna is deprecated, will "

pandas/tests/dtypes/test_inference.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1203,9 +1203,7 @@ def test_is_integer(self):
12031203
assert not is_integer(Timestamp("2011-01-01", tz="US/Eastern"))
12041204
assert not is_integer(timedelta(1000))
12051205
assert not is_integer(Timedelta("1 days"))
1206-
1207-
# questionable
1208-
assert is_integer(np.timedelta64(1, "D"))
1206+
assert not is_integer(np.timedelta64(1, "D"))
12091207

12101208
def test_is_float(self):
12111209
assert is_float(1.1)

0 commit comments

Comments
 (0)