Skip to content

Commit

Permalink
core: try coerce result back to DatetimeBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
holymonson committed Jul 22, 2018
1 parent 37c7458 commit a75f375
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ Datetimelike

- Fixed bug where two :class:`DateOffset` objects with different ``normalize`` attributes could evaluate as equal (:issue:`21404`)
- Fixed bug where :meth:`Timestamp.resolution` incorrectly returned 1-microsecond ``timedelta`` instead of 1-nanosecond :class:`Timedelta` (:issue:`21336`,:issue:`21365`)
- Fixed bug where :class:`DataFrame` with ``dtype='datetime64[ns]'`` operating with :class:`DateOffset` could cast to ``dtype='object'`` (:issue:`21610`)

Timedelta
^^^^^^^^^
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,7 @@ def _try_coerce_args(self, values, other):

def _try_coerce_result(self, result):
""" reverse of try_coerce_args """
if isinstance(result, np.ndarray):
if isinstance(result, (np.ndarray, Block)):
if result.dtype.kind in ['i', 'f', 'O']:
try:
result = result.astype('M8[ns]')
Expand Down Expand Up @@ -2785,6 +2785,12 @@ def set(self, locs, values, check=False):

self.values[locs] = values

def eval(self, try_cast=False, *args, **kwargs):
blocks = super().eval(try_cast=try_cast, *args, **kwargs)
if try_cast:
blocks = [self._try_coerce_result(block) for block in blocks]
return blocks


class DatetimeTZBlock(NonConsolidatableMixIn, DatetimeBlock):
""" implement a datetime64 block with a tz attribute """
Expand Down Expand Up @@ -2920,6 +2926,8 @@ def _try_coerce_result(self, result):
if isinstance(result, np.ndarray):
if result.dtype.kind in ['i', 'f', 'O']:
result = result.astype('M8[ns]')
elif isinstance(result, Block):
result = self.make_block_same_class(result.values.flat)
elif isinstance(result, (np.integer, np.float, np.datetime64)):
result = tslibs.Timestamp(result, tz=self.values.tz)
if isinstance(result, np.ndarray):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ def test_df_sub_datetime64_not_ns(self):
pd.Timedelta(days=2)])
tm.assert_frame_equal(res, expected)

def test_timestamp_df_add_dateoffset(self):
expected = pd.DataFrame([pd.Timestamp('2019')])
result = pd.DataFrame([pd.Timestamp('2018')]) + pd.DateOffset(years=1)
tm.assert_frame_equal(expected, result)

expected = pd.DataFrame([pd.Timestamp('2019', tz='Asia/Shanghai')])
result = (pd.DataFrame([pd.Timestamp('2018', tz='Asia/Shanghai')])
+ pd.DateOffset(years=1))
tm.assert_frame_equal(expected, result)

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

0 comments on commit a75f375

Please sign in to comment.