Skip to content

Commit a8b313f

Browse files
authored
REF: de-duplicate nested-dict handling in DataFrame construction (#41785)
1 parent e186e18 commit a8b313f

File tree

5 files changed

+5
-68
lines changed

5 files changed

+5
-68
lines changed

pandas/_libs/lib.pyi

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ def is_float_array(values: np.ndarray, skipna: bool = False): ...
5555
def is_integer_array(values: np.ndarray, skipna: bool = False): ...
5656
def is_bool_array(values: np.ndarray, skipna: bool = False): ...
5757

58-
def fast_multiget(mapping: dict, keys: np.ndarray, default=np.nan) -> np.ndarray: ...
59-
6058
def fast_unique_multiple_list_gen(gen: Generator, sort: bool = True) -> list: ...
6159
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list: ...
6260
def fast_unique_multiple(arrays: list, sort: bool = True) -> list: ...

pandas/_libs/lib.pyx

-22
Original file line numberDiff line numberDiff line change
@@ -2899,25 +2899,3 @@ def to_object_array_tuples(rows: object) -> np.ndarray:
28992899
result[i, j] = row[j]
29002900

29012901
return result
2902-
2903-
2904-
@cython.wraparound(False)
2905-
@cython.boundscheck(False)
2906-
def fast_multiget(dict mapping, ndarray keys, default=np.nan) -> np.ndarray:
2907-
cdef:
2908-
Py_ssize_t i, n = len(keys)
2909-
object val
2910-
ndarray[object] output = np.empty(n, dtype='O')
2911-
2912-
if n == 0:
2913-
# kludge, for Series
2914-
return np.empty(0, dtype='f8')
2915-
2916-
for i in range(n):
2917-
val = keys[i]
2918-
if val in mapping:
2919-
output[i] = mapping[val]
2920-
else:
2921-
output[i] = default
2922-
2923-
return maybe_convert_objects(output)

pandas/core/dtypes/cast.py

-16
Original file line numberDiff line numberDiff line change
@@ -780,22 +780,6 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
780780
return dtype, val
781781

782782

783-
def dict_compat(d: dict[Scalar, Scalar]) -> dict[Scalar, Scalar]:
784-
"""
785-
Convert datetimelike-keyed dicts to a Timestamp-keyed dict.
786-
787-
Parameters
788-
----------
789-
d: dict-like object
790-
791-
Returns
792-
-------
793-
dict
794-
795-
"""
796-
return {maybe_box_datetimelike(key): value for key, value in d.items()}
797-
798-
799783
def infer_dtype_from_array(
800784
arr, pandas_dtype: bool = False
801785
) -> tuple[DtypeObj, ArrayLike]:

pandas/core/internals/construction.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from pandas.core.dtypes.cast import (
2828
construct_1d_arraylike_from_scalar,
2929
construct_1d_ndarray_preserving_na,
30-
dict_compat,
3130
maybe_cast_to_datetime,
3231
maybe_convert_platform,
3332
maybe_infer_to_datetimelike,
@@ -61,16 +60,15 @@
6160
TimedeltaArray,
6261
)
6362
from pandas.core.construction import (
63+
create_series_with_explicit_dtype,
6464
ensure_wrapped_if_datetimelike,
6565
extract_array,
6666
range_to_ndarray,
6767
sanitize_array,
6868
)
6969
from pandas.core.indexes import base as ibase
7070
from pandas.core.indexes.api import (
71-
DatetimeIndex,
7271
Index,
73-
TimedeltaIndex,
7472
ensure_index,
7573
get_objs_combined_axis,
7674
union_indexes,
@@ -566,7 +564,6 @@ def convert(v):
566564

567565

568566
def _homogenize(data, index: Index, dtype: DtypeObj | None) -> list[ArrayLike]:
569-
oindex = None
570567
homogenized = []
571568

572569
for val in data:
@@ -581,16 +578,10 @@ def _homogenize(data, index: Index, dtype: DtypeObj | None) -> list[ArrayLike]:
581578
val = val._values
582579
else:
583580
if isinstance(val, dict):
584-
if oindex is None:
585-
oindex = index.astype("O")
586-
587-
if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
588-
# see test_constructor_dict_datetime64_index
589-
val = dict_compat(val)
590-
else:
591-
# see test_constructor_subclass_dict
592-
val = dict(val)
593-
val = lib.fast_multiget(val, oindex._values, default=np.nan)
581+
# see test_constructor_subclass_dict
582+
# test_constructor_dict_datetime64_index
583+
val = create_series_with_explicit_dtype(val, index=index)._values
584+
594585
val = sanitize_array(
595586
val, index, dtype=dtype, copy=False, raise_cast_failure=False
596587
)

pandas/tests/dtypes/cast/test_dict_compat.py

-14
This file was deleted.

0 commit comments

Comments
 (0)