Skip to content

Commit f5367d1

Browse files
Khor Chean Weiyehoshuadimarsky
Khor Chean Wei
authored andcommitted
BUG Fix: Allow using datetime64[ns, UTC] in IntervalDtype (pandas-dev#47208)
* add test * repair by comment * repair by comment * repair by comment * add test
1 parent 1c3f3a7 commit f5367d1

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ Strings
771771
Interval
772772
^^^^^^^^
773773
- Bug in :meth:`IntervalArray.__setitem__` when setting ``np.nan`` into an integer-backed array raising ``ValueError`` instead of ``TypeError`` (:issue:`45484`)
774-
-
774+
- Bug in :class:`IntervalDtype` when using datetime64[ns, tz] as a dtype string (:issue:`46999`)
775775

776776
Indexing
777777
^^^^^^^^

pandas/core/dtypes/dtypes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,12 @@ class IntervalDtype(PandasExtensionDtype):
10751075
"subtype",
10761076
"inclusive",
10771077
)
1078+
10781079
_match = re.compile(
1079-
r"(I|i)nterval\[(?P<subtype>[^,]+)(, ("
1080-
r"?P<inclusive>(right|left|both|neither)))?\]"
1080+
r"(I|i)nterval\[(?P<subtype>[^,]+(\[.+\])?)"
1081+
r"(, (?P<inclusive>(right|left|both|neither)))?\]"
10811082
)
1083+
10821084
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
10831085

10841086
def __new__(

pandas/tests/arrays/interval/test_interval.py

+17
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,20 @@ def test_arrow_interval_type_error_and_warning():
443443
msg = "Argument `closed` is deprecated in favor of `inclusive`"
444444
with tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False):
445445
ArrowIntervalType(pa.int64(), closed="both")
446+
447+
448+
@pytest.mark.parametrize("timezone", ["UTC", "US/Pacific", "GMT"])
449+
def test_interval_index_subtype(timezone, inclusive_endpoints_fixture):
450+
# GH 46999
451+
dates = date_range("2022", periods=3, tz=timezone)
452+
dtype = f"interval[datetime64[ns, {timezone}], {inclusive_endpoints_fixture}]"
453+
result = IntervalIndex.from_arrays(
454+
["2022-01-01", "2022-01-02"],
455+
["2022-01-02", "2022-01-03"],
456+
inclusive=inclusive_endpoints_fixture,
457+
dtype=dtype,
458+
)
459+
expected = IntervalIndex.from_arrays(
460+
dates[:-1], dates[1:], inclusive=inclusive_endpoints_fixture
461+
)
462+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)