diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 92ea3460ae646..990517ef460e0 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -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 ^^^^^^^^ diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 90a22fc361ad3..32594854f49ae 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1075,10 +1075,12 @@ class IntervalDtype(PandasExtensionDtype): "subtype", "inclusive", ) + _match = re.compile( - r"(I|i)nterval\[(?P[^,]+)(, (" - r"?P(right|left|both|neither)))?\]" + r"(I|i)nterval\[(?P[^,]+(\[.+\])?)" + r"(, (?P(right|left|both|neither)))?\]" ) + _cache_dtypes: dict[str_type, PandasExtensionDtype] = {} def __new__( diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 28ffd5b4caf98..44eafc72b1f5f 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -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)