Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ Strings
Interval
^^^^^^^^
- :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`)
- 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`)
- Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`)
-

Indexing
^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,8 @@ cpdef bint is_interval_array(ndarray values):
for i in range(n):
val = values[i]

if isinstance(val, Interval):
if type(val) is Interval:
# GH#46945 catch Interval exactly, excluding subclasses
if closed is None:
closed = val.closed
numeric = (
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,24 @@ def test_is_string_array(self):
)
assert not lib.is_string_array(np.array([1, 2]))

def test_is_interval_array_subclass(self):
# GH#46945

class TimestampsInterval(Interval):
def __init__(self, left: str, right: str, closed: str = "both") -> None:
super().__init__(Timestamp(left), Timestamp(right), closed)

@property
def seconds(self) -> float:
return self.length.seconds

item = TimestampsInterval("1970-01-01 00:00:00", "1970-01-01 00:00:01")
arr = np.array([item], dtype=object)
assert not lib.is_interval_array(arr)
assert lib.infer_dtype(arr) != "interval"
out = Series([item])[0]
assert isinstance(out, TimestampsInterval)

@pytest.mark.parametrize(
"func",
[
Expand Down
Loading