Skip to content

BUG: DatetimeIndex.diff raising TypeError #55761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -13,7 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression in :meth:`DatetimeIndex.diff` raising ``TypeError`` (:issue:`55080`)
-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 6 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,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just return Index(diff) unconditionally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use self._constructor in order to preserve subclasses as much as possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any cases where it makes a difference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class MyIndex(Index):
    def _constructor(self):
        return MyIndex

>>> MyIndex([1, 2]).diff(MyIndex([1, 3]))

Wouldn't it be a regression if we didn't return a MyIndex here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont support 3rd part Index subclasses

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is noted in the docs that Index subclasses are not supported. @mroeschke, any objections to updating based on that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay I didn't know we didn't support Index subclasses. Fine to update to @jbrockmendel suggestion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to return Index(diff) unconditionally

return self._constructor(diff)

@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)