Skip to content

REF: move np_can_hold_element to mgr method #45453

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
Jan 19, 2022
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: 5 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
)

from pandas.core.dtypes.cast import (
can_hold_element,
construct_1d_arraylike_from_scalar,
construct_2d_arraylike_from_scalar,
find_common_type,
Expand Down Expand Up @@ -3882,18 +3881,11 @@ def _set_value(

series = self._get_item_cache(col)
loc = self.index.get_loc(index)
dtype = series.dtype
if isinstance(dtype, np.dtype) and dtype.kind not in ["m", "M"]:
# otherwise we have EA values, and this check will be done
# via setitem_inplace
if not can_hold_element(series._values, value):
# We'll go through loc and end up casting.
raise TypeError

series._mgr.setitem_inplace(loc, value)
# Note: trying to use series._set_value breaks tests in
# tests.frame.indexing.test_indexing and tests.indexing.test_partial
except (KeyError, TypeError):

# series._set_value will do validation that may raise TypeError
# or ValueError
series._set_value(loc, value, takeable=True)
except (KeyError, TypeError, ValueError):
# set using a non-recursive method & reset the cache
if takeable:
self.iloc[index, col] = value
Expand Down
17 changes: 15 additions & 2 deletions pandas/core/internals/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
final,
)

import numpy as np

from pandas._typing import (
ArrayLike,
DtypeObj,
Shape,
)
from pandas.errors import AbstractMethodError

from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.cast import (
find_common_type,
np_can_hold_element,
)

from pandas.core.base import PandasObject
from pandas.core.indexes.api import (
Expand Down Expand Up @@ -171,7 +176,15 @@ def setitem_inplace(self, indexer, value) -> None:
in place, not returning a new Manager (and Block), and thus never changing
the dtype.
"""
self.array[indexer] = value
arr = self.array

# EAs will do this validation in their own __setitem__ methods.
if isinstance(arr, np.ndarray):
# Note: checking for ndarray instead of np.dtype means we exclude
# dt64/td64, which do their own validation.
value = np_can_hold_element(arr.dtype, value)

arr[indexer] = value

def grouped_reduce(self, func, ignore_failures: bool = False):
"""
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
)

from pandas.core.dtypes.cast import (
can_hold_element,
convert_dtypes,
maybe_box_native,
maybe_cast_pointwise_result,
Expand Down Expand Up @@ -1151,12 +1150,6 @@ def __setitem__(self, key, value) -> None:

def _set_with_engine(self, key, value) -> None:
loc = self.index.get_loc(key)
dtype = self.dtype
if isinstance(dtype, np.dtype) and dtype.kind not in ["m", "M"]:
# otherwise we have EA values, and this check will be done
# via setitem_inplace
if not can_hold_element(self._values, value):
raise ValueError

# this is equivalent to self._values[key] = value
self._mgr.setitem_inplace(loc, value)
Expand Down