Skip to content

BUG: Series dtype not as specified when values are missing #46782

Closed
@emmanuelito

Description

@emmanuelito

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

# In 1.4.2
import pandas as pd
s = pd.Series( index=[1, 2, 3 ], dtype=int)
s.dtype
# -> dtype('float64') *ouch*

# ---- vs in 1.3.5
s = pd.Series( index=[1, 2, 3 ], dtype=int)
s.dtype
# -> dtype('int64') *correct*

Issue Description

pd.Series() does not respect the specified dtype and breaks compatibility with previous (1.3.5) version.

Expected Behavior

Should be backward compatible with 1.3.5: the result should have the dtype specified by the caller.

This issue seems (I'm not sure) related to #46672

The problem is that missing values (unspecified in the constructor) are initialized to NaNs, and thus floats, whereas in Pandas 1.3.5 they were initialized with zeroes.

Installed Versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsDtype 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