Skip to content

Numeric Index constructors should validate dtype parameter #29539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jschendel opened this issue Nov 11, 2019 · 1 comment · Fixed by #29545
Closed

Numeric Index constructors should validate dtype parameter #29539

jschendel opened this issue Nov 11, 2019 · 1 comment · Fixed by #29545
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions Error Reporting Incorrect or improved errors from pandas Index Related to the Index class or subclasses
Milestone

Comments

@jschendel
Copy link
Member

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.

@jschendel jschendel added Dtype Conversions Unexpected or buggy dtype conversions Error Reporting Incorrect or improved errors from pandas Index Related to the Index class or subclasses Constructors Series/DataFrame/Index/pd.array Constructors labels Nov 11, 2019
@MarcoGorelli
Copy link
Member

Sure, I could work on this if you like (though if any new contributors following along would like to take it, please do go ahead)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions Error Reporting Incorrect or improved errors from pandas Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants