Skip to content

BUG: IntervalIndex.from_tuples raising with masked subtype #56766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2024
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ Interval
- 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`)
- Bug in :meth:`IntervalIndex.factorize` and :meth:`Series.factorize` with :class:`IntervalDtype` with datetime64 or timedelta64 intervals not preserving non-nanosecond units (:issue:`56099`)
- Bug in :meth:`IntervalIndex.from_arrays` when passed ``datetime64`` or ``timedelta64`` arrays with mismatched resolutions constructing an invalid ``IntervalArray`` object (:issue:`55714`)
- Bug in :meth:`IntervalIndex.from_tuples` raising if subtype is a nullable extension dtype (:issue:`56765`)
- Bug in :meth:`IntervalIndex.get_indexer` with datetime or timedelta intervals incorrectly matching on integer targets (:issue:`47772`)
- Bug in :meth:`IntervalIndex.get_indexer` with timezone-aware datetime intervals incorrectly matching on a sequence of timezone-naive targets (:issue:`47772`)
- Bug in setting values on a :class:`Series` with an :class:`IntervalIndex` using a slice incorrectly raising (:issue:`54722`)
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
unique,
value_counts_internal as value_counts,
)
from pandas.core.arrays import ArrowExtensionArray
from pandas.core.arrays.base import (
ExtensionArray,
_extension_array_shared_docs,
Expand Down Expand Up @@ -369,11 +370,18 @@ def _ensure_simple_new_inputs(
right = ensure_wrapped_if_datetimelike(right)
right = extract_array(right, extract_numpy=True)

lbase = getattr(left, "_ndarray", left).base
rbase = getattr(right, "_ndarray", right).base
if lbase is not None and lbase is rbase:
# If these share data, then setitem could corrupt our IA
right = right.copy()
if isinstance(left, ArrowExtensionArray) or isinstance(
right, ArrowExtensionArray
):
pass
else:
lbase = getattr(left, "_ndarray", left)
lbase = getattr(lbase, "_data", lbase).base
rbase = getattr(right, "_ndarray", right)
rbase = getattr(rbase, "_data", rbase).base
if lbase is not None and lbase is rbase:
# If these share data, then setitem could corrupt our IA
right = right.copy()

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

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexes/interval/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas.core.dtypes.common import is_unsigned_integer_dtype
from pandas.core.dtypes.dtypes import IntervalDtype

Expand Down Expand Up @@ -508,3 +510,17 @@ def test_dtype_closed_mismatch():

with pytest.raises(ValueError, match=msg):
IntervalArray([], dtype=dtype, closed="neither")


@pytest.mark.parametrize(
"dtype",
["Float64", pytest.param("float64[pyarrow]", marks=td.skip_if_no("pyarrow"))],
)
def test_ea_dtype(dtype):
# GH#56765
bins = [(0.0, 0.4), (0.4, 0.6)]
interval_dtype = IntervalDtype(subtype=dtype, closed="left")
result = IntervalIndex.from_tuples(bins, closed="left", dtype=interval_dtype)
assert result.dtype == interval_dtype
expected = IntervalIndex.from_tuples(bins, closed="left").astype(interval_dtype)
tm.assert_index_equal(result, expected)