Skip to content

Commit

Permalink
core: fix DatetimeBlock operated with timedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
holymonson committed Jul 21, 2018
1 parent 322dbf4 commit 66da338
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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 @@ -440,7 +440,7 @@ Datetimelike
Timedelta
^^^^^^^^^

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

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,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')
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
12 changes: 12 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,16 @@ 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):
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other
tm.assert_frame_equal(result, expected)

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

0 comments on commit 66da338

Please sign in to comment.