Skip to content

Commit 0dac2ee

Browse files
committed
core: cast result to TimeDeltaBlock if needed
1 parent a75f375 commit 0dac2ee

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pandas/core/internals/__init__.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
ABCSeries,
6464
ABCDatetimeIndex,
6565
ABCExtensionArray,
66-
ABCIndexClass)
66+
ABCIndexClass,
67+
ABCDateOffset,
68+
)
6769
import pandas.core.common as com
6870
import pandas.core.algorithms as algos
6971

@@ -2785,11 +2787,16 @@ def set(self, locs, values, check=False):
27852787

27862788
self.values[locs] = values
27872789

2788-
def eval(self, try_cast=False, *args, **kwargs):
2789-
blocks = super().eval(try_cast=try_cast, *args, **kwargs)
2790+
def eval(self, func, other, try_cast=False, **kwargs):
2791+
block = super().eval(func, other, try_cast=try_cast, **kwargs)[0]
27902792
if try_cast:
2791-
blocks = [self._try_coerce_result(block) for block in blocks]
2792-
return blocks
2793+
if isinstance(other,
2794+
(tslibs.Timestamp, np.datetime64, datetime, date)):
2795+
block = TimeDeltaBlock(block.values, block.mgr_locs,
2796+
ndim=block.ndim)
2797+
elif isinstance(other, ABCDateOffset):
2798+
block = self._try_coerce_result(block)
2799+
return [block]
27932800

27942801

27952802
class DatetimeTZBlock(NonConsolidatableMixIn, DatetimeBlock):

pandas/tests/frame/test_arithmetic.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import datetime
23
import pytest
34
import numpy as np
45

@@ -221,6 +222,17 @@ def test_timestamp_df_add_dateoffset(self):
221222
+ pd.DateOffset(years=1))
222223
tm.assert_frame_equal(expected, result)
223224

225+
@pytest.mark.parametrize('other', [
226+
pd.Timestamp('2017'),
227+
np.datetime64('2017'),
228+
datetime.datetime(2017, 1, 1),
229+
datetime.date(2017, 1, 1),
230+
])
231+
def test_timestamp_df_sub_timestamp(self, other):
232+
expected = pd.DataFrame([pd.Timedelta('365d')])
233+
result = pd.DataFrame([pd.Timestamp('2018')]) - other
234+
tm.assert_frame_equal(expected, result)
235+
224236
@pytest.mark.parametrize('data', [
225237
[1, 2, 3],
226238
[1.1, 2.2, 3.3],

0 commit comments

Comments
 (0)