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

ERR: invalid input to .str.replace does not raise #13438

Closed
jreback opened this issue Jun 14, 2016 · 4 comments
Closed

ERR: invalid input to .str.replace does not raise #13438

jreback opened this issue Jun 14, 2016 · 4 comments
Labels
Error Reporting Incorrect or improved errors from pandas Strings String extension data type and string data
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Jun 14, 2016

This should raise; None is not an allowed option for the replacement.

In [2]: s = pd.Series(['a', 'b', None])

In [3]: s
Out[3]: 
0       a
1       b
2    None
dtype: object

In [4]: s.str.replace('a', None)
Out[4]: 
0   NaN
1   NaN
2   NaN
dtype: float64

cc @Kwsmith

@jreback jreback added Difficulty Novice Error Reporting Incorrect or improved errors from pandas Strings String extension data type and string data labels Jun 14, 2016
@jreback jreback added this to the Next Major Release milestone Jun 14, 2016
@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Jun 16, 2016

It's not only None that gives non-sensical output, also eg numerical values:

In [115]: s.str.replace('a', 3)
Out[115]:
0   NaN
1   NaN
2   NaN
dtype: float64

So rather than checking for None, maybe just check that the replacement is string-like?
Or are there arguments that do work that are not string like?

@priyankjain
Copy link
Contributor

priyankjain commented Jun 16, 2016

Gotcha, is the following output valid:

s.str.replace('','a')

0     aaa
1     aba
2    None
dtype: object

@jorisvandenbossche
Copy link
Member

Hmm, not sure about that one. At first, it seems invalid as well, but this is the standard behaviour of str.replace:

In [125]: 'a'.replace('', 'b')
Out[125]: 'bab'

@priyankjain
Copy link
Contributor

I think it makes more sense to consider this as valid, since that's how str.replace works. Also it would make sense to throw TypeError instead of ValueError:

'abc'.replace('a', None)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-51f0617eaa9d> in <module>()
----> 1 'abc'.replace('a', None)

TypeError: Can't convert 'NoneType' object to str implicitly

@jreback jreback modified the milestones: 0.18.2, Next Major Release Jun 16, 2016
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 Strings String extension data type and string data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants