|
48 | 48 | is_signed_integer_dtype, |
49 | 49 | is_timedelta64_dtype, |
50 | 50 | is_unsigned_integer_dtype, |
| 51 | + pandas_dtype, |
51 | 52 | ) |
52 | 53 | from pandas.core.dtypes.concat import concat_compat |
53 | 54 | from pandas.core.dtypes.generic import ( |
|
68 | 69 | from pandas.core.accessor import CachedAccessor |
69 | 70 | import pandas.core.algorithms as algos |
70 | 71 | from pandas.core.arrays import ExtensionArray |
| 72 | +from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype |
71 | 73 | from pandas.core.base import IndexOpsMixin, PandasObject |
72 | 74 | import pandas.core.common as com |
73 | 75 | from pandas.core.indexers import deprecate_ndim_indexing |
@@ -292,54 +294,59 @@ def __new__( |
292 | 294 |
|
293 | 295 | name = maybe_extract_name(name, data, cls) |
294 | 296 |
|
| 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 | + |
295 | 304 | if isinstance(data, ABCPandasArray): |
296 | 305 | # ensure users don't accidentally put a PandasArray in an index. |
297 | 306 | data = data.to_numpy() |
298 | 307 |
|
| 308 | + data_dtype = getattr(data, "dtype", None) |
| 309 | + |
299 | 310 | # range |
300 | 311 | if isinstance(data, RangeIndex): |
301 | 312 | return RangeIndex(start=data, copy=copy, dtype=dtype, name=name) |
302 | 313 | elif isinstance(data, range): |
303 | 314 | return RangeIndex.from_range(data, dtype=dtype, name=name) |
304 | 315 |
|
305 | 316 | # categorical |
306 | | - elif is_categorical_dtype(data) or is_categorical_dtype(dtype): |
| 317 | + elif is_categorical_dtype(data_dtype) or is_categorical_dtype(dtype): |
307 | 318 | # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 |
308 | 319 | from pandas.core.indexes.category import CategoricalIndex |
309 | 320 |
|
310 | 321 | return _maybe_asobject(dtype, CategoricalIndex, data, copy, name, **kwargs) |
311 | 322 |
|
312 | 323 | # interval |
313 | | - elif is_interval_dtype(data) or is_interval_dtype(dtype): |
| 324 | + elif is_interval_dtype(data_dtype) or is_interval_dtype(dtype): |
314 | 325 | # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 |
315 | 326 | from pandas.core.indexes.interval import IntervalIndex |
316 | 327 |
|
317 | 328 | return _maybe_asobject(dtype, IntervalIndex, data, copy, name, **kwargs) |
318 | 329 |
|
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): |
324 | 331 | # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 |
325 | 332 | from pandas import DatetimeIndex |
326 | 333 |
|
327 | 334 | return _maybe_asobject(dtype, DatetimeIndex, data, copy, name, **kwargs) |
328 | 335 |
|
329 | | - elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype): |
| 336 | + elif is_timedelta64_dtype(data_dtype) or is_timedelta64_dtype(dtype): |
330 | 337 | # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 |
331 | 338 | from pandas import TimedeltaIndex |
332 | 339 |
|
333 | 340 | return _maybe_asobject(dtype, TimedeltaIndex, data, copy, name, **kwargs) |
334 | 341 |
|
335 | | - elif is_period_dtype(data) or is_period_dtype(dtype): |
| 342 | + elif is_period_dtype(data_dtype) or is_period_dtype(dtype): |
336 | 343 | # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 |
337 | 344 | from pandas import PeriodIndex |
338 | 345 |
|
339 | 346 | return _maybe_asobject(dtype, PeriodIndex, data, copy, name, **kwargs) |
340 | 347 |
|
341 | 348 | # 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): |
343 | 350 | if not (dtype is None or is_object_dtype(dtype)): |
344 | 351 | # coerce to the provided dtype |
345 | 352 | ea_cls = dtype.construct_array_type() |
|
0 commit comments