Skip to content

DEPR: DatetimeIndex/PeriodsIndex +/- ops (GH9094) #9630

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 1 commit into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ Deprecations
- The ``pandas.rpy`` interface is deprecated and will be removed in a future version.
Similar functionaility can be accessed thru the `rpy2 <http://rpy.sourceforge.net/>`_ project (:issue:`9602`)

- Adding ``DatetimeIndex/PeriodIndex`` to another ``DatetimeIndex/PeriodIndex`` is being deprecated as a set-operation. This will be changed to a ``TypeError`` in a future version. ``.union()`` should be used for the union set operation. (:issue:`9094`)
- Subtracting ``DatetimeIndex/PeriodIndex`` from another ``DatetimeIndex/PeriodIndex`` is being deprecated as a set-operation. This will be changed to an actual numeric subtraction yielding a ``TimeDeltaIndex`` in a future version. ``.difference()`` should be used for the differencing set operation. (:issue:`9094`)

.. _whatsnew_0160.prior_deprecations:

Expand Down
6 changes: 5 additions & 1 deletion pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Base and utility classes for tseries type pandas objects.
"""


import warnings
from datetime import datetime, time, timedelta

from pandas import compat
Expand Down Expand Up @@ -334,6 +334,8 @@ def __add__(self, other):
return other._add_delta(self)
raise TypeError("cannot add TimedeltaIndex and {typ}".format(typ=type(other)))
elif isinstance(other, Index):
warnings.warn("using '+' to provide set union with datetimelike Indexes is deprecated, "
"use .union()",FutureWarning)
return self.union(other)
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
return self._add_delta(other)
Expand All @@ -357,6 +359,8 @@ def __sub__(self, other):
raise TypeError("cannot subtract TimedeltaIndex and {typ}".format(typ=type(other)))
return self._add_delta(-other)
elif isinstance(other, Index):
warnings.warn("using '-' to provide set differences with datetimelike Indexes is deprecated, "
"use .difference()",FutureWarning)
return self.difference(other)
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
return self._add_delta(-other)
Expand Down
62 changes: 58 additions & 4 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,16 @@ def test_add_iadd(self):

for rng, other, expected in [(rng1, other1, expected1), (rng2, other2, expected2),
(rng3, other3, expected3)]:
result_add = rng + other
# GH9094
with tm.assert_produces_warning(FutureWarning):
result_add = rng + other
result_union = rng.union(other)

tm.assert_index_equal(result_add, expected)
tm.assert_index_equal(result_union, expected)
rng += other
# GH9094
with tm.assert_produces_warning(FutureWarning):
rng += other
tm.assert_index_equal(rng, expected)

# offset
Expand Down Expand Up @@ -593,6 +597,50 @@ def _check(result, expected):
expected = DatetimeIndex(['20121231','20130101','20130102'],tz='US/Eastern')
tm.assert_index_equal(result,expected)

def test_dti_dti_deprecated_ops(self):

# deprecated in 0.16.0 (GH9094)
# change to return subtraction -> TimeDeltaIndex in 0.17.0
# shoudl move to the appropriate sections above

dti = date_range('20130101',periods=3)
dti_tz = date_range('20130101',periods=3).tz_localize('US/Eastern')

with tm.assert_produces_warning(FutureWarning):
result = dti-dti
expected = Index([])
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
result = dti+dti
expected = dti
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
result = dti_tz-dti_tz
expected = Index([])
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
result = dti_tz+dti_tz
expected = dti_tz
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
result = dti_tz-dti
expected = dti_tz
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
result = dti-dti_tz
expected = dti
tm.assert_index_equal(result,expected)

with tm.assert_produces_warning(FutureWarning):
self.assertRaises(TypeError, lambda : dti_tz+dti)
with tm.assert_produces_warning(FutureWarning):
self.assertRaises(TypeError, lambda : dti+dti_tz)

def test_dti_tdi_numeric_ops(self):

# These are normally union/diff set-like ops
Expand Down Expand Up @@ -909,13 +957,19 @@ def test_add_iadd(self):
(rng5, other5, expected5), (rng6, other6, expected6),
(rng7, other7, expected7)]:

result_add = rng + other
# GH9094
with tm.assert_produces_warning(FutureWarning):
result_add = rng + other

result_union = rng.union(other)

tm.assert_index_equal(result_add, expected)
tm.assert_index_equal(result_union, expected)

# GH 6527
rng += other
# GH9094
with tm.assert_produces_warning(FutureWarning):
rng += other
tm.assert_index_equal(rng, expected)

# offset
Expand Down