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

Series.str.replace does not support $ and ^ regex charecters #30015

Closed
eeshugerman opened this issue Dec 3, 2019 · 3 comments
Closed

Series.str.replace does not support $ and ^ regex charecters #30015

eeshugerman opened this issue Dec 3, 2019 · 3 comments
Labels
Duplicate Report Duplicate issue or pull request Strings String extension data type and string data

Comments

@eeshugerman
Copy link

Code Sample

import pandas as pd

s = pd.Series(['foo'])

s.str.replace('$', 'bar', regex=True)
# 0    foo
# dtype: object

s.str.replace('^', 'bar', regex=True)
# 0    foo
# dtype: object

Problem description

When regex=True, ^ should match the beginning of the string, and $ should match the end of the string, but this is not the case.

Expected Output

import pandas as pd

s = pd.Series(['foo'])

s.str.replace('$', 'bar', regex=True)
# 0    foobar
# dtype: object

s.str.replace('^', 'bar', regex=True)
# 0    barfoo
# dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.6.9.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.3.10-134.current
machine          : x86_64
processor        : 
byteorder        : little
LC_ALL           : None
LANG             : en_US.utf8
LOCALE           : en_US.UTF-8

pandas           : 0.25.3
numpy            : 1.15.4
pytz             : 2018.7
dateutil         : 2.7.5
pip              : 18.1
setuptools       : 40.5.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : 2.7.6.1 (dt dec pq3 ext lo64)
jinja2           : 2.10
IPython          : 7.1.1
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : None
sqlalchemy       : 1.2.14
tables           : None
xarray           : None
xlrd             : 1.1.0
xlwt             : None
xlsxwriter       : None
@Liam3851
Copy link
Contributor

Liam3851 commented Dec 3, 2019

I think this is a duplicate of #24804 but it's good to see a case of an attempted meaningful single-character regex in use in the wild (while s + 'bar' is probably more common, in theory this should work).

This is a good reminder that #24804 is still there and this anomaly needs deprecation. Unless, of course, consensus now (given another year has passed and reliance interests on the pre-0.23 behavior are presumably lower) the thought is that this anomaly can just be fixed as opposed to deprecated.

@jreback
Copy link
Contributor

jreback commented Dec 3, 2019

yep this looks like a duplicate thanks @Liam3851

as always PRs are welcome to patch

@jreback jreback closed this as completed Dec 3, 2019
@jreback jreback added Duplicate Report Duplicate issue or pull request Strings String extension data type and string data labels Dec 3, 2019
@jreback jreback added this to the No action milestone Dec 3, 2019
@eeshugerman
Copy link
Author

Ah I see: when the pattern is a single character it's always treated as a literal, regardless of the regex parameter.

Are there plans to make to make regex=False the default? If so, perhaps this could be rolled into the same deprecation warning and API change documentation.

FWIW, my use case: we've built what's basically a lightweight report cleaning app, and the pattern to replace is user input. The app does not have a feature to prepend/append to values; rather the user should be able to accomplish this with the regex find/replace functionality.

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 Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

3 participants