Skip to content

Commit effc448

Browse files
committed
core: fix DatetimeBlock operated with timedelta
1 parent 322dbf4 commit effc448

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/core/internals.py

+5
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,11 @@ def _try_coerce_args(self, values, other):
27242724
"naive Block")
27252725
other_mask = isna(other)
27262726
other = other.asm8.view('i8')
2727+
elif isinstance(other, (Timedelta, timedelta, np.timedelta64)):
2728+
# should be the same as TimeDeltaBlock._box_func
2729+
other = Timedelta(other, unit='ns')
2730+
other_mask = isna(other)
2731+
other = other.asm8.view('i8')
27272732
elif hasattr(other, 'dtype') and is_datetime64_dtype(other):
27282733
other_mask = isna(other)
27292734
other = other.astype('i8', copy=False).view('i8')

pandas/tests/frame/test_arithmetic.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import datetime
3+
24
import pytest
35
import numpy as np
46

@@ -211,6 +213,16 @@ def test_df_sub_datetime64_not_ns(self):
211213
pd.Timedelta(days=2)])
212214
tm.assert_frame_equal(res, expected)
213215

216+
@pytest.mark.parametrize('other', [
217+
pd.Timedelta('1d'),
218+
datetime.timedelta(days=1),
219+
np.timedelta64(1, 'D')
220+
])
221+
def test_timestamp_df_add_timedelta(self, other):
222+
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
223+
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other
224+
tm.assert_frame_equal(result, expected)
225+
214226
@pytest.mark.parametrize('data', [
215227
[1, 2, 3],
216228
[1.1, 2.2, 3.3],

0 commit comments

Comments
 (0)