Skip to content

series.replace bug in .24 - regex not replacing patterns in middle of string (only beginning) #25530

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
SigReaver opened this issue Mar 4, 2019 · 2 comments
Labels
Duplicate Report Duplicate issue or pull request

Comments

@SigReaver
Copy link

Code Sample, a copy-pastable example if possible

###sample code:
import pandas as pd
import re

s = pd.Series(['hello there', 'well hello then'])
print(s)

'''OUTPUT
0 hello there
1 well hello then
dtype: object
'''

s.replace({'hello' : 'goodbye'}, regex=True, inplace=True)

print(s)

'''OUTPUT
0 goodbye there
1 well hello then #<-not replaced
dtype: object
'''

###expected output using re.sub as per series.replace documentation
x = re.sub('hello','goodbye', 'hello there')
print(x)
#out: goodbye there

x = re.sub('hello','goodbye', 'well hello then')
print(x)

Problem description

.24 bug where series.replace is no longer working the same as in .23 pandas. In past versions, pandas would replace substrings that appeared in the middle/end of strings, but now it only seems to work if the pattern is at the very beginning.

I saw the docs mention that passing a dict and using regex follows re.sub, but when I tried that, I got the expected result (not the one .24 pandas yielded).

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

see above - should replace sub-strings in middle (not just beginning)

Output of pd.show_versions()

[paste the output of ``pd.show_versions()`` here below this line] INSTALLED VERSIONS ------------------ commit: None python: 3.7.0.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.24.1
pytest: 3.8.0
pip: 10.0.1
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.16.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: 1.7.9
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: 2.5.6
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.1.0
lxml.etree: None
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None

@WillAyd
Copy link
Member

WillAyd commented Mar 4, 2019

This should be a duplicate of #25259 if you try on master should be resolved

@WillAyd WillAyd added the Duplicate Report Duplicate issue or pull request label Mar 4, 2019
@SigReaver
Copy link
Author

Awesome, thanks! My bad on the dupe - missed it later in that thread

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

3 participants