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: Series.ptp() #21614

Merged
merged 9 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Deprecations

- :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`).
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
-

.. _whatsnew_0240.prior_deprecations:
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8875,13 +8875,20 @@ def _add_series_only_operations(cls):
def nanptp(values, axis=0, skipna=True):
nmax = nanops.nanmax(values, axis, skipna)
nmin = nanops.nanmin(values, axis, skipna)
warnings.warn("Method .ptp is deprecated and will be removed "
"in a future version. Use numpy.ptp instead.",
FutureWarning, stacklevel=4)
return nmax - nmin

cls.ptp = _make_stat_function(
cls, 'ptp', name, name2, axis_descr,
"""Returns the difference between the maximum value and the
"""
.. deprecated:: 0.24.0
Use numpy.ptp instead
Returns the difference between the maximum value and the
Copy link
Member

Choose a reason for hiding this comment

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

This needs a blank line above this one. Can you also put the deprecated part below the text?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks - Done - let me know if the deprecated note below main text needs any edits, have also added the line Use numpy.ptp instead again

minimum value in the object. This is the equivalent of the
``numpy.ndarray`` method ``ptp``.""",
``numpy.ndarray`` method ``ptp``.
""",
nanptp)

@classmethod
Expand Down
24 changes: 17 additions & 7 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,34 +1395,44 @@ def test_numpy_argmax_deprecated(self):
s, out=data)

def test_ptp(self):
# GH21614
N = 1000
arr = np.random.randn(N)
ser = Series(arr)
assert np.ptp(ser) == np.ptp(arr)

# GH11163
s = Series([3, 5, np.nan, -3, 10])
assert s.ptp() == 13
assert pd.isna(s.ptp(skipna=False))
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
Copy link
Member

Choose a reason for hiding this comment

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

Is the check_stacklevel=False needed?

Copy link
Contributor Author

@KalyanGokhale KalyanGokhale Jul 6, 2018

Choose a reason for hiding this comment

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

Yes - it seems to be required, not for this particular check(i.e. assert s.ptp() == 13), but for e.g. the following

tm.assert_series_equal(s.ptp(level=0), expected)

without it, this test was failing with AssertionError: Warning not set with correct stacklevel.

I also did trial-and-error with different stacklevels, without any success (though stacklevel=4 seems to be appropriate for where the warning is raised)

Any suggestions? Thanks

assert s.ptp() == 13
assert pd.isna(s.ptp(skipna=False))

mi = pd.MultiIndex.from_product([['a', 'b'], [1, 2, 3]])
s = pd.Series([1, np.nan, 7, 3, 5, np.nan], index=mi)

expected = pd.Series([6, 2], index=['a', 'b'], dtype=np.float64)
tm.assert_series_equal(s.ptp(level=0), expected)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
tm.assert_series_equal(s.ptp(level=0), expected)

expected = pd.Series([np.nan, np.nan], index=['a', 'b'])
tm.assert_series_equal(s.ptp(level=0, skipna=False), expected)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
tm.assert_series_equal(s.ptp(level=0, skipna=False), expected)

with pytest.raises(ValueError):
s.ptp(axis=1)
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
s.ptp(axis=1)

s = pd.Series(['a', 'b', 'c', 'd', 'e'])
with pytest.raises(TypeError):
s.ptp()
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
s.ptp()

with pytest.raises(NotImplementedError):
s.ptp(numeric_only=True)
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
s.ptp(numeric_only=True)

def test_empty_timeseries_redections_return_nat(self):
# covers #11245
Expand Down