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

BUG: Some string methods treat "." as regex, others don't #37963

Open
2 of 3 tasks
giangiacomosanna opened this issue Nov 19, 2020 · 2 comments
Open
2 of 3 tasks

BUG: Some string methods treat "." as regex, others don't #37963

giangiacomosanna opened this issue Nov 19, 2020 · 2 comments
Labels
API - Consistency Internal Consistency of API/Behavior Docs Strings String extension data type and string data

Comments

@giangiacomosanna
Copy link

giangiacomosanna commented Nov 19, 2020

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import re
import pandas as pd

texts = ['aa.aa', 'bbb']

# str.replace vs re.sub: '.' is not treated as regex by pandas
print(*pd.Series(texts).str.replace('.', '*'), sep=', ')
print(*pd.Series(texts).str.replace('..', '**'), sep=', ')
print(*[re.sub('.', '*', t) for t in texts], sep=', ')
print(*[re.sub('..', '**', t) for t in texts], sep=', ')
print()

# str.split vs re.split: '.' is not treated as regex by pandas
print(*pd.Series(texts).str.split('.'), sep=', ')
print(*pd.Series(texts).str.split('..'), sep=', ')
print(*[re.split('.', t) for t in texts], sep=', ')
print(*[re.split('..', t) for t in texts], sep=', ')
print()

# str.contains vs re.search: '.' is treated as regex by both
print(*pd.Series(texts).str.contains('.'), sep=', ')
print(*pd.Series(texts).str.contains('..'), sep=', ')
print(*[re.search('.', t).start() for t in texts], sep=', ')
print(*[re.search('..', t).start() for t in texts], sep=', ')

Problem description

Currently some pandas string functions (eg str.replace, str.split) treat "." as a regex matching any character, while some others (eg str.contains) treat it like "[.]", the regex matching the "dot" character. The ".." string is instead always correctly handled as the regex matching any two characters.

Expected Output

pandas string functions (when in regex mode) should treat "." consistently as the regex matching any single character. If there is a reason to treat as an exception, or to do so only for some string functions, should be documented.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.1.post20201107
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@giangiacomosanna giangiacomosanna added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 19, 2020
@giangiacomosanna giangiacomosanna changed the title BUG: BUG: Some string methods treat "." as regex, others don't Nov 19, 2020
@jreback
Copy link
Contributor

jreback commented Nov 20, 2020

there are a lot of very similiar issues eg #24804

a single character is treated as not a regex. i suppose could be documented (i think it is in places).

PRs to fix are welcome.

@jreback jreback added Strings String extension data type and string data API - Consistency Internal Consistency of API/Behavior Docs and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 20, 2020
@jreback jreback added this to the Contributions Welcome milestone Nov 20, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@Vesalon
Copy link

Vesalon commented Mar 6, 2025

Can this issue be addressed? there is a similar issue I discovered, related to how square brackets in a string/pattern are handled in str.contains vs str.replace. This is very surprising discrepance and I'm surprised to see this issue is 5 years old!

The below code will show that res1 and res2 match, but res0 will ungracefully return an unexpected result:

import pandas as pd

placeholder = "[PLACEHOLDER]"

texts_w_placeholders = [
    "dummy text [PLACEHOLDER]",
    "dummy2 [PLACEHOLDER]",
    "bla bla [ANOTHER_PLACEHOLDER]",
]

df = pd.DataFrame(texts_w_placeholders, columns=['text'])

res0 = df['text'].str.contains(placeholder)
res1 = df['text'].str.contains("\[PLACEHOLDER\]")
res2 = df['text'].apply(lambda text: placeholder in text)

print(res0, res1, res2, sep="\n\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Docs Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

4 participants