From 0b040a1e5ac498d36e01bf5cb00d1b089aafa7d4 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 30 Oct 2023 08:00:20 -0400 Subject: [PATCH 1/4] BUG: DatetimeIndex.diff raising TypeError --- pandas/core/indexes/base.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 761f5df3bb4e0..a1bfe11becdad 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -7017,7 +7017,11 @@ def diff(self, periods: int = 1) -> Self: Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') """ - return self._constructor(self.to_series().diff(periods)) + diff = self.to_series().diff(periods) + if diff.dtype != self.dtype: + # e.g. DTI.diff -> TDI + return Index(diff) + return self._constructor(diff) @final def round(self, decimals: int = 0) -> Self: From 6dcefdcab5a0b31ec59c3b138bf6370d305f2a2a Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 30 Oct 2023 08:01:59 -0400 Subject: [PATCH 2/4] add test and whatsnew --- doc/source/whatsnew/v2.1.3.rst | 2 +- pandas/tests/indexes/test_datetimelike.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.3.rst b/doc/source/whatsnew/v2.1.3.rst index 1359123ef153e..ec5103c09215a 100644 --- a/doc/source/whatsnew/v2.1.3.rst +++ b/doc/source/whatsnew/v2.1.3.rst @@ -13,7 +13,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Fixed regression in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/indexes/test_datetimelike.py b/pandas/tests/indexes/test_datetimelike.py index 71cc7f29c62bc..5ad2e9b2f717e 100644 --- a/pandas/tests/indexes/test_datetimelike.py +++ b/pandas/tests/indexes/test_datetimelike.py @@ -159,3 +159,11 @@ def test_where_cast_str(self, simple_index): result = index.where(mask, ["foo"]) tm.assert_index_equal(result, expected) + + @pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"]) + def test_diff(self, unit): + # GH 55080 + dti = pd.to_datetime([10, 20, 30], unit=unit).as_unit(unit) + result = dti.diff(1) + expected = pd.TimedeltaIndex([pd.NaT, 10, 10], unit=unit).as_unit(unit) + tm.assert_index_equal(result, expected) From 0894b0c8aee2bdc3ba6990f44ee214c1ed19835f Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 30 Oct 2023 08:05:31 -0400 Subject: [PATCH 3/4] typing --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a1bfe11becdad..f2b29359d01a1 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6991,7 +6991,7 @@ def infer_objects(self, copy: bool = True) -> Index: return result @final - def diff(self, periods: int = 1) -> Self: + def diff(self, periods: int = 1) -> Index: """ Computes the difference between consecutive values in the Index object. From 05b6be3dc9eaedecfdafb5b6f1fe02e2ed4a58c4 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 30 Oct 2023 21:01:11 -0400 Subject: [PATCH 4/4] use Index --- doc/source/whatsnew/v2.1.3.rst | 4 ++-- pandas/core/indexes/base.py | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v2.1.3.rst b/doc/source/whatsnew/v2.1.3.rst index ec5103c09215a..3b1cd1c152baa 100644 --- a/doc/source/whatsnew/v2.1.3.rst +++ b/doc/source/whatsnew/v2.1.3.rst @@ -13,7 +13,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- Fixed regression in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`) +- - .. --------------------------------------------------------------------------- @@ -21,7 +21,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- +- Bug in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f2b29359d01a1..0301830df483b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -7017,11 +7017,7 @@ def diff(self, periods: int = 1) -> Index: Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64') """ - diff = self.to_series().diff(periods) - if diff.dtype != self.dtype: - # e.g. DTI.diff -> TDI - return Index(diff) - return self._constructor(diff) + return Index(self.to_series().diff(periods)) @final def round(self, decimals: int = 0) -> Self: