Skip to content

Clip Could take Series/DataFrames #2657

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

Closed
rhaskett opened this issue Jan 8, 2013 · 4 comments
Closed

Clip Could take Series/DataFrames #2657

rhaskett opened this issue Jan 8, 2013 · 4 comments

Comments

@rhaskett
Copy link

rhaskett commented Jan 8, 2013

I was a little surprised when clip would not take other pandas objects as parameters. I hacked a little something below, but for readability I thought I might suggest that this feature be added.

df1 = DataFrame(ones((2, 2)))
data = ones((2, 2))
data[0, 0] = 0
data[1, 1] = 2
df2 = DataFrame(data)
print df2.clip(1.5, 0.5)

df3 = df2.copy()
df3[df2 > df1 * 1.5] = (df1 * 1.5)[df2 > df1 * 1.5]
df3[df2 < df1 * .5] = (df1 * .5)[df2 < df1 * .5]
print df3
@brandon-rhodes
Copy link
Contributor

I was also surprised, this morning, that clip() does not accept Series as bounds for pair-wise comparison. Here is an example of my current work-around, compared with how I thought that clip() would operate (in case it makes the issue clearer for anyone, since the issue above does not actually show how clip() ought to work):

import pandas as pd
from StringIO import StringIO

df = pd.DataFrame.from_csv(StringIO("""\
,over,but_not_over,rate
0,692.00,2179.00,0.10
1,2179.00,6733.00,0.15
2,6733.00,12892.00,0.25
3,12892.00,19279.00,0.28
4,19279.00,33888.00,0.33
5,33888.00,38192.00,0.35
6,38192.00,inf,0.396
"""))

wages = 29279.00

# Today, I have to write:

wages_in_bracket = (df['but_not_over'].clip_upper(wages) - df['over']).clip_lower(0.00)
print wages_in_bracket

# But I had expected to be able to:

wages_in_bracket = df['but_not_over'].clip(df['over'], wages) - df['over']
#
# Traceback (most recent call last):
#  ...
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@kokes
Copy link
Contributor

kokes commented Oct 19, 2018

Isn't this a duplicate of #6966? I've just tested the second example and it does work.

@jreback
Copy link
Contributor

jreback commented Oct 19, 2018

yep

@jreback jreback closed this as completed Oct 19, 2018
@brandon-rhodes
Copy link
Contributor

I was commenting on Pandas issues back in 2013 — five years ago? Wow, I've used Pandas for a long time!

I'm happy to hear that clipping against series is now possible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants