Skip to content

REF: remove sanitize_index #40089

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 3 commits into from
Feb 27, 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
18 changes: 18 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Note: pandas.core.common is *not* part of the public API.
"""
from __future__ import annotations

from collections import (
abc,
Expand All @@ -12,6 +13,7 @@
from functools import partial
import inspect
from typing import (
TYPE_CHECKING,
Any,
Callable,
Collection,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a super fan of putting things in common.py but i guess this fits.

"""
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)})"
)
8 changes: 3 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@
ndarray_to_mgr,
nested_data_to_arrays,
reorder_arrays,
sanitize_index,
to_arrays,
treat_as_nested,
)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down