Skip to content

BUG: convert_dtypes() leaves int + pd.NA Series as object instead of converting to Int64 #48791

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
zaneselvans opened this issue Sep 26, 2022 · 0 comments · Fixed by #48857
Closed
2 of 3 tasks
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@zaneselvans
Copy link

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

import numpy as np
import pandas as pd
from pandas.testing import assert_series_equal

# Passes -- both dtypes are Int64
s1 = pd.Series([0, 1, 2, 3])
assert_series_equal(s1.astype("Int64"), s1.convert_dtypes())

# Passes -- both dtypes are Int64
s2 = pd.Series([0, 1, 2, np.nan])
assert_series_equal(s2.astype("Int64"), s2.convert_dtypes())

# Passes -- both dtypes are Int64
s3 = pd.Series([0, 1, 2, None])
assert_series_equal(s3.astype("Int64"), s3.convert_dtypes())

# Fails -- dtypes are different (object vs. Int64)
s4 = pd.Series([0, 1, 2, pd.NA])
assert_series_equal(s4.astype("Int64"), s4.convert_dtypes())
# Attribute "dtype" are different
# [left]:  Int64
# [right]: object

Issue Description

With a Series containing python ints and values of np.nan or None, convert_dtypes() infers that the type should be the nullable pandas type Int64 and converts, as it does for a Series composed entirely of valid ints. However, a Series that contains int and pd.NA missing values unexpectedly retains its object dtype.

This behavior is present in at least pandas v1.4.4 and v1.5.0.

Expected Behavior

Given that pd.NA is already the appropriate missing value for an Int64 Series, I thought that convert_dtypes() would convert the type of the Series from object to Int64. This is also what I expected based on the documentation:

For object-dtyped columns, if infer_objects is True, use the inference rules as during normal Series/DataFrame construction. Then, if possible, convert to StringDtype, BooleanDtype or an appropriate integer or floating extension type, otherwise leave as object.

Clearly it's possible to convert to Int64 since the explicit astype("Int64") has no problem.

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-48-generic
Version : #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 65.4.0
pip : 22.2.2
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 : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@zaneselvans zaneselvans added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 26, 2022
@phofl phofl added Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants