Skip to content

REF: de-duplicate nested-dict handling in DataFrame construction #41785

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 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ def is_float_array(values: np.ndarray, skipna: bool = False): ...
def is_integer_array(values: np.ndarray, skipna: bool = False): ...
def is_bool_array(values: np.ndarray, skipna: bool = False): ...

def fast_multiget(mapping: dict, keys: np.ndarray, default=np.nan) -> np.ndarray: ...

def fast_unique_multiple_list_gen(gen: Generator, sort: bool = True) -> list: ...
def fast_unique_multiple_list(lists: list, sort: bool = True) -> list: ...
def fast_unique_multiple(arrays: list, sort: bool = True) -> list: ...
Expand Down
22 changes: 0 additions & 22 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2841,25 +2841,3 @@ def to_object_array_tuples(rows: object) -> np.ndarray:
result[i, j] = row[j]

return result


@cython.wraparound(False)
@cython.boundscheck(False)
def fast_multiget(dict mapping, ndarray keys, default=np.nan) -> np.ndarray:
cdef:
Py_ssize_t i, n = len(keys)
object val
ndarray[object] output = np.empty(n, dtype='O')

if n == 0:
# kludge, for Series
return np.empty(0, dtype='f8')

for i in range(n):
val = keys[i]
if val in mapping:
output[i] = mapping[val]
else:
output[i] = default

return maybe_convert_objects(output)
16 changes: 0 additions & 16 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,22 +780,6 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
return dtype, val


def dict_compat(d: dict[Scalar, Scalar]) -> dict[Scalar, Scalar]:
"""
Convert datetimelike-keyed dicts to a Timestamp-keyed dict.

Parameters
----------
d: dict-like object

Returns
-------
dict

"""
return {maybe_box_datetimelike(key): value for key, value in d.items()}


def infer_dtype_from_array(
arr, pandas_dtype: bool = False
) -> tuple[DtypeObj, ArrayLike]:
Expand Down
19 changes: 5 additions & 14 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from pandas.core.dtypes.cast import (
construct_1d_arraylike_from_scalar,
construct_1d_ndarray_preserving_na,
dict_compat,
maybe_cast_to_datetime,
maybe_convert_platform,
maybe_infer_to_datetimelike,
Expand Down Expand Up @@ -61,16 +60,15 @@
TimedeltaArray,
)
from pandas.core.construction import (
create_series_with_explicit_dtype,
ensure_wrapped_if_datetimelike,
extract_array,
range_to_ndarray,
sanitize_array,
)
from pandas.core.indexes import base as ibase
from pandas.core.indexes.api import (
DatetimeIndex,
Index,
TimedeltaIndex,
ensure_index,
get_objs_combined_axis,
union_indexes,
Expand Down Expand Up @@ -566,7 +564,6 @@ def convert(v):


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

for val in data:
Expand All @@ -581,16 +578,10 @@ def _homogenize(data, index: Index, dtype: DtypeObj | None) -> list[ArrayLike]:
val = val._values
else:
if isinstance(val, dict):
if oindex is None:
oindex = index.astype("O")

if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
# see test_constructor_dict_datetime64_index
val = dict_compat(val)
else:
# see test_constructor_subclass_dict
val = dict(val)
val = lib.fast_multiget(val, oindex._values, default=np.nan)
# see test_constructor_subclass_dict
# test_constructor_dict_datetime64_index
val = create_series_with_explicit_dtype(val, index=index)._values

val = sanitize_array(
val, index, dtype=dtype, copy=False, raise_cast_failure=False
)
Expand Down
14 changes: 0 additions & 14 deletions pandas/tests/dtypes/cast/test_dict_compat.py

This file was deleted.