Skip to content

Commit 066a4f7

Browse files
authored
BUG: inference with Interval subclass (#62680)
1 parent e8da6ba commit 066a4f7

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,8 @@ Strings
10171017
Interval
10181018
^^^^^^^^
10191019
- :meth:`Index.is_monotonic_decreasing`, :meth:`Index.is_monotonic_increasing`, and :meth:`Index.is_unique` could incorrectly be ``False`` for an ``Index`` created from a slice of another ``Index``. (:issue:`57911`)
1020+
- Bug in :class:`Index`, :class:`Series`, :class:`DataFrame` constructors when given a sequence of :class:`Interval` subclass objects casting them to :class:`Interval` (:issue:`46945`)
10201021
- Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`)
1021-
-
10221022

10231023
Indexing
10241024
^^^^^^^^

pandas/_libs/lib.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,8 @@ cpdef bint is_interval_array(ndarray values):
22552255
for i in range(n):
22562256
val = values[i]
22572257

2258-
if isinstance(val, Interval):
2258+
if type(val) is Interval:
2259+
# GH#46945 catch Interval exactly, excluding subclasses
22592260
if closed is None:
22602261
closed = val.closed
22612262
numeric = (

pandas/tests/dtypes/test_inference.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,24 @@ def test_is_string_array(self):
16051605
)
16061606
assert not lib.is_string_array(np.array([1, 2]))
16071607

1608+
def test_is_interval_array_subclass(self):
1609+
# GH#46945
1610+
1611+
class TimestampsInterval(Interval):
1612+
def __init__(self, left: str, right: str, closed="both") -> None:
1613+
super().__init__(Timestamp(left), Timestamp(right), closed)
1614+
1615+
@property
1616+
def seconds(self) -> float:
1617+
return self.length.seconds
1618+
1619+
item = TimestampsInterval("1970-01-01 00:00:00", "1970-01-01 00:00:01")
1620+
arr = np.array([item], dtype=object)
1621+
assert not lib.is_interval_array(arr)
1622+
assert lib.infer_dtype(arr) != "interval"
1623+
out = Series([item])[0]
1624+
assert isinstance(out, TimestampsInterval)
1625+
16081626
@pytest.mark.parametrize(
16091627
"func",
16101628
[

0 commit comments

Comments
 (0)