def test_dict_compat(): data_datetime64 = { np.datetime64("1990-03-15"): 1, np.datetime64("2015-03-15"): 2 } data_unchanged = {1: 2, 3: 4, 5: 6} expected = {Timestamp("1990-3-15"): 1, Timestamp("2015-03-15"): 2} assert dict_compat(data_datetime64) == expected assert dict_compat(expected) == expected assert dict_compat(data_unchanged) == data_unchanged
def _homogenize(data, index, dtype: Optional[DtypeObj]): oindex = None homogenized = [] for val in data: if isinstance(val, ABCSeries): if dtype is not None: val = val.astype(dtype) if val.index is not index: # Forces alignment. No need to copy data since we # are putting it into an ndarray later val = val.reindex(index, copy=False) else: if isinstance(val, dict): if oindex is None: oindex = index.astype("O") if isinstance(index, (ABCDatetimeIndex, ABCTimedeltaIndex)): val = dict_compat(val) else: val = dict(val) val = lib.fast_multiget(val, oindex._values, default=np.nan) val = sanitize_array( val, index, dtype=dtype, copy=False, raise_cast_failure=False ) homogenized.append(val) return homogenized
def _homogenize(data, index: Index, dtype: Optional[DtypeObj]): oindex = None homogenized = [] for val in data: if isinstance(val, ABCSeries): if dtype is not None: val = val.astype(dtype) if val.index is not index: # Forces alignment. No need to copy data since we # are putting it into an ndarray later val = val.reindex(index, copy=False) # TODO extract_array should be preferred, but that gives failures for # `extension/test_numpy.py` (extract_array will convert numpy arrays # to PandasArray), see https://github.com/pandas-dev/pandas/issues/40021 # val = extract_array(val, extract_numpy=True) val = val._values else: if isinstance(val, dict): if oindex is None: oindex = index.astype("O") if isinstance(index, (ABCDatetimeIndex, ABCTimedeltaIndex)): val = dict_compat(val) else: val = dict(val) val = lib.fast_multiget(val, oindex._values, default=np.nan) val = sanitize_array( val, index, dtype=dtype, copy=False, raise_cast_failure=False ) homogenized.append(val) return homogenized
def _homogenize(data, index: Index, dtype: DtypeObj | None) -> list[ArrayLike]: oindex = None homogenized = [] for val in data: if isinstance(val, ABCSeries): if dtype is not None: val = val.astype(dtype, copy=False) if val.index is not index: # Forces alignment. No need to copy data since we # are putting it into an ndarray later val = val.reindex(index, copy=False) 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) val = sanitize_array(val, index, dtype=dtype, copy=False, raise_cast_failure=False) homogenized.append(val) return homogenized