Skip to content

REF: de-duplicate object-dtype handling #32168

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

Merged
merged 3 commits into from
Feb 22, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,7 @@ def __new__(
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
from pandas.core.indexes.interval import IntervalIndex

closed = kwargs.pop("closed", None)
if is_dtype_equal(_o_dtype, dtype):
return IntervalIndex(
data, name=name, copy=copy, closed=closed, **kwargs
).astype(object)
return IntervalIndex(
data, dtype=dtype, name=name, copy=copy, closed=closed, **kwargs
)
return _maybe_asobject(dtype, IntervalIndex, data, copy, name, **kwargs)

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

if is_dtype_equal(_o_dtype, dtype):
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
# will raise in the where `data` is already tz-aware. So
# we leave it out of this step and cast to object-dtype after
# the DatetimeIndex construction.
# Note we can pass copy=False because the .astype below
# will always make a copy
return DatetimeIndex(data, copy=False, name=name, **kwargs).astype(
object
)
else:
return DatetimeIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
return _maybe_asobject(dtype, DatetimeIndex, data, copy, name, **kwargs)

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

if is_dtype_equal(_o_dtype, dtype):
# Note we can pass copy=False because the .astype below
# will always make a copy
return TimedeltaIndex(data, copy=False, name=name, **kwargs).astype(
object
)
else:
return TimedeltaIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
return _maybe_asobject(dtype, TimedeltaIndex, data, copy, name, **kwargs)

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

if is_dtype_equal(_o_dtype, dtype):
return PeriodIndex(data, copy=False, name=name, **kwargs).astype(object)
return PeriodIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
return _maybe_asobject(dtype, PeriodIndex, data, copy, name, **kwargs)

# extension dtype
elif is_extension_array_dtype(data) or is_extension_array_dtype(dtype):
Expand Down Expand Up @@ -5765,3 +5738,40 @@ def _try_convert_to_int_array(
pass

raise ValueError


def _maybe_asobject(dtype, klass, data, copy: bool, name: Label, **kwargs):
"""
If and object dtype was specified, create the non-object Index
and then convert it to object.

Parameters
----------
dtype : np.dtype, ExtensionDtype, str
klass : Index subclass
data : list-like
copy : bool
name : hashable
**kwargs

Returns
-------
Index

Notes
-----
We assume that calling .astype(object) on this klass will make a copy.
"""

# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
# will raise in the where `data` is already tz-aware. So
# we leave it out of this step and cast to object-dtype after
# the DatetimeIndex construction.

if is_dtype_equal(_o_dtype, dtype):
# Note we can pass copy=False because the .astype below
# will always make a copy
index = klass(data, copy=False, name=name, **kwargs)
return index.astype(object)

return klass(data, dtype=dtype, copy=copy, name=name, **kwargs)