Skip to content

DEPR: deprecate integer add/sub with DTI/TDI/PI/Timestamp/Period #22535

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 40 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
145eb6a
DEPR: deprecate integer add/sub with DTI/TDI/PI/Timestamp/Period
jbrockmendel Aug 29, 2018
d5c5f28
say future version instead of a specific version number
jbrockmendel Aug 29, 2018
3fb29bc
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Aug 31, 2018
81c9eab
typo fixup
jbrockmendel Aug 31, 2018
26c9966
Catch FutureWarning/PerformanceWarning in tests
jbrockmendel Sep 1, 2018
11959b1
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Sep 5, 2018
8952cb5
set stacklevel
jbrockmendel Sep 5, 2018
7c79364
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Sep 7, 2018
d0fa41a
Avoid warnings in plotting._converter
jbrockmendel Sep 7, 2018
c23346b
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Sep 7, 2018
f6daf34
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Sep 9, 2018
b7e3dcf
Fixup flipped condition
jbrockmendel Sep 9, 2018
2a829d3
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Sep 28, 2018
f24643c
whatsnew section on deprecation
jbrockmendel Sep 28, 2018
6fea2a8
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 4, 2018
b469bef
just input in whatsnew executable block
jbrockmendel Oct 4, 2018
20d58fa
Catch warnings in tests
jbrockmendel Oct 4, 2018
984bc7e
TST: catch warnings for strict test run
jbrockmendel Oct 4, 2018
841718c
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 5, 2018
7e7a348
Catch warnings
jbrockmendel Oct 5, 2018
17f6be0
Avoid warnings
jbrockmendel Oct 5, 2018
29cca46
catch more warnings
jbrockmendel Oct 5, 2018
b0a222a
avoid warning
jbrockmendel Oct 5, 2018
14b9eaf
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 15, 2018
55dc265
Avoid need to catch deprecation warnings
jbrockmendel Oct 15, 2018
9443df9
stop using deprecated usage
jbrockmendel Oct 15, 2018
d46452d
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 17, 2018
af31b0c
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 18, 2018
0bebbe0
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 28, 2018
c7dd7ce
update to user PeriodArray private method
jbrockmendel Oct 29, 2018
ca55b4a
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 29, 2018
394a3b8
suppress
jbrockmendel Oct 29, 2018
3884bb4
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 29, 2018
a15d9e4
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 30, 2018
5256671
function for warning
jbrockmendel Oct 30, 2018
d8865d7
ignore stacklevel
jbrockmendel Oct 30, 2018
da72660
Merge branch 'master' of https://github.com/pandas-dev/pandas into in…
jbrockmendel Oct 31, 2018
fcd65b3
Show deprecation warning in whatsnew
jbrockmendel Oct 31, 2018
6ded8b1
move location of addsub_int_array warning
jbrockmendel Oct 31, 2018
4a7b589
rename deprecation warning function
jbrockmendel Oct 31, 2018
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
16 changes: 8 additions & 8 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -990,17 +990,17 @@ Current Behavior:

.. ipython:: python

Copy link
Contributor

Choose a reason for hiding this comment

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

these should be executable statements, e.g. just show the input, the output is run

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated. One day I'll get the hang of this doc system...

Copy link
Contributor

Choose a reason for hiding this comment

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

if you can update these

Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the ipython line numbers

In [3]: per = pd.Period('2016Q1')
In [4]: per + 3 * per.freq
per = pd.Period('2016Q1')
per + 3 * per.freq

Copy link
Contributor

Choose a reason for hiding this comment

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

you might want to show the deprecation warning itself on 1 case here (you will need to add :okwarning: though)

In [5]: ts = pd.Timestamp('1994-05-06 12:15:16', freq=pd.offsets.Hour())
In [6]: ts + 2 * ts.freq
ts = pd.Timestamp('1994-05-06 12:15:16', freq=pd.offsets.Hour())
ts + 2 * ts.freq

