Skip to content

Commit 6613784

Browse files
authored
BUG: IntervalIndex.from_tuples raising with masked subtype (#56766)
1 parent 764b1ef commit 6613784

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
@@ -80,6 +80,7 @@
8080
unique,
8181
value_counts_internal as value_counts,
8282
)
83+
from pandas.core.arrays import ArrowExtensionArray
8384
from pandas.core.arrays.base import (
8485
ExtensionArray,
8586
_extension_array_shared_docs,
@@ -369,11 +370,18 @@ def _ensure_simple_new_inputs(
369370
right = ensure_wrapped_if_datetimelike(right)
370371
right = extract_array(right, extract_numpy=True)
371372

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

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

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

@@ -508,3 +510,17 @@ def test_dtype_closed_mismatch():
508510

509511
with pytest.raises(ValueError, match=msg):
510512
IntervalArray([], dtype=dtype, closed="neither")
513+
514+
515+
@pytest.mark.parametrize(
516+
"dtype",
517+
["Float64", pytest.param("float64[pyarrow]", marks=td.skip_if_no("pyarrow"))],
518+
)
519+
def test_ea_dtype(dtype):
520+
# GH#56765
521+
bins = [(0.0, 0.4), (0.4, 0.6)]
522+
interval_dtype = IntervalDtype(subtype=dtype, closed="left")
523+
result = IntervalIndex.from_tuples(bins, closed="left", dtype=interval_dtype)
524+
assert result.dtype == interval_dtype
525+
expected = IntervalIndex.from_tuples(bins, closed="left").astype(interval_dtype)
526+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)