Skip to content
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

Closed
wants to merge 2 commits into from
Closed
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/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Datetimelike
Timedelta
^^^^^^^^^

-
- Bug in :class:`DataFrame` with ``dtype='datetime64[ns]'`` when adding :class:`Timedelta` (:issue:`22005`)
-
-

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,11 @@ def _try_coerce_args(self, values, other):
"naive Block")
other_mask = isna(other)
other = other.asm8.view('i8')
elif isinstance(other, (timedelta, np.timedelta64)):
if not isinstance(other, Timedelta):
other = Timedelta(other)
other_mask = isna(other)
other = other.asm8.view('i8')
elif hasattr(other, 'dtype') and is_datetime64_dtype(other):
other_mask = isna(other)
other = other.astype('i8', copy=False).view('i8')
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import datetime

import pytest
import numpy as np

Expand Down Expand Up @@ -211,6 +213,23 @@ def test_df_sub_datetime64_not_ns(self):
pd.Timedelta(days=2)])
tm.assert_frame_equal(res, expected)

@pytest.mark.parametrize('other', [
pd.Timedelta('1d'),
datetime.timedelta(days=1),
np.timedelta64(1, 'D')
])
def test_timestamp_df_add_timedelta(self, other):
# GH 22005
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
Copy link
Contributor

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)

Copy link
Contributor Author

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'

Copy link
Member

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.

result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other
Copy link
Contributor

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

Copy link
Member

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

tm.assert_frame_equal(result, expected)

result = pd.DataFrame([pd.Timestamp('2018-01-03')]) - other
tm.assert_frame_equal(result, expected)

result = other + pd.DataFrame([pd.Timestamp('2018-01-01')])
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize('data', [
[1, 2, 3],
[1.1, 2.2, 3.3],
Expand Down