From 06b45dfaf28ac828170fed695a60ec5eca7e6a1b Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 00:12:55 +0800 Subject: [PATCH 1/5] add test --- pandas/core/dtypes/dtypes.py | 6 ++++-- pandas/tests/arrays/interval/test_interval.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 64d46976b54f6..08d6f59c67be6 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -1059,10 +1059,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..951f47f3ffd61 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -443,3 +443,15 @@ 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") + + +def test_interval_index_subtype(): + # GH 46999 + dates = date_range("2022", periods=3, tz="UTC") + result = IntervalIndex.from_arrays( + ["2022-01-01", "2022-01-02"], + ["2022-01-02", "2022-01-03"], + dtype="interval[datetime64[ns, UTC], both]", + ) + expected = IntervalIndex.from_arrays(dates[:-1], dates[1:]) + tm.assert_index_equal(result, expected) From a4247c669ec8d9434403cc88740be1be430bdbb0 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 18:24:43 +0800 Subject: [PATCH 2/5] repair by comment --- pandas/tests/arrays/interval/test_interval.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 951f47f3ffd61..d9925611a3c06 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -445,13 +445,17 @@ def test_arrow_interval_type_error_and_warning(): ArrowIntervalType(pa.int64(), closed="both") -def test_interval_index_subtype(): +@pytest.mark.parametrize("timezone", ["UTC", "US/Pacific"]) +@pytest.mark.parametrize("inclusive", ["both", "neither", "left", "right"]) +def test_interval_index_subtype(timezone, inclusive): # GH 46999 - dates = date_range("2022", periods=3, tz="UTC") + dates = date_range("2022", periods=3, tz=timezone) + dtype = f"interval[datetime64[ns, {timezone}], {inclusive}]" result = IntervalIndex.from_arrays( ["2022-01-01", "2022-01-02"], ["2022-01-02", "2022-01-03"], - dtype="interval[datetime64[ns, UTC], both]", + inclusive=inclusive, + dtype=dtype, ) - expected = IntervalIndex.from_arrays(dates[:-1], dates[1:]) + expected = IntervalIndex.from_arrays(dates[:-1], dates[1:], inclusive=inclusive) tm.assert_index_equal(result, expected) From e0d2a6b2d829dc717590f2ce4f19c4dd65528e3b Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 18:26:18 +0800 Subject: [PATCH 3/5] repair by comment --- pandas/tests/arrays/interval/test_interval.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index d9925611a3c06..3170aa38d4a9e 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -445,7 +445,7 @@ def test_arrow_interval_type_error_and_warning(): ArrowIntervalType(pa.int64(), closed="both") -@pytest.mark.parametrize("timezone", ["UTC", "US/Pacific"]) +@pytest.mark.parametrize("timezone", ["UTC", "US/Pacific", "GMT"]) @pytest.mark.parametrize("inclusive", ["both", "neither", "left", "right"]) def test_interval_index_subtype(timezone, inclusive): # GH 46999 From da53f9206c0f0cccf5364a8dc45b5efa838f8120 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 18:32:19 +0800 Subject: [PATCH 4/5] repair by comment --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 6bf6fd65f5633..715480aa7ca54 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -756,7 +756,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` Allow using datetime64[ns, UTC] in IntervalDtype (:issue:`46999`) Indexing ^^^^^^^^ From f62bef23cd7ca48d93f25d26fcee45d2bcc86db7 Mon Sep 17 00:00:00 2001 From: weikhor Date: Mon, 6 Jun 2022 16:14:04 +0800 Subject: [PATCH 5/5] add test --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/tests/arrays/interval/test_interval.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 715480aa7ca54..25175011f23a3 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -756,7 +756,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` Allow using datetime64[ns, UTC] in IntervalDtype (:issue:`46999`) +- Bug in :class:`IntervalDtype` when using datetime64[ns, tz] as a dtype string (:issue:`46999`) Indexing ^^^^^^^^ diff --git a/pandas/tests/arrays/interval/test_interval.py b/pandas/tests/arrays/interval/test_interval.py index 3170aa38d4a9e..44eafc72b1f5f 100644 --- a/pandas/tests/arrays/interval/test_interval.py +++ b/pandas/tests/arrays/interval/test_interval.py @@ -446,16 +446,17 @@ def test_arrow_interval_type_error_and_warning(): @pytest.mark.parametrize("timezone", ["UTC", "US/Pacific", "GMT"]) -@pytest.mark.parametrize("inclusive", ["both", "neither", "left", "right"]) -def test_interval_index_subtype(timezone, inclusive): +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}]" + 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, + inclusive=inclusive_endpoints_fixture, dtype=dtype, ) - expected = IntervalIndex.from_arrays(dates[:-1], dates[1:], inclusive=inclusive) + expected = IntervalIndex.from_arrays( + dates[:-1], dates[1:], inclusive=inclusive_endpoints_fixture + ) tm.assert_index_equal(result, expected)