diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8d0c8e5f29413..32439af6db238 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4298,8 +4298,14 @@ def insert(self, loc, column, value, allow_duplicates: bool = False) -> None: "Cannot specify 'allow_duplicates=True' when " "'self.flags.allows_duplicate_labels' is False." ) + if not allow_duplicates and column in self.columns: + # Should this be a different kind of error?? + raise ValueError(f"cannot insert {column}, already exists") + if not isinstance(loc, int): + raise TypeError("loc must be int") + value = self._sanitize_column(value) - self._mgr.insert(loc, column, value, allow_duplicates=allow_duplicates) + self._mgr.insert(loc, column, value) def assign(self, **kwargs) -> DataFrame: r""" diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py index 99a1706c671b1..950d229c45f9e 100644 --- a/pandas/core/internals/array_manager.py +++ b/pandas/core/internals/array_manager.py @@ -904,7 +904,7 @@ def iset(self, loc: Union[int, slice, np.ndarray], value): self.arrays[mgr_idx] = value_arr return - def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False): + def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None: """ Insert item at selected position. @@ -912,25 +912,18 @@ def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False ---------- loc : int item : hashable - value : array_like - allow_duplicates: bool - If False, trying to insert non-unique item will raise - + value : np.ndarray or ExtensionArray """ - if not allow_duplicates and item in self.items: - # Should this be a different kind of error?? - raise ValueError(f"cannot insert {item}, already exists") - - if not isinstance(loc, int): - raise TypeError("loc must be int") - # insert to the axis; this could possibly raise a TypeError new_axis = self.items.insert(loc, item) value = extract_array(value, extract_numpy=True) if value.ndim == 2: if value.shape[0] == 1: - value = value[0, :] + # error: Invalid index type "Tuple[int, slice]" for + # "Union[Any, ExtensionArray, ndarray]"; expected type + # "Union[int, slice, ndarray]" + value = value[0, :] # type: ignore[index] else: raise ValueError( f"Expected a 1D array, got an array with shape {value.shape}" diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index e4cce731b7b56..14fa994631623 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1208,9 +1208,7 @@ def value_getitem(placement): # Newly created block's dtype may already be present. self._known_consolidated = False - def insert( - self, loc: int, item: Hashable, value: ArrayLike, allow_duplicates: bool = False - ): + def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None: """ Insert item at selected position. @@ -1219,17 +1217,7 @@ def insert( loc : int item : hashable value : np.ndarray or ExtensionArray - allow_duplicates: bool - If False, trying to insert non-unique item will raise - """ - if not allow_duplicates and item in self.items: - # Should this be a different kind of error?? - raise ValueError(f"cannot insert {item}, already exists") - - if not isinstance(loc, int): - raise TypeError("loc must be int") - # insert to the axis; this could possibly raise a TypeError new_axis = self.items.insert(loc, item)