Skip to content

Commit 1b99813

Browse files
committed
CLN: move clip to core/generic (adds to Panel as well), related to (GH2747)
1 parent 1434776 commit 1b99813

File tree

6 files changed

+68
-100
lines changed

6 files changed

+68
-100
lines changed

doc/source/api.rst

+3
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ Computations / Descriptive Stats
708708
:toctree: generated/
709709

710710
Panel.abs
711+
Panel.clip
712+
Panel.clip_lower
713+
Panel.clip_upper
711714
Panel.count
712715
Panel.cummax
713716
Panel.cummin

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
260260
- Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue:`4605`), and adds ``rename``
261261
with the same signature for ``Panel``
262262
- Series (for index) / Panel (for items) now as attribute access to its elements (:issue:`1903`)
263+
- Refactor ``clip`` methods to core/generic.py (:issue:`4798`)
263264
- Refactor of ``_get_numeric_data/_get_bool_data`` to core/generic.py, allowing Series/Panel functionaility
264265
- Refactor of Series arithmetic with time-like objects (datetime/timedelta/time
265266
etc.) into a separate, cleaned up wrapper class. (:issue:`4613`)

doc/source/v0.13.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,9 @@ and behaviors. Series formerly subclassed directly from ``ndarray``. (:issue:`40
353353
- Refactor ``Series.reindex`` to core/generic.py (:issue:`4604`, :issue:`4618`), allow ``method=`` in reindexing
354354
on a Series to work
355355
- ``Series.copy`` no longer accepts the ``order`` parameter and is now consistent with ``NDFrame`` copy
356-
- Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue`4605`), and adds ``rename``
356+
- Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue:`4605`), and adds ``rename``
357357
with the same signature for ``Panel``
358+
- Refactor ``clip`` methods to core/generic.py (:issue:`4798`)
358359
- Refactor of ``_get_numeric_data/_get_bool_data`` to core/generic.py, allowing Series/Panel functionaility
359360
- ``Series`` (for index) / ``Panel`` (for items) now allow attribute access to its elements (:issue:`1903`)
360361

pandas/core/frame.py

-41
Original file line numberDiff line numberDiff line change
@@ -4396,47 +4396,6 @@ def f(arr):
43964396

43974397
data = self._get_numeric_data() if numeric_only else self
43984398
return data.apply(f, axis=axis)
4399-
4400-
def clip(self, lower=None, upper=None):
4401-
"""
4402-
Trim values at input threshold(s)
4403-
4404-
Parameters
4405-
----------
4406-
lower : float, default None
4407-
upper : float, default None
4408-
4409-
Returns
4410-
-------
4411-
clipped : DataFrame
4412-
"""
4413-
4414-
# GH 2747 (arguments were reversed)
4415-
if lower is not None and upper is not None:
4416-
lower, upper = min(lower, upper), max(lower, upper)
4417-
4418-
return self.apply(lambda x: x.clip(lower=lower, upper=upper))
4419-
4420-
def clip_upper(self, threshold):
4421-
"""
4422-
Trim values above threshold
4423-
4424-
Returns
4425-
-------
4426-
clipped : DataFrame
4427-
"""
4428-
return self.apply(lambda x: x.clip_upper(threshold))
4429-
4430-
def clip_lower(self, threshold):
4431-
"""
4432-
Trim values below threshold
4433-
4434-
Returns
4435-
-------
4436-
clipped : DataFrame
4437-
"""
4438-
return self.apply(lambda x: x.clip_lower(threshold))
4439-
44404399
def rank(self, axis=0, numeric_only=None, method='average',
44414400
na_option='keep', ascending=True):
44424401
"""

pandas/core/generic.py

+62
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,68 @@ def f(x):
19201920

19211921
return obj
19221922

1923+
def clip(self, lower=None, upper=None, out=None):
1924+
"""
1925+
Trim values at input threshold(s)
1926+
1927+
Parameters
1928+
----------
1929+
lower : float, default None
1930+
upper : float, default None
1931+
1932+
Returns
1933+
-------
1934+
clipped : Series
1935+
"""
1936+
if out is not None: # pragma: no cover
1937+
raise Exception('out argument is not supported yet')
1938+
1939+
# GH 2747 (arguments were reversed)
1940+
if lower is not None and upper is not None:
1941+
lower, upper = min(lower, upper), max(lower, upper)
1942+
1943+
result = self
1944+
if lower is not None:
1945+
result = result.clip_lower(lower)
1946+
if upper is not None:
1947+
result = result.clip_upper(upper)
1948+
1949+
return result
1950+
1951+
def clip_upper(self, threshold):
1952+
"""
1953+
Return copy of input with values above given value truncated
1954+
1955+
See also
1956+
--------
1957+
clip
1958+
1959+
Returns
1960+
-------
1961+
clipped : same type as input
1962+
"""
1963+
if isnull(threshold):
1964+
raise ValueError("Cannot use an NA value as a clip threshold")
1965+
1966+
return self.where((self <= threshold) | isnull(self), threshold)
1967+
1968+
def clip_lower(self, threshold):
1969+
"""
1970+
Return copy of the input with values below given value truncated
1971+
1972+
See also
1973+
--------
1974+
clip
1975+
1976+
Returns
1977+
-------
1978+
clipped : same type as input
1979+
"""
1980+
if isnull(threshold):
1981+
raise ValueError("Cannot use an NA value as a clip threshold")
1982+
1983+
return self.where((self >= threshold) | isnull(self), threshold)
1984+
19231985
def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
19241986
group_keys=True, squeeze=False):
19251987
"""

pandas/core/series.py

-58
Original file line numberDiff line numberDiff line change
@@ -2102,64 +2102,6 @@ def autocorr(self):
21022102
"""
21032103
return self.corr(self.shift(1))
21042104

2105-
def clip(self, lower=None, upper=None, out=None):
2106-
"""
2107-
Trim values at input threshold(s)
2108-
2109-
Parameters
2110-
----------
2111-
lower : float, default None
2112-
upper : float, default None
2113-
2114-
Returns
2115-
-------
2116-
clipped : Series
2117-
"""
2118-
if out is not None: # pragma: no cover
2119-
raise Exception('out argument is not supported yet')
2120-
2121-
result = self
2122-
if lower is not None:
2123-
result = result.clip_lower(lower)
2124-
if upper is not None:
2125-
result = result.clip_upper(upper)
2126-
2127-
return result
2128-
2129-
def clip_upper(self, threshold):
2130-
"""
2131-
Return copy of series with values above given value truncated
2132-
2133-
See also
2134-
--------
2135-
clip
2136-
2137-
Returns
2138-
-------
2139-
clipped : Series
2140-
"""
2141-
if isnull(threshold):
2142-
raise ValueError("Cannot use an NA value as a clip threshold")
2143-
2144-
return self.where((self <= threshold) | isnull(self), threshold)
2145-
2146-
def clip_lower(self, threshold):
2147-
"""
2148-
Return copy of series with values below given value truncated
2149-
2150-
See also
2151-
--------
2152-
clip
2153-
2154-
Returns
2155-
-------
2156-
clipped : Series
2157-
"""
2158-
if isnull(threshold):
2159-
raise ValueError("Cannot use an NA value as a clip threshold")
2160-
2161-
return self.where((self >= threshold) | isnull(self), threshold)
2162-
21632105
def dot(self, other):
21642106
"""
21652107
Matrix multiplication with DataFrame or inner-product with Series objects

0 commit comments

Comments
 (0)