Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -7017,7 +7017,7 @@ 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))
return Index(self.to_series().diff(periods))

@final
def round(self, decimals: int = 0) -> Self:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)