Skip to content
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

BUG: DataFrame.clip arguments inconsistent (close #GH 2747) #2750

Merged
merged 2 commits into from
Feb 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -5042,6 +5042,11 @@ def clip(self, upper=None, lower=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):
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6470,6 +6470,22 @@ def test_clip(self):
double = self.frame.clip(upper=median, lower=median)
self.assert_(not (double.values != median).any())

def test_dataframe_clip(self):

# GH #2747
df = DataFrame(np.random.randn(1000,2))

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

Expand Down