Skip to content

Commit 6d15e4e

Browse files
committed
DEPR: deprecate DatetimeIndex - DatetimeIndex (subtraction) (GH9094)
this currently represents a set difference operation and should actually be DTI subtraction to yield a TimeDeltaIndex. Change in a future version.
1 parent a162728 commit 6d15e4e

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

doc/source/whatsnew/v0.16.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ Deprecations
385385
- The ``pandas.rpy`` interface is deprecated and will be removed in a future version.
386386
Similar functionaility can be accessed thru the `rpy2 <http://rpy.sourceforge.net/>`_ project (:issue:`9602`)
387387

388+
- 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`)
389+
- 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`)
388390

389391
.. _whatsnew_0160.prior_deprecations:
390392

pandas/tseries/base.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Base and utility classes for tseries type pandas objects.
33
"""
44

5-
5+
import warnings
66
from datetime import datetime, time, timedelta
77

88
from pandas import compat
@@ -334,6 +334,8 @@ def __add__(self, other):
334334
return other._add_delta(self)
335335
raise TypeError("cannot add TimedeltaIndex and {typ}".format(typ=type(other)))
336336
elif isinstance(other, Index):
337+
warnings.warn("using '+' to provide set union with datetimelike Indexes is deprecated, "
338+
"use .union()",FutureWarning)
337339
return self.union(other)
338340
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
339341
return self._add_delta(other)
@@ -357,6 +359,8 @@ def __sub__(self, other):
357359
raise TypeError("cannot subtract TimedeltaIndex and {typ}".format(typ=type(other)))
358360
return self._add_delta(-other)
359361
elif isinstance(other, Index):
362+
warnings.warn("using '-' to provide set differences with datetimelike Indexes is deprecated, "
363+
"use .difference()",FutureWarning)
360364
return self.difference(other)
361365
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
362366
return self._add_delta(-other)

pandas/tseries/tests/test_base.py

+58-4
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@ def test_add_iadd(self):
196196

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

202204
tm.assert_index_equal(result_add, expected)
203205
tm.assert_index_equal(result_union, expected)
204-
rng += other
206+
# GH9094
207+
with tm.assert_produces_warning(FutureWarning):
208+
rng += other
205209
tm.assert_index_equal(rng, expected)
206210

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

600+
def test_dti_dti_deprecated_ops(self):
601+
602+
# deprecated in 0.16.0 (GH9094)
603+
# change to return subtraction -> TimeDeltaIndex in 0.17.0
604+
# shoudl move to the appropriate sections above
605+
606+
dti = date_range('20130101',periods=3)
607+
dti_tz = date_range('20130101',periods=3).tz_localize('US/Eastern')
608+
609+
with tm.assert_produces_warning(FutureWarning):
610+
result = dti-dti
611+
expected = Index([])
612+
tm.assert_index_equal(result,expected)
613+
614+
with tm.assert_produces_warning(FutureWarning):
615+
result = dti+dti
616+
expected = dti
617+
tm.assert_index_equal(result,expected)
618+
619+
with tm.assert_produces_warning(FutureWarning):
620+
result = dti_tz-dti_tz
621+
expected = Index([])
622+
tm.assert_index_equal(result,expected)
623+
624+
with tm.assert_produces_warning(FutureWarning):
625+
result = dti_tz+dti_tz
626+
expected = dti_tz
627+
tm.assert_index_equal(result,expected)
628+
629+
with tm.assert_produces_warning(FutureWarning):
630+
result = dti_tz-dti
631+
expected = dti_tz
632+
tm.assert_index_equal(result,expected)
633+
634+
with tm.assert_produces_warning(FutureWarning):
635+
result = dti-dti_tz
636+
expected = dti
637+
tm.assert_index_equal(result,expected)
638+
639+
with tm.assert_produces_warning(FutureWarning):
640+
self.assertRaises(TypeError, lambda : dti_tz+dti)
641+
with tm.assert_produces_warning(FutureWarning):
642+
self.assertRaises(TypeError, lambda : dti+dti_tz)
643+
596644
def test_dti_tdi_numeric_ops(self):
597645

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

912-
result_add = rng + other
960+
# GH9094
961+
with tm.assert_produces_warning(FutureWarning):
962+
result_add = rng + other
963+
913964
result_union = rng.union(other)
914965

915966
tm.assert_index_equal(result_add, expected)
916967
tm.assert_index_equal(result_union, expected)
968+
917969
# GH 6527
918-
rng += other
970+
# GH9094
971+
with tm.assert_produces_warning(FutureWarning):
972+
rng += other
919973
tm.assert_index_equal(rng, expected)
920974

921975
# offset

0 commit comments

Comments
 (0)