Skip to content

BUG: Regex parameter from replace function doesn't work on string dtype #36472

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
GYHHAHA opened this issue Sep 19, 2020 · 1 comment
Closed
Labels
Duplicate Report Duplicate issue or pull request

Comments

@GYHHAHA
Copy link
Contributor

GYHHAHA commented Sep 19, 2020

import pandas as pd
print(pd.version)
1.1.1
s1 = pd.Series(['a','b'], dtype='string')
s2 = pd.Series(['a','b'], dtype='object')
s1.replace(r'[a]',pd.NA,regex=True)
0 a
1 b
dtype: string
s2.replace(r'[a]',pd.NA,regex=True)
0
1 b
dtype: object

Hi, it seems that regex doesn't function well for the replace method on string dtype, but normal on object dtype.

Besides, I also find setting pd.NA as repl of str.replace method will raise error, both on string and object.
Yet I think it may be better not to raise this error because the pd.NA indeed could be a missing string value.

pd.Series(['a','b'],dtype='string').str.replace('a',pd.NA)
TypeError: repl must be a string or callable
pd.Series(['a','b'],dtype='object').str.replace('a',pd.NA)
TypeError: repl must be a string or callable

Thanks!

@GYHHAHA GYHHAHA added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 19, 2020
@GYHHAHA
Copy link
Contributor Author

GYHHAHA commented Sep 19, 2020

Oh, I find #35977 and #36038 have already discussed this bug.
Just see the second part for pd.NA permission in str.replace method.

@dsaxton dsaxton added Duplicate Report Duplicate issue or pull request and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 19, 2020
@GYHHAHA GYHHAHA closed this as completed Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants