Skip to content

Commit 2f8b7ea

Browse files
jbrockmendelluckyvs1
authored andcommitted
REF: simplify Index.__new__ (pandas-dev#38910)
1 parent abeec80 commit 2f8b7ea

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

pandas/core/indexes/base.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
TYPE_CHECKING,
77
Any,
88
Callable,
9+
Dict,
910
FrozenSet,
1011
Hashable,
1112
List,
@@ -131,6 +132,11 @@
131132
_Identity = NewType("_Identity", object)
132133

133134

135+
def disallow_kwargs(kwargs: Dict[str, Any]):
136+
if kwargs:
137+
raise TypeError(f"Unexpected keyword arguments {repr(set(kwargs))}")
138+
139+
134140
def _new_Index(cls, d):
135141
"""
136142
This is called upon unpickling, rather than the default which doesn't
@@ -296,32 +302,29 @@ def __new__(
296302
return result.astype(dtype, copy=False)
297303
return result
298304

299-
if is_ea_or_datetimelike_dtype(dtype):
305+
elif is_ea_or_datetimelike_dtype(dtype):
300306
# non-EA dtype indexes have special casting logic, so we punt here
301307
klass = cls._dtype_to_subclass(dtype)
302308
if klass is not Index:
303309
return klass(data, dtype=dtype, copy=copy, name=name, **kwargs)
304310

305-
if is_ea_or_datetimelike_dtype(data_dtype):
311+
ea_cls = dtype.construct_array_type()
312+
data = ea_cls._from_sequence(data, dtype=dtype, copy=copy)
313+
data = np.asarray(data, dtype=object)
314+
disallow_kwargs(kwargs)
315+
return Index._simple_new(data, name=name)
316+
317+
elif is_ea_or_datetimelike_dtype(data_dtype):
306318
klass = cls._dtype_to_subclass(data_dtype)
307319
if klass is not Index:
308320
result = klass(data, copy=copy, name=name, **kwargs)
309321
if dtype is not None:
310322
return result.astype(dtype, copy=False)
311323
return result
312324

313-
# extension dtype
314-
if is_extension_array_dtype(data_dtype) or is_extension_array_dtype(dtype):
315-
if not (dtype is None or is_object_dtype(dtype)):
316-
# coerce to the provided dtype
317-
ea_cls = dtype.construct_array_type()
318-
data = ea_cls._from_sequence(data, dtype=dtype, copy=False)
319-
else:
320-
data = np.asarray(data, dtype=object)
321-
322-
# coerce to the object dtype
323-
data = data.astype(object)
324-
return Index(data, dtype=object, copy=copy, name=name, **kwargs)
325+
data = np.array(data, dtype=object, copy=copy)
326+
disallow_kwargs(kwargs)
327+
return Index._simple_new(data, name=name)
325328

326329
# index-like
327330
elif isinstance(data, (np.ndarray, Index, ABCSeries)):
@@ -333,7 +336,7 @@ def __new__(
333336
# should not be coerced
334337
# GH 11836
335338
data = _maybe_cast_with_dtype(data, dtype, copy)
336-
dtype = data.dtype # TODO: maybe not for object?
339+
dtype = data.dtype
337340

338341
if data.dtype.kind in ["i", "u", "f"]:
339342
# maybe coerce to a sub-class
@@ -342,16 +345,15 @@ def __new__(
342345
arr = com.asarray_tuplesafe(data, dtype=object)
343346

344347
if dtype is None:
345-
new_data = _maybe_cast_data_without_dtype(arr)
346-
new_dtype = new_data.dtype
347-
return cls(
348-
new_data, dtype=new_dtype, copy=copy, name=name, **kwargs
349-
)
348+
arr = _maybe_cast_data_without_dtype(arr)
349+
dtype = arr.dtype
350+
351+
if kwargs:
352+
return cls(arr, dtype, copy=copy, name=name, **kwargs)
350353

351354
klass = cls._dtype_to_subclass(arr.dtype)
352355
arr = klass._ensure_array(arr, dtype, copy)
353-
if kwargs:
354-
raise TypeError(f"Unexpected keyword arguments {repr(set(kwargs))}")
356+
disallow_kwargs(kwargs)
355357
return klass._simple_new(arr, name)
356358

357359
elif is_scalar(data):

pandas/core/indexes/datetimelike.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def _convert_arr_indexer(self, keyarr):
623623
return com.asarray_tuplesafe(keyarr)
624624

625625

626-
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
626+
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
627627
"""
628628
Mixin class for methods shared by DatetimeIndex and TimedeltaIndex,
629629
but not PeriodIndex
@@ -816,11 +816,7 @@ def _union(self, other, sort):
816816
i8self = Int64Index._simple_new(self.asi8)
817817
i8other = Int64Index._simple_new(other.asi8)
818818
i8result = i8self._union(i8other, sort=sort)
819-
# pandas\core\indexes\datetimelike.py:887: error: Unexpected
820-
# keyword argument "freq" for "DatetimeTimedeltaMixin" [call-arg]
821-
result = type(self)(
822-
i8result, dtype=self.dtype, freq="infer" # type: ignore[call-arg]
823-
)
819+
result = type(self)(i8result, dtype=self.dtype, freq="infer")
824820
return result
825821

826822
# --------------------------------------------------------------------

0 commit comments

Comments
 (0)