Replace partial matches behavior · Issue #5143 · pandas-dev/pandas (original) (raw)

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?