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: DataFrame.astype(dict) can cause column metadata to be lost #19920

Closed
jschendel opened this Issue Feb 27, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@jschendel
Member

jschendel commented Feb 27, 2018

Code Sample, a copy-pastable example if possible

Setup:

In [2]: df = pd.DataFrame(data=np.random.randint(5, size=(3, 4)),
   ...:                   columns=pd.UInt64Index(range(500, 504), name='foo'))
   ...:

In [3]: df
Out[3]:
foo  500  501  502  503
0      4    4    2    1
1      0    2    3    1
2      1    4    0    0

In [4]: df.columns
Out[4]: UInt64Index([500, 501, 502, 503], dtype='uint64', name='foo')

Using .astype(dict) loses column metadata:

In [5]: df = df.astype({500: 'float64', 501: 'uint64'})

In [6]: df.columns
Out[6]: Int64Index([500, 501, 502, 503], dtype='int64')

Problem description

Column metadata is lost: the name has been removed and it has been cast from UInt64Index to Int64Index.

Expected Output

I'd expect the columns post-astype to be the same as pre-astype.

@jschendel

This comment has been minimized.

Member

jschendel commented Feb 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment