Skip to content

Commit 4060498

Browse files
authored
REF: remove sanitize_index (#40089)
1 parent 47c6d16 commit 4060498

File tree

4 files changed

+22
-33
lines changed

4 files changed

+22
-33
lines changed

pandas/core/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
Note: pandas.core.common is *not* part of the public API.
55
"""
6+
from __future__ import annotations
67

78
from collections import (
89
abc,
@@ -12,6 +13,7 @@
1213
from functools import partial
1314
import inspect
1415
from typing import (
16+
TYPE_CHECKING,
1517
Any,
1618
Callable,
1719
Collection,
@@ -51,6 +53,9 @@
5153
from pandas.core.dtypes.inference import iterable_not_string
5254
from pandas.core.dtypes.missing import isna
5355

56+
if TYPE_CHECKING:
57+
from pandas import Index
58+
5459

5560
class SettingWithCopyError(ValueError):
5661
pass
@@ -512,3 +517,16 @@ def temp_setattr(obj, attr: str, value) -> Iterator[None]:
512517
setattr(obj, attr, value)
513518
yield obj
514519
setattr(obj, attr, old_value)
520+
521+
522+
def require_length_match(data, index: Index):
523+
"""
524+
Check the length of data matches the length of the index.
525+
"""
526+
if len(data) != len(index):
527+
raise ValueError(
528+
"Length of values "
529+
f"({len(data)}) "
530+
"does not match length of index "
531+
f"({len(index)})"
532+
)

pandas/core/frame.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@
183183
ndarray_to_mgr,
184184
nested_data_to_arrays,
185185
reorder_arrays,
186-
sanitize_index,
187186
to_arrays,
188187
treat_as_nested,
189188
)
@@ -4207,15 +4206,14 @@ def _sanitize_column(self, value) -> ArrayLike:
42074206
value = _reindex_for_setitem(value, self.index)
42084207

42094208
elif isinstance(value, ExtensionArray):
4210-
# Explicitly copy here, instead of in sanitize_index,
4211-
# as sanitize_index won't copy an EA, even with copy=True
4209+
# Explicitly copy here
42124210
value = value.copy()
4213-
value = sanitize_index(value, self.index)
4211+
com.require_length_match(value, self.index)
42144212

42154213
elif is_sequence(value):
4214+
com.require_length_match(value, self.index)
42164215

42174216
# turn me into an ndarray
4218-
value = sanitize_index(value, self.index)
42194217
if not isinstance(value, (np.ndarray, Index)):
42204218
if isinstance(value, list) and len(value) > 0:
42214219
value = maybe_convert_platform(value)

pandas/core/internals/construction.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
maybe_convert_platform,
3636
maybe_infer_to_datetimelike,
3737
maybe_upcast,
38-
sanitize_to_nanoseconds,
3938
)
4039
from pandas.core.dtypes.common import (
4140
is_datetime64tz_dtype,
@@ -810,28 +809,3 @@ def convert(arr):
810809
arrays = [convert(arr) for arr in content]
811810

812811
return arrays
813-
814-
815-
# ---------------------------------------------------------------------
816-
# Series-Based
817-
818-
819-
def sanitize_index(data, index: Index):
820-
"""
821-
Sanitize an index type to return an ndarray of the underlying, pass
822-
through a non-Index.
823-
"""
824-
if len(data) != len(index):
825-
raise ValueError(
826-
"Length of values "
827-
f"({len(data)}) "
828-
"does not match length of index "
829-
f"({len(index)})"
830-
)
831-
832-
if isinstance(data, np.ndarray):
833-
834-
# coerce datetimelike types to ns
835-
data = sanitize_to_nanoseconds(data)
836-
837-
return data

pandas/core/series.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@
126126
from pandas.core.indexes.timedeltas import TimedeltaIndex
127127
from pandas.core.indexing import check_bool_indexer
128128
from pandas.core.internals import SingleBlockManager
129-
from pandas.core.internals.construction import sanitize_index
130129
from pandas.core.shared_docs import _shared_docs
131130
from pandas.core.sorting import (
132131
ensure_key_mapped,
@@ -388,7 +387,7 @@ def __init__(
388387
data = [data]
389388
index = ibase.default_index(len(data))
390389
elif is_list_like(data):
391-
sanitize_index(data, index)
390+
com.require_length_match(data, index)
392391

393392
# create/copy the manager
394393
if isinstance(data, SingleBlockManager):

0 commit comments

Comments
 (0)