Skip to content

DEPR update dedeprecation message for np.ptp #28665 #6581 #30401

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

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
69 changes: 36 additions & 33 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10184,39 +10184,42 @@ def mad(self, axis=None, skipna=None, level=None):
_min_examples,
)

@classmethod
def _add_series_only_operations(cls):
"""
Add the series only operations to the cls; evaluate the doc
strings again.
"""

axis_descr, name, name2 = _doc_parms(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,
"""Return the difference between the min and max value.
\n.. deprecated:: 0.24.0 Use numpy.ptp instead
\nReturn the difference between the maximum value and the
minimum value in the object. This is the equivalent of the
``numpy.ndarray`` method ``ptp``.""",
nanptp,
)
# @classmethod
Copy link
Member

Choose a reason for hiding this comment

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

delete all of this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

# def _add_series_only_operations(cls):
# """
# Add the series only operations to the cls; evaluate the doc
# strings again.
# """

# axis_descr, name, name2 = _doc_parms(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."
# "if you are already using numpy.ptp and still getting this message,"
# "please call to_numpy() to avoid this message in future calls."
# "For example: np.ptp(pd.Series([1, 2, 3]).to_numpy())",
# FutureWarning,
# stacklevel=4,
# )
# return nmax - nmin

# cls.ptp = _make_stat_function(
# cls,
# "ptp",
# name,
# name2,
# axis_descr,
# """Return the difference between the min and max value.
# \n.. deprecated:: 0.24.0 Use numpy.ptp instead
# \nReturn the difference between the maximum value and the
# minimum value in the object. This is the equivalent of the
# ``numpy.ndarray`` method ``ptp``.""",
# nanptp,
# )

@classmethod
def _add_series_or_dataframe_operations(cls):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4390,7 +4390,7 @@ def to_period(self, freq=None, copy=True):
["index"], docs={"index": "The index (axis labels) of the Series."},
)
Series._add_numeric_operations()
Series._add_series_only_operations()
# Series._add_series_only_operations()
Copy link
Member

Choose a reason for hiding this comment

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

delete. basically never leave commented-out code in a PR

Copy link
Contributor Author

@hasnain2808 hasnain2808 Dec 23, 2019

Choose a reason for hiding this comment

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

actually made this commit to get some help on getting this future warning

Copy link
Contributor Author

@hasnain2808 hasnain2808 Dec 23, 2019

Choose a reason for hiding this comment

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

so I removed code and tests for ptp
remove the part where series only functions are added
removed the entire series only function because ptp was the only series only function
deleted tests for pandas ptp function
but the test written for np.ptp gives me a future warning

`
____________________________________________________________ TestSeriesAnalytics.test_ptp ____________________________________________________________

self = <pandas.tests.series.test_analytics.TestSeriesAnalytics object at 0x7f666e0267b8>

def test_ptp(self):
    # GH21614
    N = 1000
    arr = np.random.randn(N)
    ser = Series(arr)
    with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
      assert np.ptp(ser) == np.ptp(arr)

pandas/tests/series/test_analytics.py:861:

self = <contextlib._GeneratorContextManager object at 0x7f666e03ad30>, type = None, value = None, traceback = None

def __exit__(self, type, value, traceback):
    if type is None:
        try:
          next(self.gen)

E AssertionError: Did not see expected warning of class 'FutureWarning'

/usr/lib/python3.6/contextlib.py:88: AssertionError
================================================ 1 failed, 163 passed, 1 skipped, 3 xfailed in 1.96s =================================================
`

Copy link
Member

Choose a reason for hiding this comment

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

Remove the assert_produces_warning, since it shouldn't anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it works but can someone please explain to me the reason behind it so that I learn

Series._add_series_or_dataframe_operations()

# Add arithmetic!
Expand Down
62 changes: 31 additions & 31 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,37 +861,37 @@ def test_ptp(self):
assert np.ptp(ser) == np.ptp(arr)

# GH11163
s = Series([3, 5, np.nan, -3, 10])
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
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, 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, check_stacklevel=False):
tm.assert_series_equal(s.ptp(level=0, skipna=False), expected)

msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
s.ptp(axis=1)

s = pd.Series(["a", "b", "c", "d", "e"])
msg = r"unsupported operand type\(s\) for -: 'str' and 'str'"
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
s.ptp()

msg = r"Series\.ptp does not implement numeric_only\."
with pytest.raises(NotImplementedError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
s.ptp(numeric_only=True)
# s = Series([3, 5, np.nan, -3, 10])
# with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# 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, 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, check_stacklevel=False):
# tm.assert_series_equal(s.ptp(level=0, skipna=False), expected)

# msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
# with pytest.raises(ValueError, match=msg):
# with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# s.ptp(axis=1)

# s = pd.Series(["a", "b", "c", "d", "e"])
# msg = r"unsupported operand type\(s\) for -: 'str' and 'str'"
# with pytest.raises(TypeError, match=msg):
# with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# s.ptp()

# msg = r"Series\.ptp does not implement numeric_only\."
# with pytest.raises(NotImplementedError, match=msg):
# with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# s.ptp(numeric_only=True)

def test_repeat(self):
s = Series(np.random.randn(3), index=["a", "b", "c"])
Expand Down