Skip to content

BUG: replace not converting dtypes #3907

Closed
@jreback

Description

@jreback

I believe replace should do a convert_objects(copy=False) after replacement to provide dtype soft-conversion

In [1]: df = DataFrame([['foo','bar','bah'],['bar','foo','bah']])

In [2]: df
Out[2]: 
     0    1    2
0  foo  bar  bah
1  bar  foo  bah

In [3]: m = { 'foo' : 1, 'bar' : 2, 'bah' : 3 }

In [5]: df.replace(m)
Out[5]: 
   0  1  2
0  1  3  2
1  2  1  3

In [6]: df.replace(m).dtypes
Out[6]: 
0    object
1    object
2    object
dtype: object

In [8]: df.replace(m).convert_objects().dtypes
Out[8]: 
0    int64
1    int64
2    int64
dtype: object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions