-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Replace in string
series with NA
#32621
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
@albertotb Thanks for the report! Slightly related to #32075 It seems this works with scalars:
but not with lists/dicts |
string
series with NAstring
series with NA
Hi, can I try to pick this up? |
Yes, that would be welcome! |
So, here's a few things I've noticed so far:
I'm now investigating what's the best way of resolving this. |
The pd.NA values are replaced with np.nan before comparing the arrays/scalars
I also noticed another issue, have a look at this code: import pandas as pd
import numpy as np
replacements = {'one': '1', 'two': '2'}
series_a = pd.Series(['one', 'two'], dtype='string')
for rep in replacements:
series_a = series_a.replace(rep, replacements[rep])
series_b = pd.Series(['one', 'two'], dtype='string')
series_b = series_b.replace(to_replace=replacements) It produces the following output: Shouldn't they both return a |
Added condition for when to apply the na replacement
Here's an explanation of my current solution (chrispe@47f6676). In order to enable comparison with arrays containing # Replace all definitions of missing values (isna=True) to a numpy.nan
# Where x is an array of values
x = np.where(isna(x), np.nan, x) |
The pd.NA values are replaced with np.nan before comparing the arrays/scalars
Made improvements based on the tests which failed
Added change to resolve linting check
Added test for the reported bug
Code Sample, a copy-pastable example if possible
Problem description
pandas.Series.replace
cannot be used in series of typestring
that contain<NA>
Expected Output
I would expect
C
to output the same asB
but withstring
dtypeOutput of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-112-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : es_ES.UTF-8
LOCALE : es_ES.UTF-8
pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 41.4.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: