Skip to content

Nullable Int64 datatype is not preserved when used as index #30001

Closed
@jfogelberg

Description

@jfogelberg

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({'a': [1, 2, None], 'b':[1, 2, 1]}, dtype=pd.Int64Dtype())
df.set_index('a').reset_index().dtypes

a float64
b Int64
dtype: object

Problem description

Nullable Int64 datatype is lost if the column is used as index.
This means that it is lost whenever we do a groupby followed by a reset_index and if we try to merge it with the original dataframe we get errors (at least if we try to merge int64 with Int64).

If the column contained NaNs it will be converted to float, if it did not contain NaNs it will be converted to int64 (not nullable).

Expected Output

a Int64
b Int64
dtype: object

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.24.2
pytest: 4.6.2
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.10
numpy: 1.16.4
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.9.0
sphinx: None
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.0
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: 1.3.5
pymysql: None
psycopg2: None
jinja2: 2.10.3
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate ReportDuplicate issue or pull requestExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions