Skip to content

BUG: convert_dtypes and infer_objects do not convert numeric-like strings  #56610

@abokey1

Description

@abokey1

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame({"col1": ["123", "456"], "col2": ["1.23", "45.6"]})

>>> df.dtypes
# col1    object
# col2    object
# dtype: object

>>> df.convert_dtypes().dtypes
# col1    string[python]
# col2    string[python]
# dtype: object

>>> df.infer_objects().dtypes
# col1    object
# col2    object
# dtype: object

Issue Description

As stated in the title, neither of the two methods succeeds in correctly inferring the best possible dtypes.

Expected Behavior

A possible workaround would be :

>>> df.apply(pd.to_numeric).dtypes
# col1      int64
# col2    float64
# dtype: object

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsDtype ConversionsUnexpected or buggy dtype conversions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions