Skip to content

Commit 8d299e4

Browse files
authored
REF: call pandas_dtype up-front in Index.__new__ (#33407)
1 parent e7cbe6d commit 8d299e4

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

pandas/core/indexes/base.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
is_signed_integer_dtype,
4949
is_timedelta64_dtype,
5050
is_unsigned_integer_dtype,
51+
pandas_dtype,
5152
)
5253
from pandas.core.dtypes.concat import concat_compat
5354
from pandas.core.dtypes.generic import (
@@ -68,6 +69,7 @@
6869
from pandas.core.accessor import CachedAccessor
6970
import pandas.core.algorithms as algos
7071
from pandas.core.arrays import ExtensionArray
72+
from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype
7173
from pandas.core.base import IndexOpsMixin, PandasObject
7274
import pandas.core.common as com
7375
from pandas.core.indexers import deprecate_ndim_indexing
@@ -292,54 +294,59 @@ def __new__(
292294

293295
name = maybe_extract_name(name, data, cls)
294296

297+
if dtype is not None:
298+
dtype = pandas_dtype(dtype)
299+
if "tz" in kwargs:
300+
tz = kwargs.pop("tz")
301+
validate_tz_from_dtype(dtype, tz)
302+
dtype = tz_to_dtype(tz)
303+
295304
if isinstance(data, ABCPandasArray):
296305
# ensure users don't accidentally put a PandasArray in an index.
297306
data = data.to_numpy()
298307

308+
data_dtype = getattr(data, "dtype", None)
309+
299310
# range
300311
if isinstance(data, RangeIndex):
301312
return RangeIndex(start=data, copy=copy, dtype=dtype, name=name)
302313
elif isinstance(data, range):
303314
return RangeIndex.from_range(data, dtype=dtype, name=name)
304315

305316
# categorical
306-
elif is_categorical_dtype(data) or is_categorical_dtype(dtype):
317+
elif is_categorical_dtype(data_dtype) or is_categorical_dtype(dtype):
307318
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
308319
from pandas.core.indexes.category import CategoricalIndex
309320

310321
return _maybe_asobject(dtype, CategoricalIndex, data, copy, name, **kwargs)
311322

312323
# interval
313-
elif is_interval_dtype(data) or is_interval_dtype(dtype):
324+
elif is_interval_dtype(data_dtype) or is_interval_dtype(dtype):
314325
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
315326
from pandas.core.indexes.interval import IntervalIndex
316327

317328
return _maybe_asobject(dtype, IntervalIndex, data, copy, name, **kwargs)
318329

319-
elif (
320-
is_datetime64_any_dtype(data)
321-
or is_datetime64_any_dtype(dtype)
322-
or "tz" in kwargs
323-
):
330+
elif is_datetime64_any_dtype(data_dtype) or is_datetime64_any_dtype(dtype):
324331
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
325332
from pandas import DatetimeIndex
326333

327334
return _maybe_asobject(dtype, DatetimeIndex, data, copy, name, **kwargs)
328335

329-
elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype):
336+
elif is_timedelta64_dtype(data_dtype) or is_timedelta64_dtype(dtype):
330337
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
331338
from pandas import TimedeltaIndex
332339

333340
return _maybe_asobject(dtype, TimedeltaIndex, data, copy, name, **kwargs)
334341

335-
elif is_period_dtype(data) or is_period_dtype(dtype):
342+
elif is_period_dtype(data_dtype) or is_period_dtype(dtype):
336343
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
337344
from pandas import PeriodIndex
338345

339346
return _maybe_asobject(dtype, PeriodIndex, data, copy, name, **kwargs)
340347

341348
# extension dtype
342-
elif is_extension_array_dtype(data) or is_extension_array_dtype(dtype):
349+
elif is_extension_array_dtype(data_dtype) or is_extension_array_dtype(dtype):
343350
if not (dtype is None or is_object_dtype(dtype)):
344351
# coerce to the provided dtype
345352
ea_cls = dtype.construct_array_type()

pandas/tests/indexes/ranges/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_constructor_invalid_args(self):
4343
r"kind, 0 was passed"
4444
)
4545
with pytest.raises(TypeError, match=msg):
46-
Index(0, 1000)
46+
Index(0)
4747

4848
@pytest.mark.parametrize(
4949
"args",

0 commit comments

Comments
 (0)