From 5ecd6a31004a0784a1755a090f92eebb47f228b5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 23 Jan 2020 20:55:16 -0800 Subject: [PATCH 1/2] BUG: passing TDA and wrong freq to TimedeltaIndex --- pandas/core/indexes/timedeltas.py | 11 ----------- pandas/tests/indexes/timedeltas/test_constructors.py | 6 ++++++ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index d0a31b68250ad..6a82b0bb23258 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -163,17 +163,6 @@ def __new__( "represent unambiguous timedelta values durations." ) - if isinstance(data, TimedeltaArray): - if copy: - data = data.copy() - return cls._simple_new(data, name=name, freq=freq) - - if isinstance(data, TimedeltaIndex) and freq is None and name is None: - if copy: - return data.copy() - else: - return data._shallow_copy() - # - Cases checked above all return/raise before reaching here - # tdarr = TimedeltaArray._from_sequence( diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index 39abbf59d1e56..32e6821e87f05 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -47,6 +47,12 @@ def test_infer_from_tdi_mismatch(self): # GH#23789 TimedeltaArray(tdi, freq="D") + with pytest.raises(ValueError, match=msg): + TimedeltaIndex(tdi._data, freq="D") + + with pytest.raises(ValueError, match=msg): + TimedeltaArray(tdi._data, freq="D") + def test_dt64_data_invalid(self): # GH#23539 # passing tz-aware DatetimeIndex raises, naive or ndarray[datetime64] From 0401e27b389e96ef72a30789bdd3934897e54788 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Jan 2020 12:15:56 -0800 Subject: [PATCH 2/2] restore fastpath --- pandas/core/indexes/timedeltas.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 6a82b0bb23258..e78714487f01e 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -163,6 +163,17 @@ def __new__( "represent unambiguous timedelta values durations." ) + if isinstance(data, TimedeltaArray) and freq is None: + if copy: + data = data.copy() + return cls._simple_new(data, name=name, freq=freq) + + if isinstance(data, TimedeltaIndex) and freq is None and name is None: + if copy: + return data.copy() + else: + return data._shallow_copy() + # - Cases checked above all return/raise before reaching here - # tdarr = TimedeltaArray._from_sequence(