Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Moved deprecation docstring, fixed tests
PTP
  • Loading branch information
KalyanGokhale committed Jun 26, 2018
commit 0028c7c98ff89599733f18a16adc4e2133db5ded
12 changes: 6 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8871,10 +8871,6 @@ def _add_series_only_operations(cls):
axis_descr, name, name2 = _doc_parms(cls)

def nanptp(values, axis=0, skipna=True):
"""
.. deprecated:: 0.24.0
Use numpy.ptp instead
"""
nmax = nanops.nanmax(values, axis, skipna)
nmin = nanops.nanmin(values, axis, skipna)
warnings.warn("Method .ptp is deprecated and will be removed "
Expand All @@ -8884,9 +8880,13 @@ def nanptp(values, axis=0, skipna=True):

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
18 changes: 10 additions & 8 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,41 +1376,43 @@ 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])
# GH21614
# Suppressed deprecation warnings in this original test for ptp
with tm.assert_produces_warning(FutureWarning):
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)
with tm.assert_produces_warning(FutureWarning):
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'])
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
tm.assert_series_equal(s.ptp(level=0, skipna=False), expected)

with pytest.raises(ValueError):
with tm.assert_produces_warning(FutureWarning):
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):
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
s.ptp()

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

def test_empty_timeseries_redections_return_nat(self):
Expand Down