Skip to content

BUG: .str.replace() with capture groups does not play nice with string methods #61043

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
3 tasks done
noahblakesmith opened this issue Mar 3, 2025 · 1 comment
Closed
3 tasks done
Labels
Bug Strings String extension data type and string data

Comments

@noahblakesmith
Copy link

noahblakesmith commented Mar 3, 2025

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

Code

import pandas as pd
c = pd.Series("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")
x, y, z = "\\b(FOX|THE)\\b", "_ABC_", "\\1_ABC_"
print(c.str.replace(x, y.lower(), regex=True))
print(c.str.replace(x, z.lower(), regex=True))

Output

0    _abc_ QUICK BROWN _abc_ JUMPS OVER _abc_ LAZY DOG
dtype: object
0    THE_abc_ QUICK BROWN FOX_abc_ JUMPS OVER THE_a...
dtype: object

Issue Description

The .lower() string method inconsistently modifies the repl argument when the latter includes a regex capture group.

Expected Behavior

I would expect .lower() to modify all characters in repl, including those in the capture group (or a warning stating otherwise).

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.10.16
python-bits : 64
OS : Darwin
OS-release : 24.3.0
Version : Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:16 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0
Cython : None
sphinx : None
IPython : 8.33.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.2.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None

@noahblakesmith noahblakesmith added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 3, 2025
@rhshadrach
Copy link
Member

Thanks for the report. You're calling lower on a string. There is no way for pandas to tell the difference between the following two lines.

print(c.str.replace(x, "\\1_ABC_".lower(), regex=True))
print(c.str.replace(x, "\\1_abc_", regex=True))

Closing for now. If you believe I've misunderstood the issue, please comment how here and we can reopen!

@rhshadrach rhshadrach added Strings String extension data type and string data and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 3, 2025
@rhshadrach rhshadrach closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

2 participants