diff --git a/pandas/core/common.py b/pandas/core/common.py index 8625c5063382f..871f5ac651cce 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -3,6 +3,7 @@ Note: pandas.core.common is *not* part of the public API. """ +from __future__ import annotations from collections import ( abc, @@ -12,6 +13,7 @@ from functools import partial import inspect from typing import ( + TYPE_CHECKING, Any, Callable, Collection, @@ -51,6 +53,9 @@ from pandas.core.dtypes.inference import iterable_not_string from pandas.core.dtypes.missing import isna +if TYPE_CHECKING: + from pandas import Index + class SettingWithCopyError(ValueError): pass @@ -512,3 +517,16 @@ def temp_setattr(obj, attr: str, value) -> Iterator[None]: setattr(obj, attr, value) yield obj setattr(obj, attr, old_value) + + +def require_length_match(data, index: Index): + """ + Check the length of data matches the length of the index. + """ + if len(data) != len(index): + raise ValueError( + "Length of values " + f"({len(data)}) " + "does not match length of index " + f"({len(index)})" + ) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 469783913dc42..cf985ba621cfb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -183,7 +183,6 @@ ndarray_to_mgr, nested_data_to_arrays, reorder_arrays, - sanitize_index, to_arrays, treat_as_nested, ) @@ -4024,15 +4023,14 @@ def _sanitize_column(self, value) -> ArrayLike: value = _reindex_for_setitem(value, self.index) elif isinstance(value, ExtensionArray): - # Explicitly copy here, instead of in sanitize_index, - # as sanitize_index won't copy an EA, even with copy=True + # Explicitly copy here value = value.copy() - value = sanitize_index(value, self.index) + com.require_length_match(value, self.index) elif is_sequence(value): + com.require_length_match(value, self.index) # turn me into an ndarray - value = sanitize_index(value, self.index) if not isinstance(value, (np.ndarray, Index)): if isinstance(value, list) and len(value) > 0: value = maybe_convert_platform(value) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 20536c7a94695..0f1fe77c6bb3a 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -36,7 +36,6 @@ maybe_convert_platform, maybe_infer_to_datetimelike, maybe_upcast, - sanitize_to_nanoseconds, ) from pandas.core.dtypes.common import ( is_datetime64tz_dtype, @@ -810,28 +809,3 @@ def convert(arr): arrays = [convert(arr) for arr in content] return arrays - - -# --------------------------------------------------------------------- -# Series-Based - - -def sanitize_index(data, index: Index): - """ - Sanitize an index type to return an ndarray of the underlying, pass - through a non-Index. - """ - if len(data) != len(index): - raise ValueError( - "Length of values " - f"({len(data)}) " - "does not match length of index " - f"({len(index)})" - ) - - if isinstance(data, np.ndarray): - - # coerce datetimelike types to ns - data = sanitize_to_nanoseconds(data) - - return data diff --git a/pandas/core/series.py b/pandas/core/series.py index 5fece72ccddca..58c9f116e011a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -126,7 +126,6 @@ from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.indexing import check_bool_indexer from pandas.core.internals import SingleBlockManager -from pandas.core.internals.construction import sanitize_index from pandas.core.shared_docs import _shared_docs from pandas.core.sorting import ( ensure_key_mapped, @@ -388,7 +387,7 @@ def __init__( data = [data] index = ibase.default_index(len(data)) elif is_list_like(data): - sanitize_index(data, index) + com.require_length_match(data, index) # create/copy the manager if isinstance(data, SingleBlockManager):