Skip to content

Commit 28bfb14

Browse files
committed
Merge pull request #11172 from ajcr/ptp-handle-nan
ENH: make Series.ptp ignore NaN values (GH11163)
2 parents 13e45c0 + ae29378 commit 28bfb14

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ Other enhancements
527527

528528
- ``pd.read_csv`` is now able to infer compression type for files read from AWS S3 storage (:issue:`11070`, :issue:`11074`).
529529

530+
530531
.. _whatsnew_0170.api:
531532

532533
.. _whatsnew_0170.api_breaking:

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ API changes
6060
- ``Series.sort_index()`` now correctly handles the ``inplace`` option (:issue:`11402`)
6161

6262
- ``DataFrame.itertuples()`` now returns ``namedtuple`` objects, when possible. (:issue:`11269`)
63+
- ``Series.ptp`` will now ignore missing values by default (:issue:`11163`)
6364

6465
.. _whatsnew_0171.deprecations:
6566

pandas/core/generic.py

+11
Original file line numberDiff line numberDiff line change
@@ -4701,6 +4701,17 @@ def stat_func(self, axis=None, skipna=None, level=None,
47014701
want the *index* of the minimum, use ``idxmin``. This is the
47024702
equivalent of the ``numpy.ndarray`` method ``argmin``.""", nanops.nanmin)
47034703

4704+
if cls.__name__ == 'Series':
4705+
def nanptp(values, axis=0, skipna=True):
4706+
nmax = nanops.nanmax(values, axis, skipna)
4707+
nmin = nanops.nanmin(values, axis, skipna)
4708+
return nmax - nmin
4709+
4710+
cls.ptp = _make_stat_function('ptp', """
4711+
Returns the difference between the maximum value and the minimum
4712+
value in the object. This is the equivalent of the ``numpy.ndarray``
4713+
method ``ptp``.""", nanptp)
4714+
47044715
def _make_logical_function(name, desc, f):
47054716

47064717
@Substitution(outname=name, desc=desc)

pandas/core/series.py

-3
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,6 @@ def multi(values, qs):
12771277

12781278
return self._maybe_box(lambda values: multi(values, q), dropna=True)
12791279

1280-
def ptp(self, axis=None, out=None):
1281-
return _values_from_object(self).ptp(axis, out)
1282-
12831280
def corr(self, other, method='pearson',
12841281
min_periods=None):
12851282
"""

pandas/tests/test_series.py

+25
Original file line numberDiff line numberDiff line change
@@ -5949,6 +5949,31 @@ def test_ptp(self):
59495949
ser = Series(arr)
59505950
self.assertEqual(np.ptp(ser), np.ptp(arr))
59515951

5952+
# GH11163
5953+
s = Series([3, 5, np.nan, -3, 10])
5954+
self.assertEqual(s.ptp(), 13)
5955+
self.assertTrue(pd.isnull(s.ptp(skipna=False)))
5956+
5957+
mi = pd.MultiIndex.from_product([['a','b'], [1,2,3]])
5958+
s = pd.Series([1, np.nan, 7, 3, 5, np.nan], index=mi)
5959+
5960+
expected = pd.Series([6, 2], index=['a', 'b'], dtype=np.float64)
5961+
self.assert_series_equal(s.ptp(level=0), expected)
5962+
5963+
expected = pd.Series([np.nan, np.nan], index=['a', 'b'])
5964+
self.assert_series_equal(s.ptp(level=0, skipna=False), expected)
5965+
5966+
with self.assertRaises(ValueError):
5967+
s.ptp(axis=1)
5968+
5969+
s = pd.Series(['a', 'b', 'c', 'd', 'e'])
5970+
with self.assertRaises(TypeError):
5971+
s.ptp()
5972+
5973+
with self.assertRaises(NotImplementedError):
5974+
s.ptp(numeric_only=True)
5975+
5976+
59525977
def test_asof(self):
59535978
# array or list or dates
59545979
N = 50

0 commit comments

Comments
 (0)