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

Replace partial matches behavior #5143

Closed
TomAugspurger opened this issue Oct 7, 2013 · 8 comments · Fixed by #5145
Closed

Replace partial matches behavior #5143

TomAugspurger opened this issue Oct 7, 2013 · 8 comments · Fixed by #5145

Comments

@TomAugspurger
Copy link
Contributor

I ran into some unexpected behavior with replace today.

I want to replace the answer with a numeric value according to the dictionary weights

In [33]: df.answer.head()
Out[33]: 
0       Strongly Agree
1                Agree
2              Neutral
3             Disagree
4    Strongly Disagree
Name: answer, dtype: object

In [34]: weights
Out[34]: 
{'Agree': 4,
 'Disagree': 2,
 'Neutral': 3,
 'Strongly Agree': 5,
 'Strongly Disagree': 1}

In [35]: df.answer.replace(weights).head()
Out[35]: 
0    4
1    4
2    3
3    2
4    2
dtype: int64

It looks like replace matches on the first part, Agree, or Disagree and doesn't make it through the dict to the Stronglys. Am I just being a noob with how regexes work, or is this a bug?

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

ok yep ... should possibly add a 'search' or 'match' parameter or something like match=False

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

right now it does search

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

in this case instead of using replace you can do

Series(weights)[answer]

this is probably faster for larger series as well

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

@jreback @jtratner what do think ?

was thinking:

mode='search' (default) or mode='match'

or

match=False

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

really trivial fix after deciding on the API

@jreback
Copy link
Contributor

jreback commented Oct 7, 2013

he wants an exact match....I thought you do that UNLESS you see a regex?

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

hm yes should be that ... let me look

@cpcloud
Copy link
Member

cpcloud commented Oct 7, 2013

yep just needed to accept tuples

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

Successfully merging a pull request may close this issue.

3 participants