In [7]: tdi = pd.timedelta_range('1D', periods=2)
In [8]: tdi - np.array([2 * tdi.freq, 1 * tdi.freq])
tdi = pd.timedelta_range('1D', periods=2)
tdi - np.array([2 * tdi.freq, 1 * tdi.freq])

In [9]: dti = pd.date_range('2001-01-01', periods=2, freq='7D')
In [10]: dti + pd.Index([1 * dti.freq, 2 * dti.freq])
dti = pd.date_range('2001-01-01', periods=2, freq='7D')
dti + pd.Index([1 * dti.freq, 2 * dti.freq])

.. _whatsnew_0240.prior_deprecations:

Expand Down
17 changes: 3 additions & 14 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from datetime import datetime, date
import warnings

from cpython cimport (
PyUnicode_Check,
Expand Down Expand Up @@ -34,7 +33,7 @@ cdef extern from "src/datetime/np_datetime.h":
cimport util
from util cimport is_period_object, is_string_object

from timestamps import Timestamp
from timestamps import Timestamp, int_op_deprecated
from timezones cimport is_utc, is_tzlocal, get_dst_info
from timedeltas import Timedelta
from timedeltas cimport delta_to_nanoseconds
Expand Down Expand Up @@ -1646,12 +1645,7 @@ cdef class _Period(object):
elif other is NaT:
return NaT
elif util.is_integer_object(other):
warnings.warn("Addition of integers to {cls} is "
"deprecated, will be removed in a future "
"version. Instead of adding `n`, add "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)
int_op_deprecated(self)

ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
Expand Down Expand Up @@ -1679,12 +1673,7 @@ cdef class _Period(object):
neg_other = -other
return self + neg_other
elif util.is_integer_object(other):
warnings.warn("Subtraction of integers from {cls} is "
"deprecated, will be removed in a future "
"version. Instead of subtracting `n`, subtract "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)
int_op_deprecated(self)

ordinal = self.ordinal - other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
Expand Down
19 changes: 12 additions & 7 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,19 @@ from timezones cimport (
_zero_time = datetime_time(0, 0)
_no_input = object()


# ----------------------------------------------------------------------

def int_op_deprecated(obj):
Copy link
Contributor

Choose a reason for hiding this comment

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

ok this looks good. can you rename to
``maybe_integer_op_deprecated`

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

# GH#22535 add/sub of integers and int-arrays is deprecated
if obj.freq is not None:
warnings.warn("Addition/subtraction of integers and integer-arrays "
"to {cls} is deprecated, will be removed in a future "
"version. Instead of adding/subtracting `n`, use "
"`n * self.freq`"
.format(cls=type(obj).__name__),
FutureWarning)


cdef inline object create_timestamp_from_ts(int64_t value,
npy_datetimestruct dts,
Expand Down Expand Up @@ -324,13 +335,7 @@ cdef class _Timestamp(datetime):
tz=self.tzinfo, freq=self.freq)

elif is_integer_object(other):
if self.freq is not None:
warnings.warn("Addition of integers to {cls} is "
"deprecated, will be removed in a future "
"version. Instead of adding `n`, add "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)
int_op_deprecated(self)

if self is NaT:
# to be compat with Period
Expand Down
37 changes: 5 additions & 32 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas._libs import lib, iNaT, NaT
from pandas._libs.tslibs import timezones
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds, Timedelta
from pandas._libs.tslibs.timestamps import int_op_deprecated
from pandas._libs.tslibs.period import (
Period, DIFFERENT_FREQ_INDEX, IncompatibleFrequency)

Expand Down Expand Up @@ -494,16 +495,8 @@ def _addsub_int_array(self, other, op, suppress=False):
assert not is_period_dtype(self)
assert op in [operator.add, operator.sub]

if self.freq is not None and not suppress:
# case with freq of None will raise
# we need to use a different stacklevel for Index vs Array
lvl = 3 + 1 * isinstance(self, ABCIndexClass)
warnings.warn("Addition/subtraction of integer array from {cls} "
"is deprecated, will be removed in a future "
"version. Instead of adding `arr`, "
"add `arr * self.freq`"
.format(cls=type(self).__name__),
FutureWarning, stacklevel=lvl)
if not suppress:
int_op_deprecated(self)

if self.freq is None:
# GH#19123
Expand Down Expand Up @@ -647,17 +640,7 @@ def __add__(self, other):
elif lib.is_integer(other):
# This check must come after the check for np.timedelta64
# as is_integer returns True for these
if self.freq is not None:
# case with freq of None will raise
# we need to use a different stacklevel for Index vs Array
lvl = 2 + 1 * isinstance(self, ABCIndexClass)
warnings.warn("Addition of integers to {cls} is "
"deprecated, will be removed in a future "
"version. Instead of adding `n`, "
"add `n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning, stacklevel=lvl)

int_op_deprecated(self)
result = self._time_shift(other)

# array-like others
Expand Down Expand Up @@ -716,17 +699,7 @@ def __sub__(self, other):
elif lib.is_integer(other):
# This check must come after the check for np.timedelta64
# as is_integer returns True for these
if self.freq is not None:
# case with freq of None will raise
# we need to use a different stacklevel for Index vs Array
lvl = 2 + 1 * isinstance(self, ABCIndexClass)
warnings.warn("Subtraction of integers from {cls} is "
"deprecated, will be removed in a future "
"version. Instead of subtracting `n`, "
"subtract `n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning, stacklevel=lvl)

int_op_deprecated(self)
result = self._time_shift(-other)

elif isinstance(other, Period):
Expand Down
9 changes: 2 additions & 7 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from datetime import timedelta
import operator
import warnings

import numpy as np

Expand All @@ -14,6 +13,7 @@
)
from pandas._libs.tslibs import period as libperiod
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds, Timedelta
from pandas._libs.tslibs.timestamps import int_op_deprecated
from pandas._libs.tslibs.fields import isleapyear_arr
from pandas.util._decorators import cache_readonly, Appender
from pandas.util._validators import validate_fillna_kwargs
Expand Down Expand Up @@ -726,12 +726,7 @@ def _addsub_int_array(
# type: (...) -> PeriodArray

if not suppress:
warnings.warn("Addition/subtraction of integer array from {cls} "
"is deprecated, will be removed in a future "
"version. Instead of adding `arr`, "
"add `arr * self.freq`"
.format(cls=type(self).__name__),
FutureWarning, stacklevel=3)
int_op_deprecated(self)

assert op in [operator.add, operator.sub]
if op is operator.sub:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def test_dti_shift_freqs(self):
def test_dti_shift_int(self):
rng = date_range('1/1/2000', periods=20)

with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng + 5

expected = rng.shift(5)
tm.assert_index_equal(result, expected)

with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng - 5

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/timedeltas/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_ufunc_coercions(self):
def test_tdi_add_int(self, one):
# Variants of `one` for #19012
rng = timedelta_range('1 days 09:00:00', freq='H', periods=10)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng + one
expected = timedelta_range('1 days 10:00:00', freq='H', periods=10)
Expand All @@ -146,7 +146,7 @@ def test_tdi_iadd_int(self, one):

def test_tdi_sub_int(self, one):
rng = timedelta_range('1 days 09:00:00', freq='H', periods=10)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng - one
expected = timedelta_range('1 days 08:00:00', freq='H', periods=10)
Expand All @@ -169,7 +169,7 @@ def test_tdi_add_integer_array(self, box):
rng = timedelta_range('1 days 09:00:00', freq='H', periods=3)
other = box([4, 3, 2])
expected = TimedeltaIndex(['1 day 13:00:00'] * 3)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng + other
tm.assert_index_equal(result, expected)
Expand All @@ -185,7 +185,7 @@ def test_tdi_sub_integer_array(self, box):
rng = timedelta_range('9H', freq='H', periods=3)
other = box([4, 3, 2])
expected = TimedeltaIndex(['5H', '7H', '9H'])
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#22535
result = rng - other
tm.assert_index_equal(result, expected)
Expand Down