Skip to content

BUG: Forcing an int dtype on DataFrame construction raises an odd error #58586

Open
@amanlai

Description

@amanlai

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([['1', '2'], ['3', '4']], dtype='int32')

Issue Description

The above code raises ValueError: Trying to coerce float values to integers on the development version (it is raised on 2.1.1 as well). However, if we force 'Int32', there is no error and the construction works as expected.

Expected Behavior

There should probably not be an error in the first place since .astype('int32') works as expected and extension dtypes also work as expected. Even if an error should be raised, the message is a bit off since the values are string representation of integers not floats, so perhaps the error message should be ValueError: Trying to coerce values to integers. Try astype instead. or something along those lines.

Installed Versions

commit : ea7bcd1
python : 3.12.0
OS : Windows 10
pandas : 3.0.0.dev0+880.gea7bcd14c8
numpy : 2.1.0.dev0+git20240402.e191a5f

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions