Skip to content

BUG: replace method with regex=True does not work for byte string #34993

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
2 of 3 tasks
wenjuno opened this issue Jun 25, 2020 · 1 comment
Closed
2 of 3 tasks

BUG: replace method with regex=True does not work for byte string #34993

wenjuno opened this issue Jun 25, 2020 · 1 comment
Labels
Strings String extension data type and string data Usage Question

Comments

@wenjuno
Copy link

wenjuno commented Jun 25, 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
s = pd.Series([b'abc'])
# This will replace "abc" with "123", output:
#
# 0    b'123'
# dtype: object
#
print(s.replace(b'abc', b'123'))
# But with regex=True, it doesn't work, output:
#
# 0    b'abc'
# dtype: object
#
print(s.replace(b'abc', b'123', regex=True))

Problem description

It worked in pandas 0.25. It also works when the data is unicode string instead of byte string.

I see a discussion about regex parameter in #33302 and one suggestion is to use str.replace instead. But that doesn't work for byte string:

In [2]: s = pd.Series([b'abc'])

In [3]: s.str.replace(b'abc', b'123')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-da8a12a3193b> in <module>
----> 1 s.str.replace(b'abc', b'123')

/data/wenjun/miniconda3/envs/test_pd/lib/python3.7/site-packages/pandas/core/strings.py in wrapper(self, *args, **kwargs)
   1951                     f"inferred dtype '{self._inferred_dtype}'."
   1952                 )
-> 1953                 raise TypeError(msg)
   1954             return func(self, *args, **kwargs)
   1955

TypeError: Cannot use .str.replace with values of inferred dtype 'bytes'.

So it seems my only workaround is to decode the byte string into unicode string before calling the replace. If there are better ways to do it I'm happy to learn. Thanks.

Expected Output

0    b'123'
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-108-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.3.1.post20200622
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.15.0
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
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@wenjuno wenjuno added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 25, 2020
@jreback
Copy link
Contributor

jreback commented Jun 25, 2020

this is as expected you must always decode bytes before you can anything with them
you cannot treat them like strings which was incorrect in prior versions

@jreback jreback added Strings String extension data type and string data Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 25, 2020
@jreback jreback added this to the No action milestone Jun 25, 2020
@jreback jreback closed this as completed Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Strings String extension data type and string data Usage Question
Projects
None yet
Development

No branches or pull requests

2 participants