-
-
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
core: fix DatetimeBlock operated with timedelta #22007
Conversation
effc448
to
66da338
Compare
Codecov Report
@@ Coverage Diff @@
## master #22007 +/- ##
==========================================
+ Coverage 92.02% 92.02% +<.01%
==========================================
Files 170 170
Lines 50710 50715 +5
==========================================
+ Hits 46664 46669 +5
Misses 4046 4046
Continue to review full report at Codecov.
|
expected = pd.DataFrame([pd.Timestamp('2018-01-02')]) | ||
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other | ||
tm.assert_frame_equal(result, expected) | ||
|
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.
Can you reference the issue as a comment under the function definition.
66da338
to
057a8f3
Compare
]) | ||
def test_timestamp_df_add_timedelta(self, other): | ||
# GH 22005 | ||
expected = pd.DataFrame([pd.Timestamp('2018-01-02')]) |
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.
can you add another test that uses a dataframe of mixed types (e.g. just add a string column and an integer column)
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.
int or str can't operate with Timedelta. Even if they are mixed in one DataFrame, they use different Block, won't be affect by DatetimeBlock here.
pd.Timedelta('1d') + pd.DataFrame([pd.Timestamp('2018-01-01'), 123])
TypeError: Could not operate Timedelta('1 days 00:00:00') with block values unsupported operand type(s) for +: 'Timedelta' and 'int'
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.
int or str can't operate with Timedelta.
The idea is 1) for the mixed-dtype cases that are valid, test them, 2) for (a few of) the mixed-dtype cases that are not, test that they raise as expected.
def test_timestamp_df_add_timedelta(self, other): | ||
# GH 22005 | ||
expected = pd.DataFrame([pd.Timestamp('2018-01-02')]) | ||
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other |
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.
can you add the reverse test as well (other + DataFrame...)
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.
While you're at it, might as well do subtraction too
pandas/core/internals/__init__.py
Outdated
@@ -2725,6 +2725,11 @@ def _try_coerce_args(self, values, other): | |||
"naive Block") | |||
other_mask = isna(other) | |||
other = other.asm8.view('i8') | |||
elif isinstance(other, (Timedelta, timedelta, np.timedelta64)): | |||
# should be the same as TimeDeltaBlock._box_func | |||
other = Timedelta(other, unit='ns') |
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.
you don't need the unit
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.
Also don't need to check for Timedelta, since it subclasses timedelta.
Pending requested fleshing out in tests (and rebase), this looks like a fine stop-gap solution until we rip out Block-based arithmetic altogether. |
057a8f3
to
518b3aa
Compare
The issue #22005 this was written for was closed by #22163. @holymonson are there other bugs this fixes? |
Nope, I will close this. |
git diff upstream/master -u -- "*.py" | flake8 --diff