-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: Raise TypeError when subracting DateTimeArray and other date types #59901
base: main
Are you sure you want to change the base?
BUG: Raise TypeError when subracting DateTimeArray and other date types #59901
Conversation
@rhshadrach - can you verify if my implementation here is correct? I'm not sure of this. A bit of guidance could be useful. Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is looking good, CI failures look unrelated. They'll be fixed outside of this PR.
from pandas.core.arrays import DatetimeArray
datetime_result = self - other
if isinstance(datetime_result, DatetimeArray):
raise TypeError(
"TypeError: unsupported operand type(s) for -: "
f"'{type(self).__name__}' and '{type(other).__name__}'"
)
return -(datetime_result) I added an outside top-level import because I am experiencing if isinstance(datetime_result, DatetimeArray): Also, I replaced the messages with the variable type as any wildcard r"TypeError: unsupported operand type\(s\) for -: 'DatetimeArray' and '.*'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@mroeschke - could you review this. Thanks |
pandas/core/arrays/datetimelike.py
Outdated
datetime_result = self - other | ||
if isinstance(datetime_result, DatetimeArray): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little nervous enforcing a particular type out of a rsub
operation.
Could we just catch the TypeError
and reraise it with a better message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I agree that it would be a better implementation
My implementation Since there are already other error messages for other dtypes like Cannot subtract tz-naive and tz-aware datetime-like objects. If I implement it as try:
return -(self - other)
except TypeError as e:
raise TypeError(
"Unsupported operand type(s) for -: "
f"'{type(self).__name__}' and '{type(other).__name__}'"
) from e I would have to fix all tests related to raising |
Looks like my previous commit affects other datetime related operations. Could you give me ideas on catching the error? From my understanding, when a datetime objects go into this block return -(self - other) It will always raise a # We get here with e.g. datetime objects
from pandas.core.arrays import DatetimeArray
datetime_result = self - other
if isinstance(datetime_result, DatetimeArray):
raise TypeError(
"Unsupported operand type(s) for -: "
f"'{type(self).__name__}' and '{type(other).__name__}'"
)
return -(datetime_result) Though from checking the CI failures, I can't seem to trace/ I'm not familiar how my latest commit affected them. Guidance would be helpful. Thank you! |
pandas/core/arrays/datetimelike.py
Outdated
try: | ||
return -(datetime_result) | ||
except TypeError as e: | ||
if isinstance(datetime_result, DatetimeArray): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this if
statement here. We'd always want to raise the exception
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.