Skip to content

Fix Series.__sub__ non-nano datetime64 #18783

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

Merged
merged 13 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from 8 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.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Reshaping
Numeric
^^^^^^^

-
- Bug in :func:`Series.__sub__` subtracting a non-nanosecond ``np.datetime64`` object from a ``Series`` gave incorrect results (:issue:`7996`)
-
-

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
is_datetimelike_v_numeric,
is_integer_dtype, is_categorical_dtype,
is_object_dtype, is_timedelta64_dtype,
is_datetime64_dtype, is_datetime64tz_dtype,
is_datetime64_dtype, is_datetime64tz_dtype, is_datetime64_ns_dtype,
is_bool_dtype, is_datetimetz,
is_list_like,
is_scalar,
Expand Down Expand Up @@ -497,6 +497,11 @@ def _convert_to_array(self, values, name=None, other=None):
elif not (isinstance(values, (np.ndarray, ABCSeries)) and
is_datetime64_dtype(values)):
values = libts.array_to_datetime(values)
elif (is_datetime64_dtype(values) and
not is_datetime64_ns_dtype(values)):
# GH#7996 e.g. np.datetime64('2013-01-01') is datetime64[D]
values = values.astype('datetime64[ns]')

elif inferred_type in ('timedelta', 'timedelta64'):
# have a timedelta, convert to to ns here
values = to_timedelta(values, errors='coerce', box=False)
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,36 @@ def test_timedelta64_ops_nat(self):


class TestDatetimeSeriesArithmetic(object):
def test_sub_datetime64_not_ns(self):
# GH#7996
ser = Series(date_range('20130101', periods=3))
dt64 = np.datetime64('2013-01-01')
assert dt64.dtype == 'datetime64[D]'
res = ser - dt64
expected = pd.Series([Timedelta(days=0), Timedelta(days=1),
Timedelta(days=2)])
tm.assert_series_equal(res, expected)

res = dt64 - ser
tm.assert_series_equal(res, -expected)
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 parametrize

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without introducing a whole lot of extra boilerplate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure that is the case. you are repeating Series/DTI testing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, just changed. It is much less obvious to a casual reader exactly what this is testing.


dti = pd.DatetimeIndex(ser)
res = dti - dt64
tm.assert_index_equal(res, pd.Index(expected))

res = dt64 - dti
tm.assert_index_equal(res, pd.Index(-expected))

@pytest.mark.xfail(reason='GH#7996 datetime64 units not converted to nano')
def test_frame_sub_datetime64_not_ns(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a separate issue for this? if not let's create one and reference it (as closing #7996 with this PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just opened #18874.

df = pd.DataFrame(date_range('20130101', periods=3))
dt64 = np.datetime64('2013-01-01')
assert dt64.dtype == 'datetime64[D]'
res = df - dt64
expected = pd.DataFrame([Timedelta(days=0), Timedelta(days=1),
Timedelta(days=2)])
tm.assert_frame_equal(res, expected)

def test_operators_datetimelike(self):
def run_ops(ops, get_ser, test_ser):

Expand Down