Skip to content

Regression for pd.Series.replace(np.nan, inplace=True) #5319

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
michaelaye opened this issue Oct 25, 2013 · 23 comments · Fixed by #5600
Closed

Regression for pd.Series.replace(np.nan, inplace=True) #5319

michaelaye opened this issue Oct 25, 2013 · 23 comments · Fixed by #5600
Labels
Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@michaelaye
Copy link
Contributor

This code was working in pd 0.12.0, but is failing in current master ('0.12.0-955-ga96cd66'):

df.last_el_cmd.replace(np.nan, inplace=True)

The error:

/usr/local/epd/lib/python2.7/site-packages/pandas-0.12.0_955_ga96cd66-py2.7-linux-x86_64.egg/pandas/core/generic.pyc in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   1903             if not is_dictlike(to_replace):
   1904                 if not is_dictlike(regex):
-> 1905                     raise TypeError('If "to_replace" and "value" are both None'
   1906                                     ' and "to_replace" is not a list, then '
   1907                                     'regex must be a mapping')

TypeError: If "to_replace" and "value" are both None and "to_replace" is not a list, then regex must be a mapping

I saw that some filter related stuff was updated to use regex for 0.13dev, but I don't understand why this makes my command failing?

@michaelaye
Copy link
Contributor Author

df.last_el_cmd.replace([np.nan], inplace=True) seems to be the way now, correct?

@jtratner
Copy link
Contributor

So this is working for you now? (Despite the confusing error message) Regardless that TypeError doesn't make sense, how can to_replace simultaneously be None and also potentially a list... I might take a glance at some point.

@ghost ghost assigned jtratner Oct 25, 2013
@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

Its type depends on the other arguments. To replace could be none if you pass a string to regex.

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

I can take a look tonight

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

I think I know what the issue is.

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

Filter is unrlated to this

@jtratner
Copy link
Contributor

The literal text says that to_replace is None and to_replace is not a list. First statement always implies the second right?

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

And type error is valid there. This is a bug that is incorrectly triggering a typewrror

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

Oh I c. Yup that is an error reporting big :)

@cpcloud
Copy link
Member

cpcloud commented Oct 25, 2013

Actually I'm surprised this isn't covered. It's such a simple case.

@michaelaye
Copy link
Contributor Author

But: Why is this a bug now and was working in 0.12? What DID change?

@jreback
Copy link
Contributor

jreback commented Oct 25, 2013

in 0.13, Series went from a sub-class of ndarray to a sub-class of NDFrame (which sub-class the rest of pandas objects). As a result we were able to move most methods to a generic class, and have consisten type signatures for all objects. In this case, this the frame method was much more complete and accepts much more input. This is merely a validation issue, AFAIK. @cpcloud will address shortly. see here: http://pandas.pydata.org/pandas-docs/dev/whatsnew.html#internal-refactoring

@michaelaye
Copy link
Contributor Author

Bump. This is still an API change (at least for me), because before, df.last_el_cmd.replace(np.nan, inplace=True) was working, now I have to df.last_el_cmd.replace([np.nan], inplace=True).
Do I have to encapsulate the argument in a list or not?

@jreback
Copy link
Contributor

jreback commented Nov 26, 2013

@cpcloud what did we decide about this?

@cpcloud
Copy link
Member

cpcloud commented Nov 26, 2013

is this still broken? i can't really test right now ... can look later

@cpcloud
Copy link
Member

cpcloud commented Nov 26, 2013

oh i c, nvm

@cpcloud
Copy link
Member

cpcloud commented Nov 26, 2013

this should work, so it's a bug

@jreback
Copy link
Contributor

jreback commented Nov 27, 2013

@cpcloud @michaelaye see #5600

The DataFrame case always raised a TypeError, its just slightly different. In theory could support it, but it IS confusing (and prob an error), so fine with the error. (FYI the inplace and not are the same case)

@michaelaye
Copy link
Contributor Author

Don't get me wrong, I don't insist on it working as a scalar, as it is obvious that in many cases pandas requires a container as parameter. I just would like to freeze the type on my end the way it is supposed to be and go ahead.

@jreback
Copy link
Contributor

jreback commented Nov 27, 2013

I think for compat it should (as it did in 0.12), the PR fixes this, no?

@michaelaye
Copy link
Contributor Author

How do I check an unmerged pull request? Would I have to fork your pandas fork?

@jtratner
Copy link
Contributor

jtratner commented Dec 1, 2013

@michaelaye you need to pull it down from @jreback 's branch:

git remote add jreback https://www.github.com/jreback/pandas.git
git fetch jreback
git checkout -t jreback/replace_api
# build/install

@michaelaye
Copy link
Contributor Author

Great, thanks!
I confirm that this PR again allows a np.nan scalar to be used as first parameter in Series.replace()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
4 participants