-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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: Negation of .str.isnumeric()
changes dtype
when pd.NA
is present
#61182
Comments
Thanks for the report! Your input to print(~True)
# -2 If you would like Kleene-logic, then you should likely specify Edit: The remainder of this comment is misleading, see below.
pd.set_option("infer_string", True)
s = pd.Series(["", pd.NA])
print(s.str.isnumeric())
# 0 False
# 1 False
# dtype: bool |
Thanks for the response, @rhshadrach. What you said makes sense. However, using pd.set_option("infer_string", True)
s = pd.Series(["", pd.NA])
print(~s.str.isnumeric())
# TypeError: bad operand type for unary ~: 'float'
t = pd.Series([""])
print(~t.str.isnumeric())
# 0 False
# dtype: bool
# 0 True
# dtype: bool |
Not sure that should be the case here with the legacy object array. pd.NA, although a python object that can be held by an object array, since it can hold any Python object is not the internal representation of a missing value. I would suspect that If we wanted to propagate the pd.NA value for the legacy object array we would need to return a pandas nullable boolean array which would mean the return type of So it appears that the return type being a object array when pd.NA is present is a bug. Thanks for the report. |
I think there are a few bugs wrapped up in this discussion. To clarify, the behavior you are looking for is achievable when you use the >>> ser = pd.Series(["", pd.NA], dtype=pd.StringDtype())
>>> ~ser.str.isnumeric()
0 True
1 <NA>
dtype: boolean The "infer_string" option uses that same |
Thanks @WillAyd - I've edited my comment above. So I think we're good here; my example above should have been: pd.set_option("infer_string", True)
s = pd.Series(["", np.nan])
print(s.str.isnumeric())
# 0 False
# 1 False
# dtype: bool With NaN, I think we want this to come out as |
Hmm that's tricky. I think if you were to just evaluate the result of an inversion with np.nan, it would be strange to get Generally there isn't a universal solution to a problem like this using |
not sure why this is closed. the docs https://pandas.pydata.org/docs/reference/api/pandas.Series.str.isnumeric.html state that the return type is "Series or Index of boolean values with the same length as the original Series/Index.". All the examples show here we have s = pd.Series(["", "0", "123", " 123", pd.NA])
print(s)
print(s.str.isnumeric())
# 0
# 1 0
# 2 123
# 3 123
# 4 <NA>
# dtype: object
# 0 False
# 1 True
# 2 True
# 3 False
# 4 <NA>
# dtype: object Surely this is a bug? The expected result of
so that the result could be used as an indexer and that logical negation would work as expected? I would not expect |
Thanks @simonjayhawkins - I missed your previous comment. Reopening. However it's not clear to me that the propagation of the NA value in object dtype is a bug. I'd guess it's likely that the documentation of No strong opinion on my side.
Why not object? |
So it appears that object dtype is returned using the string accessor on object series containing non-string values... s = pd.Series(["", "0", "123", " 123", 123])
print(s)
print(s.str.isnumeric())
# 0
# 1 0
# 2 123
# 3 123
# 4 123
# dtype: object
# 0 False
# 1 True
# 2 True
# 3 False
# 4 NaN
# dtype: object In this case I would have expected the numeric 123 value to also be False and a boolean array returned since the str accessor should only be operating on the string values IMO. So this does not look like an inconsistency arising specifically from having the pd.NA object as a value in a object dtype Series.
Agree. If returning an object array when using the str accessor on object arrays that have some non-string values is long standing behavior then I'm inclined to not consider the pd.NA being persevered a bug at this time. |
Pandas version checks
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 main branch of pandas.
Reproducible Example
Issue Description
When
pd.NA
is present in aSeries
object, negating the.str.isnumeric()
method changesbool
values toint
values.Expected Behavior
Negation should adhere to the Kleene logic implemented elsewhere in
pandas
.Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.10.16
python-bits : 64
OS : Linux
OS-release : 6.8.0-1021-azure
Version : #25-Ubuntu SMP Wed Jan 15 20:45:09 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 2.2.2
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 25.0
Cython : None
sphinx : None
IPython : 8.34.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.3.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 5.3.1
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.39
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: