Skip to content

BUG Fix: Allow using datetime64[ns, UTC] in IntervalDtype #47208

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 12 commits into from
Jun 7, 2022
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ Strings
Interval
^^^^^^^^
- Bug in :meth:`IntervalArray.__setitem__` when setting ``np.nan`` into an integer-backed array raising ``ValueError`` instead of ``TypeError`` (:issue:`45484`)
-
- Bug in :class:`IntervalDtype` when using datetime64[ns, tz] as a dtype string (:issue:`46999`)

Indexing
^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,10 +1075,12 @@ class IntervalDtype(PandasExtensionDtype):
"subtype",
"inclusive",
)

_match = re.compile(
r"(I|i)nterval\[(?P<subtype>[^,]+)(, ("
r"?P<inclusive>(right|left|both|neither)))?\]"
r"(I|i)nterval\[(?P<subtype>[^,]+(\[.+\])?)"
r"(, (?P<inclusive>(right|left|both|neither)))?\]"
)

_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

def __new__(
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,20 @@ def test_arrow_interval_type_error_and_warning():
msg = "Argument `closed` is deprecated in favor of `inclusive`"
with tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False):
ArrowIntervalType(pa.int64(), closed="both")


@pytest.mark.parametrize("timezone", ["UTC", "US/Pacific", "GMT"])
def test_interval_index_subtype(timezone, inclusive_endpoints_fixture):
# GH 46999
dates = date_range("2022", periods=3, tz=timezone)
dtype = f"interval[datetime64[ns, {timezone}], {inclusive_endpoints_fixture}]"
result = IntervalIndex.from_arrays(
["2022-01-01", "2022-01-02"],
["2022-01-02", "2022-01-03"],
inclusive=inclusive_endpoints_fixture,
dtype=dtype,
)
expected = IntervalIndex.from_arrays(
dates[:-1], dates[1:], inclusive=inclusive_endpoints_fixture
)
tm.assert_index_equal(result, expected)