Skip to content

BUG: Raise TypeError when subracting DateTimeArray and other date types #59901

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

Closed
Closed
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
99b1c3d
sync to master
KevsterAmp Sep 7, 2024
55e6e47
Merge remote-tracking branch 'upstream/main'
KevsterAmp Sep 7, 2024
e5b75f0
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Sep 25, 2024
2e041b9
raise TypeError when "-" is used between DateTimeArray
KevsterAmp Sep 26, 2024
11dd714
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Sep 26, 2024
b9f41fb
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Oct 3, 2024
9f14a2a
remove pandas library and improve formatting
KevsterAmp Oct 3, 2024
1df1edc
modify tests revolving this issue
KevsterAmp Oct 3, 2024
ca071a5
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Nov 12, 2024
5feb3a6
import outside top-level to handle DatetimeArray initialization
KevsterAmp Nov 12, 2024
8f7430f
replace prev error msg to new implemented error msg
KevsterAmp Nov 12, 2024
71a0c0f
replace prev error msg on test_timedelta64 with new error msg
KevsterAmp Nov 12, 2024
1aba9a2
specify dtype since test func doesn't contain multiple params/loops
KevsterAmp Nov 12, 2024
5b20b59
catch TypeError on DateTimeArrays specifically; Improve error message
KevsterAmp Nov 16, 2024
17ace06
fix tests due to changed error message
KevsterAmp Nov 16, 2024
25286eb
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Nov 16, 2024
23eb6c2
remove if statement to always raise the exception on TypeERror
KevsterAmp Dec 5, 2024
341d085
Merge remote-tracking branch 'upstream/main' into better-msg-delta-se…
KevsterAmp Dec 5, 2024
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
7 changes: 6 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import warnings

import numpy as np
import pandas

from pandas._config import using_string_dtype
from pandas._config.config import get_option
Expand Down Expand Up @@ -1488,7 +1489,11 @@ def __rsub__(self, other):
return (-self) + other

# We get here with e.g. datetime objects
return -(self - other)
datetime_result = self - other
if isinstance(datetime_result, pandas.core.arrays.datetimes.DatetimeArray):
raise TypeError("TypeError: unsupported operand type(s) for -: "
f"'{type(self).__name__}' and '{type(other).__name__}'")
return -(datetime_result)

def __iadd__(self, other) -> Self:
result = self + other
Expand Down
Loading