From 179de43ca3be63f7937a925f2c4dbfad87177583 Mon Sep 17 00:00:00 2001 From: jreback Date: Thu, 24 Jan 2013 12:54:50 -0500 Subject: [PATCH 1/2] BUG: DataFrame.clip arguments were inconsistent to numpy and Series.clip, closes GH #2747 --- RELEASE.rst | 6 ++++++ pandas/core/frame.py | 2 +- pandas/tests/test_frame.py | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/RELEASE.rst b/RELEASE.rst index 981fa5bed257d..7767129de084a 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -22,6 +22,12 @@ Where to get it * Binary installers on PyPI: http://pypi.python.org/pypi/pandas * Documentation: http://pandas.pydata.org +**API Changes** + + - arguments to DataFrame.clip were inconsistent to numpy and Series clipping (GH2747_) + +.. _GH2747: https://github.com/pydata/pandas/issues/2747 + pandas 0.10.1 ============= diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 301ea9d28d001..cdb3f22fb7d87 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5029,7 +5029,7 @@ def f(arr): data = self._get_numeric_data() if numeric_only else self return data.apply(f, axis=axis) - def clip(self, upper=None, lower=None): + def clip(self, lower=None, upper=None): """ Trim values at input threshold(s) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 09747ba3f09f0..2318d25fa0f37 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6470,6 +6470,21 @@ def test_clip(self): double = self.frame.clip(upper=median, lower=median) self.assert_(not (double.values != median).any()) + def test_dataframe_clip(self): + + lb = -1 + ub = 1 + + # GH #2747 + df = DataFrame(np.random.randn(1000,2)) + lb_mask = df.values <= lb + ub_mask = df.values >= ub + mask = ~lb_mask & ~ub_mask + clipped_df = df.clip(lb, ub) + self.assert_((clipped_df.values[lb_mask] == lb).all() == True) + self.assert_((clipped_df.values[ub_mask] == ub).all() == True) + self.assert_((clipped_df.values[mask] == df.values[mask]).all() == True) + def test_get_X_columns(self): # numeric and object columns From 8b77ace96423f18ace2e9a4dd961956e8c156974 Mon Sep 17 00:00:00 2001 From: jreback Date: Fri, 8 Feb 2013 10:19:38 -0500 Subject: [PATCH 2/2] BUG: provide backwards compatibility to DataFrame.clip if arguments are reversed --- pandas/core/frame.py | 5 +++++ pandas/tests/test_frame.py | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index cdb3f22fb7d87..d625fb87e3fd5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5042,6 +5042,11 @@ def clip(self, lower=None, upper=None): ------- 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): diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 2318d25fa0f37..d55e7a7b65fe3 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6472,18 +6472,19 @@ def test_clip(self): def test_dataframe_clip(self): - lb = -1 - ub = 1 - # GH #2747 df = DataFrame(np.random.randn(1000,2)) - lb_mask = df.values <= lb - ub_mask = df.values >= ub - mask = ~lb_mask & ~ub_mask - clipped_df = df.clip(lb, ub) - self.assert_((clipped_df.values[lb_mask] == lb).all() == True) - self.assert_((clipped_df.values[ub_mask] == ub).all() == True) - self.assert_((clipped_df.values[mask] == df.values[mask]).all() == True) + + for lb, ub in [(-1,1),(1,-1)]: + clipped_df = df.clip(lb, ub) + + lb, ub = min(lb,ub), max(ub,lb) + lb_mask = df.values <= lb + ub_mask = df.values >= ub + mask = ~lb_mask & ~ub_mask + self.assert_((clipped_df.values[lb_mask] == lb).all() == True) + self.assert_((clipped_df.values[ub_mask] == ub).all() == True) + self.assert_((clipped_df.values[mask] == df.values[mask]).all() == True) def test_get_X_columns(self): # numeric and object columns