diff --git a/doc/source/api.rst b/doc/source/api.rst index 9cf10d3f0780d..538965d0be7ad 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -708,6 +708,9 @@ Computations / Descriptive Stats :toctree: generated/ Panel.abs + Panel.clip + Panel.clip_lower + Panel.clip_upper Panel.count Panel.cummax Panel.cummin diff --git a/doc/source/release.rst b/doc/source/release.rst index 4f02fdbbfe97a..a23aa2fcebc12 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -260,6 +260,7 @@ See :ref:`Internal Refactoring` - Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue:`4605`), and adds ``rename`` with the same signature for ``Panel`` - Series (for index) / Panel (for items) now as attribute access to its elements (:issue:`1903`) +- Refactor ``clip`` methods to core/generic.py (:issue:`4798`) - Refactor of ``_get_numeric_data/_get_bool_data`` to core/generic.py, allowing Series/Panel functionaility - Refactor of Series arithmetic with time-like objects (datetime/timedelta/time etc.) into a separate, cleaned up wrapper class. (:issue:`4613`) diff --git a/doc/source/v0.13.0.txt b/doc/source/v0.13.0.txt index a38ff2fa6d457..3431d82219856 100644 --- a/doc/source/v0.13.0.txt +++ b/doc/source/v0.13.0.txt @@ -353,8 +353,9 @@ and behaviors. Series formerly subclassed directly from ``ndarray``. (:issue:`40 - Refactor ``Series.reindex`` to core/generic.py (:issue:`4604`, :issue:`4618`), allow ``method=`` in reindexing on a Series to work - ``Series.copy`` no longer accepts the ``order`` parameter and is now consistent with ``NDFrame`` copy -- Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue`4605`), and adds ``rename`` +- Refactor ``rename`` methods to core/generic.py; fixes ``Series.rename`` for (:issue:`4605`), and adds ``rename`` with the same signature for ``Panel`` +- Refactor ``clip`` methods to core/generic.py (:issue:`4798`) - Refactor of ``_get_numeric_data/_get_bool_data`` to core/generic.py, allowing Series/Panel functionaility - ``Series`` (for index) / ``Panel`` (for items) now allow attribute access to its elements (:issue:`1903`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 71d7f826781df..2b0e18c0c5524 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4396,47 +4396,6 @@ def f(arr): data = self._get_numeric_data() if numeric_only else self return data.apply(f, axis=axis) - - def clip(self, lower=None, upper=None): - """ - Trim values at input threshold(s) - - Parameters - ---------- - lower : float, default None - upper : float, default None - - Returns - ------- - clipped : DataFrame - """ - - # GH 2747 (arguments were reversed) - if lower is not None and upper is not None: - lower, upper = min(lower, upper), max(lower, upper) - - return self.apply(lambda x: x.clip(lower=lower, upper=upper)) - - def clip_upper(self, threshold): - """ - Trim values above threshold - - Returns - ------- - clipped : DataFrame - """ - return self.apply(lambda x: x.clip_upper(threshold)) - - def clip_lower(self, threshold): - """ - Trim values below threshold - - Returns - ------- - clipped : DataFrame - """ - return self.apply(lambda x: x.clip_lower(threshold)) - def rank(self, axis=0, numeric_only=None, method='average', na_option='keep', ascending=True): """ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2919790300bc3..d5c265dcf93a0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1920,6 +1920,68 @@ def f(x): return obj + def clip(self, lower=None, upper=None, out=None): + """ + Trim values at input threshold(s) + + Parameters + ---------- + lower : float, default None + upper : float, default None + + Returns + ------- + clipped : Series + """ + if out is not None: # pragma: no cover + raise Exception('out argument is not supported yet') + + # GH 2747 (arguments were reversed) + if lower is not None and upper is not None: + lower, upper = min(lower, upper), max(lower, upper) + + result = self + if lower is not None: + result = result.clip_lower(lower) + if upper is not None: + result = result.clip_upper(upper) + + return result + + def clip_upper(self, threshold): + """ + Return copy of input with values above given value truncated + + See also + -------- + clip + + Returns + ------- + clipped : same type as input + """ + if isnull(threshold): + raise ValueError("Cannot use an NA value as a clip threshold") + + return self.where((self <= threshold) | isnull(self), threshold) + + def clip_lower(self, threshold): + """ + Return copy of the input with values below given value truncated + + See also + -------- + clip + + Returns + ------- + clipped : same type as input + """ + if isnull(threshold): + raise ValueError("Cannot use an NA value as a clip threshold") + + return self.where((self >= threshold) | isnull(self), threshold) + def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index ef8c630a7bde8..3f93f210ad4cf 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2102,64 +2102,6 @@ def autocorr(self): """ return self.corr(self.shift(1)) - def clip(self, lower=None, upper=None, out=None): - """ - Trim values at input threshold(s) - - Parameters - ---------- - lower : float, default None - upper : float, default None - - Returns - ------- - clipped : Series - """ - if out is not None: # pragma: no cover - raise Exception('out argument is not supported yet') - - result = self - if lower is not None: - result = result.clip_lower(lower) - if upper is not None: - result = result.clip_upper(upper) - - return result - - def clip_upper(self, threshold): - """ - Return copy of series with values above given value truncated - - See also - -------- - clip - - Returns - ------- - clipped : Series - """ - if isnull(threshold): - raise ValueError("Cannot use an NA value as a clip threshold") - - return self.where((self <= threshold) | isnull(self), threshold) - - def clip_lower(self, threshold): - """ - Return copy of series with values below given value truncated - - See also - -------- - clip - - Returns - ------- - clipped : Series - """ - if isnull(threshold): - raise ValueError("Cannot use an NA value as a clip threshold") - - return self.where((self >= threshold) | isnull(self), threshold) - def dot(self, other): """ Matrix multiplication with DataFrame or inner-product with Series objects