Skip to content

DataFrame.loc/iloc fails to update object with new dtype #24269

Open
@TomAugspurger

Description

@TomAugspurger

Similar to #4312 and #5702, but this seems specific to object dtype -> new dytpe.

In this first case, we likely convert the whole block, even though we just wanted A.

In [20]: df = pd.DataFrame({"A": [1, 2], "B": [3, 4]}, dtype=object)

In [21]: df.dtypes
Out[21]:
A    object
B    object
dtype: object

In [22]: df.loc[:, ['A']] = df.loc[:, ['A']].astype(int)

In [23]: df.dtypes
Out[23]:
A    int64
B    int64
dtype: object

In this one, (maybe a different bug), we fail to convert ['a', 'b'] to float when they start out in an object block with other values.

In [13]: df = pd.DataFrame([[np.nan, np.nan, 1, pd.Timestamp('2000')]], columns=['a', 'b', 'c', 'd'], dtype=object)

In [14]: df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(float)

In [15]: df.dtypes
Out[15]:
a    object
b    object
c    object
d    object
dtype: object

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions