-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Series.str.strip() removes numeric and bool values instead of only string whitespace #44867
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
Comments
Thanks @mspacek for the report.
This is not a bug, it is longstanding, tested and expected behavior which is unlikely to be changed. PR to clarify the behavior for mixed type object Series welcome. from https://pandas.pydata.org/pandas-docs/dev/user_guide/text.html#string-methods
and there is a discussion issue open to consider deprecating the str accessor on object dtype Series #29710. and tbc it is not just
as a workaround for the specific use case to get the "expected" behavior, could maybe consider something like...
|
Thanks for the reply @simonjayhawkins ! I guess I find the behaviour very surprising, since it leads to silent data loss, which to me qualifies as a major bug. Good to see though that it's being considered for deprecation, and that the documentation is being updated. |
Hi, |
May I work on this? |
Hello I am first time contributor. If the issue is still open I would like to work on it? |
take |
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 master branch of pandas.
Reproducible Example
Issue Description
The documentation for
Series.str.strip()
says:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.strip.html
I assumed that for Series with mixed data types, i.e. that have
dtype=object
, this method would only strip whitespace from string-like entries. Instead, to my surprise, it also replaces numeric and bool values withNaN
.I assume this isn't intentional, but if it is intentional, it should be documented.
Expected Behavior
Numeric and bool values should be left untouched.
Series.str.strip()
should only apply to string values. Here's my naive implementation:Installed Versions
INSTALLED VERSIONS
commit : 66e3805
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-91-lowlatency
Version : #102-Ubuntu SMP PREEMPT Fri Nov 5 18:18:39 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.5
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.7.3
pip : 21.3.1
setuptools : 45.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.30.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.5.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: