Skip to content

Simple operation unexpectedly changes dtype #10503

Closed
@jeggleston

Description

@jeggleston

Hi all,
I can't find any documentation that says this should happen, so I think it's a bug. But maybe something's happening that I don't understand. When I do a simple operation (adding 1 to a slice), suddenly the dtype of the columns changes from uint32 to int64.
Any ideas why this is happening? Bug?
Thanks

Make a sample dataframe. Columns are dtype uint32.

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a':[0, 1, 1], 'b':[100, 200, 300]}, dtype='uint32')

In [3]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 2 columns):
a    3 non-null uint32
b    3 non-null uint32
dtypes: uint32(2)
memory usage: 48.0 bytes

Take a slice of a column. Adding 1 to that slice still results in dtype uint32.

In [4]: ix = df['a'] == 1

In [5]: z = df.loc[ix, 'b']

In [6]: z + 1
Out[6]: 
1    201
2    301
Name: b, dtype: uint32

But, if I modify that slice in the original dataframe, suddenly both columns of the dataframe are int64.

In [7]: df.loc[ix, 'b'] = z + 1

In [8]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 2 columns):
a    3 non-null int64
b    3 non-null int64
dtypes: int64(2)
memory usage: 72.0 bytes

I've seen this in 0.16, 0.16.1, and 0.16.2.

In [9]: pd.__version__
Out[9]: '0.16.2'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions