You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importpandasaspds=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
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
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
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 usestr.replace
instead. But that doesn't work for byte string: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
The text was updated successfully, but these errors were encountered: