Skip to content

Commit bf28e02

Browse files
Backport PR #56766 on branch 2.2.x (BUG: IntervalIndex.from_tuples raising with masked subtype) (#56785)
Backport PR #56766: BUG: IntervalIndex.from_tuples raising with masked subtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 58c9ef7 commit bf28e02

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ Interval
817817
- Bug in :class:`Interval` ``__repr__`` not displaying UTC offsets for :class:`Timestamp` bounds. Additionally the hour, minute and second components will now be shown (:issue:`55015`)
818818
- Bug in :meth:`IntervalIndex.factorize` and :meth:`Series.factorize` with :class:`IntervalDtype` with datetime64 or timedelta64 intervals not preserving non-nanosecond units (:issue:`56099`)
819819
- Bug in :meth:`IntervalIndex.from_arrays` when passed ``datetime64`` or ``timedelta64`` arrays with mismatched resolutions constructing an invalid ``IntervalArray`` object (:issue:`55714`)
820+
- Bug in :meth:`IntervalIndex.from_tuples` raising if subtype is a nullable extension dtype (:issue:`56765`)
820821
- Bug in :meth:`IntervalIndex.get_indexer` with datetime or timedelta intervals incorrectly matching on integer targets (:issue:`47772`)
821822
- Bug in :meth:`IntervalIndex.get_indexer` with timezone-aware datetime intervals incorrectly matching on a sequence of timezone-naive targets (:issue:`47772`)
822823
- Bug in setting values on a :class:`Series` with an :class:`IntervalIndex` using a slice incorrectly raising (:issue:`54722`)

pandas/core/arrays/interval.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
unique,
8080
value_counts_internal as value_counts,
8181
)
82+
from pandas.core.arrays import ArrowExtensionArray
8283
from pandas.core.arrays.base import (
8384
ExtensionArray,
8485
_extension_array_shared_docs,
@@ -370,11 +371,18 @@ def _ensure_simple_new_inputs(
370371
right = ensure_wrapped_if_datetimelike(right)
371372
right = extract_array(right, extract_numpy=True)
372373

373-
lbase = getattr(left, "_ndarray", left).base
374-
rbase = getattr(right, "_ndarray", right).base
375-
if lbase is not None and lbase is rbase:
376-
# If these share data, then setitem could corrupt our IA
377-
right = right.copy()
374+
if isinstance(left, ArrowExtensionArray) or isinstance(
375+
right, ArrowExtensionArray
376+
):
377+
pass
378+
else:
379+
lbase = getattr(left, "_ndarray", left)
380+
lbase = getattr(lbase, "_data", lbase).base
381+
rbase = getattr(right, "_ndarray", right)
382+
rbase = getattr(rbase, "_data", rbase).base
383+
if lbase is not None and lbase is rbase:
384+
# If these share data, then setitem could corrupt our IA
385+
right = right.copy()
378386

379387
dtype = IntervalDtype(left.dtype, closed=closed)
380388

pandas/tests/indexes/interval/test_constructors.py

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import numpy as np
44
import pytest
55

6+
import pandas.util._test_decorators as td
7+
68
from pandas.core.dtypes.common import is_unsigned_integer_dtype
79
from pandas.core.dtypes.dtypes import IntervalDtype
810

@@ -517,3 +519,17 @@ def test_dtype_closed_mismatch():
517519

518520
with pytest.raises(ValueError, match=msg):
519521
IntervalArray([], dtype=dtype, closed="neither")
522+
523+
524+
@pytest.mark.parametrize(
525+
"dtype",
526+
["Float64", pytest.param("float64[pyarrow]", marks=td.skip_if_no("pyarrow"))],
527+
)
528+
def test_ea_dtype(dtype):
529+
# GH#56765
530+
bins = [(0.0, 0.4), (0.4, 0.6)]
531+
interval_dtype = IntervalDtype(subtype=dtype, closed="left")
532+
result = IntervalIndex.from_tuples(bins, closed="left", dtype=interval_dtype)
533+
assert result.dtype == interval_dtype
534+
expected = IntervalIndex.from_tuples(bins, closed="left").astype(interval_dtype)
535+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)