Skip to content

Commit 80387ae

Browse files
authored
REF: de-duplicate object-dtype handling (#32168)
1 parent 00e8e4a commit 80387ae

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

pandas/core/indexes/base.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,7 @@ def __new__(
311311
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
312312
from pandas.core.indexes.interval import IntervalIndex
313313

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)
322315

323316
elif (
324317
is_datetime64_any_dtype(data)
@@ -328,39 +321,19 @@ def __new__(
328321
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
329322
from pandas import DatetimeIndex
330323

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)
343325

344326
elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype):
345327
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
346328
from pandas import TimedeltaIndex
347329

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)
356331

357332
elif is_period_dtype(data) or is_period_dtype(dtype):
358333
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
359334
from pandas import PeriodIndex
360335

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)
364337

365338
# extension dtype
366339
elif is_extension_array_dtype(data) or is_extension_array_dtype(dtype):
@@ -5775,3 +5748,40 @@ def _try_convert_to_int_array(
57755748
pass
57765749

57775750
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

Comments
 (0)