Skip to content

Numeric Index constructors should validate dtype parameter #29539

Closed
@jschendel

Description

@jschendel

The Int64Index, UInt64Index, and Float64Index constructors do not validate the dtype parameter and instead ignore it when it conflicts with the default dtype:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.26.0.dev0+842.g78c3c1640'

In [2]: pd.Int64Index([1, 2, 3], dtype="float64")
Out[2]: Int64Index([1, 2, 3], dtype='int64')

In [3]: pd.UInt64Index([1, 2, 3], dtype="categorical")
Out[3]: UInt64Index([1, 2, 3], dtype='uint64')

In [4]: pd.Float64Index([1, 2, 3], dtype="datetime64")
Out[4]: Float64Index([1.0, 2.0, 3.0], dtype='float64')

Other index types do not allow non-standard dtypes:

In [5]: pd.CategoricalIndex(list('abc'), dtype='timedelta64')
---------------------------------------------------------------------------
ValueError: Unknown dtype 'timedelta64'

In [6]: pd.DatetimeIndex(['2018-01-01'], dtype='int64')
---------------------------------------------------------------------------
ValueError: Unexpected value for 'dtype': 'int64'. Must be 'datetime64[ns]' or DatetimeTZDtype'.

In [7]: pd.IntervalIndex([pd.Interval(0, 1)], dtype=object)
---------------------------------------------------------------------------
TypeError: dtype must be an IntervalDtype, got object

In [8]: pd.RangeIndex(0, 3, dtype='float64')
---------------------------------------------------------------------------
TypeError: Invalid to pass a non-int64 dtype to RangeIndex

Expected Output

I'd expect In [2]/In [3]/In [4] to raise a ValueError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsDtype ConversionsUnexpected or buggy dtype conversionsError ReportingIncorrect or improved errors from pandasIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions