@@ -311,14 +311,7 @@ def __new__(
311
311
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
312
312
from pandas .core .indexes .interval import IntervalIndex
313
313
314
- closed = kwargs .pop ("closed" , None )
315
- if is_dtype_equal (_o_dtype , dtype ):
316
- return IntervalIndex (
317
- data , name = name , copy = copy , closed = closed , ** kwargs
318
- ).astype (object )
319
- return IntervalIndex (
320
- data , dtype = dtype , name = name , copy = copy , closed = closed , ** kwargs
321
- )
314
+ return _maybe_asobject (dtype , IntervalIndex , data , copy , name , ** kwargs )
322
315
323
316
elif (
324
317
is_datetime64_any_dtype (data )
@@ -328,39 +321,19 @@ def __new__(
328
321
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
329
322
from pandas import DatetimeIndex
330
323
331
- if is_dtype_equal (_o_dtype , dtype ):
332
- # GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
333
- # will raise in the where `data` is already tz-aware. So
334
- # we leave it out of this step and cast to object-dtype after
335
- # the DatetimeIndex construction.
336
- # Note we can pass copy=False because the .astype below
337
- # will always make a copy
338
- return DatetimeIndex (data , copy = False , name = name , ** kwargs ).astype (
339
- object
340
- )
341
- else :
342
- return DatetimeIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
324
+ return _maybe_asobject (dtype , DatetimeIndex , data , copy , name , ** kwargs )
343
325
344
326
elif is_timedelta64_dtype (data ) or is_timedelta64_dtype (dtype ):
345
327
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
346
328
from pandas import TimedeltaIndex
347
329
348
- if is_dtype_equal (_o_dtype , dtype ):
349
- # Note we can pass copy=False because the .astype below
350
- # will always make a copy
351
- return TimedeltaIndex (data , copy = False , name = name , ** kwargs ).astype (
352
- object
353
- )
354
- else :
355
- return TimedeltaIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
330
+ return _maybe_asobject (dtype , TimedeltaIndex , data , copy , name , ** kwargs )
356
331
357
332
elif is_period_dtype (data ) or is_period_dtype (dtype ):
358
333
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
359
334
from pandas import PeriodIndex
360
335
361
- if is_dtype_equal (_o_dtype , dtype ):
362
- return PeriodIndex (data , copy = False , name = name , ** kwargs ).astype (object )
363
- return PeriodIndex (data , dtype = dtype , copy = copy , name = name , ** kwargs )
336
+ return _maybe_asobject (dtype , PeriodIndex , data , copy , name , ** kwargs )
364
337
365
338
# extension dtype
366
339
elif is_extension_array_dtype (data ) or is_extension_array_dtype (dtype ):
@@ -5775,3 +5748,40 @@ def _try_convert_to_int_array(
5775
5748
pass
5776
5749
5777
5750
raise ValueError
5751
+
5752
+
5753
+ def _maybe_asobject (dtype , klass , data , copy : bool , name : Label , ** kwargs ):
5754
+ """
5755
+ If an object dtype was specified, create the non-object Index
5756
+ and then convert it to object.
5757
+
5758
+ Parameters
5759
+ ----------
5760
+ dtype : np.dtype, ExtensionDtype, str
5761
+ klass : Index subclass
5762
+ data : list-like
5763
+ copy : bool
5764
+ name : hashable
5765
+ **kwargs
5766
+
5767
+ Returns
5768
+ -------
5769
+ Index
5770
+
5771
+ Notes
5772
+ -----
5773
+ We assume that calling .astype(object) on this klass will make a copy.
5774
+ """
5775
+
5776
+ # GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
5777
+ # will raise in the where `data` is already tz-aware. So
5778
+ # we leave it out of this step and cast to object-dtype after
5779
+ # the DatetimeIndex construction.
5780
+
5781
+ if is_dtype_equal (_o_dtype , dtype ):
5782
+ # Note we can pass copy=False because the .astype below
5783
+ # will always make a copy
5784
+ index = klass (data , copy = False , name = name , ** kwargs )
5785
+ return index .astype (object )
5786
+
5787
+ return klass (data , dtype = dtype , copy = copy , name = name , ** kwargs )
0 commit comments