Skip to content
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

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

Merged
merged 40 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ Deprecations
- The signature of :meth:`Series.to_csv` has been uniformed to that of doc:meth:`DataFrame.to_csv`: the name of the first argument is now 'path_or_buf', the order of subsequent arguments has changed, the 'header' argument now defaults to True. (:issue:`19715`)
- :meth:`Categorical.from_codes` has deprecated providing float values for the ``codes`` argument. (:issue:`21767`)
- :func:`pandas.read_table` is deprecated. Instead, use :func:`pandas.read_csv` passing ``sep='\t'`` if necessary (:issue:`21948`)
- Addition or subtraction of integers with :class:`Timestamp`, :class:`Period`, :class:`DatetimeIndex`, :class:`TimedeltaIndex`, :class:`PeriodIndex` is deprecated, will be removed in a future version (:issue:`21939`)
Copy link
Contributor

Choose a reason for hiding this comment

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

pls make a sub-section and show what is being deprecated and what is the alternative. this is actually a rather large change.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try to get to this tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jreback just added this section; wasn't sure on where in the doc to put it and the ipython/code-block syntax is a shot in the dark. Please advise.

- Addition or subtraction of integer-dtyped arrays with:class:`DatetimeIndex`, :class:`TimedeltaIndex`, :class:`PeriodIndex` is deprecated, will be removed in a future version (:issue:`21939`)
Copy link
Contributor

Choose a reason for hiding this comment

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

space after with


.. _whatsnew_0240.prior_deprecations:

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

from cpython cimport (
PyUnicode_Check,
Expand Down Expand Up @@ -1645,6 +1646,14 @@ cdef class _Period(object):
elif other is NaT:
return NaT
elif util.is_integer_object(other):
warnings.warn("Addition of integers to {cls} is "
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 call a function for this warning (not sure exatcly where), rather than repeating each time, prob need to pass in the level

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 sure what you have in mind. Just a one-liner containing this warning?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah a module level function just seems better than copy-pasting

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 do this

"deprecated, will be removed in version "
"0.26.0 (or 1.0, whichever comes first). "
"Instead of adding `n`, add "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)

ordinal = self.ordinal + other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif (PyDateTime_Check(other) or
Expand All @@ -1671,6 +1680,14 @@ 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 version "
"0.26.0 (or 1.0, whichever comes first). "
"Instead of subtracting `n`, subtract "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)

ordinal = self.ordinal - other * self.freq.n
return Period(ordinal=ordinal, freq=self.freq)
elif is_period_object(other):
Expand Down
12 changes: 11 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,24 @@ cdef class _Timestamp(datetime):
return np.datetime64(self.value, 'ns')

def __add__(self, other):
cdef int64_t other_int, nanos
cdef:
int64_t other_int, nanos

if is_timedelta64_object(other):
other_int = other.astype('timedelta64[ns]').view('i8')
return Timestamp(self.value + other_int,
tz=self.tzinfo, freq=self.freq)

elif is_integer_object(other):
if self.freq is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

is it necessary for freq to be none here? (we don't have this check elsewhere)

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be not-None, good catch.

Copy link
Contributor

Choose a reason for hiding this comment

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

does this need to be changed?

warnings.warn("Addition of integers to {cls} is "
"deprecated, will be removed in version "
"0.26.0 (or 1.0, whichever comes first). "
"Instead of adding `n`, add "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)

if self is NaT:
# to be compat with Period
return NaT
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,15 @@ def _addsub_int_array(self, other, op):
result : same class as self
"""
assert op in [operator.add, operator.sub]

if self.freq is not None:
# case with freq of None will raise
warnings.warn("Addition/subtraction of integer array from {cls} "
"is deprecated, will be removed in version "
"0.26.0 (or 1.0, whichever comes first). "
jreback marked this conversation as resolved.
Show resolved Hide resolved
"Instead of adding `arr`, add `arr * self.freq`"
.format(cls=type(self).__name__), FutureWarning)

if is_period_dtype(self):
# easy case for PeriodIndex
if op is operator.sub:
Expand Down Expand Up @@ -584,6 +593,14 @@ 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
warnings.warn("Addition of integers to {cls} is "
"deprecated, will be removed in version "
"0.26.0 (or 1.0, whichever comes first). "
"Instead of adding `n`, add `n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)
result = self.shift(other)

# array-like others
Expand Down Expand Up @@ -636,6 +653,15 @@ 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
warnings.warn("Subtraction of integers from {cls} is "
"deprecated, will be removed in version "
"0.26.0 (or 1.0, whichever comes first). "
"Instead of subtracting `n`, subtract "
"`n * self.freq`"
.format(cls=type(self).__name__),
FutureWarning)
result = self.shift(-other)
elif isinstance(other, Period):
result = self._sub_period(other)
Expand Down