Skip to content

BUG: Fix infer_dtype_from_scalar to infer IntervalDtype #30339

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
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/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ Interval
^^^^^^^^

- Bug in :meth:`IntervalIndex.get_indexer` where a :class:`Categorical` or :class:`CategoricalIndex` ``target`` would incorrectly raise a ``TypeError`` (:issue:`30063`)
-
- Bug in ``pandas.core.dtypes.cast.infer_dtype_from_scalar`` where passing ``pandas_dtype=True`` did not infer :class:`IntervalDtype` (:issue:`30337`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove the whatsnew if it's not needed.


Indexing
^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
is_unsigned_integer_dtype,
pandas_dtype,
)
from .dtypes import DatetimeTZDtype, ExtensionDtype, PeriodDtype
from .dtypes import DatetimeTZDtype, ExtensionDtype, IntervalDtype, PeriodDtype
from .generic import (
ABCDataFrame,
ABCDatetimeArray,
Expand Down Expand Up @@ -601,6 +601,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
if lib.is_period(val):
dtype = PeriodDtype(freq=val.freq)
val = val.ordinal
elif lib.is_interval(val):
subtype = infer_dtype_from_scalar(val.left, pandas_dtype=True)[0]
dtype = IntervalDtype(subtype=subtype)

return dtype, val

Expand Down
29 changes: 28 additions & 1 deletion pandas/tests/dtypes/cast/test_infer_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
)
from pandas.core.dtypes.common import is_dtype_equal

from pandas import Categorical, Period, Series, Timedelta, Timestamp, date_range
from pandas import (
Categorical,
Interval,
Period,
Series,
Timedelta,
Timestamp,
date_range,
)
import pandas.util.testing as tm


Expand Down Expand Up @@ -107,6 +115,25 @@ def test_infer_from_scalar_tz(tz, pandas_dtype):
assert val == exp_val


@pytest.mark.parametrize(
"left, right, subtype",
[
(0, 1, "int64"),
(0.0, 1.0, "float64"),
(Timestamp(0), Timestamp(1), "datetime64[ns]"),
(Timestamp(0, tz="UTC"), Timestamp(1, tz="UTC"), "datetime64[ns, UTC]"),
(Timedelta(0), Timedelta(1), "timedelta64[ns]"),
],
)
def test_infer_from_interval(left, right, subtype, closed, pandas_dtype):
# GH 30337
interval = Interval(left, right, closed)
result_dtype, result_value = infer_dtype_from_scalar(interval, pandas_dtype)
expected_dtype = f"interval[{subtype}]" if pandas_dtype else np.object_
assert result_dtype == expected_dtype
assert result_value == interval


def test_infer_dtype_from_scalar_errors():
msg = "invalid ndarray passed to infer_dtype_from_scalar"

Expand Down