Skip to content

Commit c8493e3

Browse files
authored
REF: move repeated logic from Manager.insert to DataFrame.insert (#40618)
1 parent 0354620 commit c8493e3

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4298,8 +4298,14 @@ def insert(self, loc, column, value, allow_duplicates: bool = False) -> None:
42984298
"Cannot specify 'allow_duplicates=True' when "
42994299
"'self.flags.allows_duplicate_labels' is False."
43004300
)
4301+
if not allow_duplicates and column in self.columns:
4302+
# Should this be a different kind of error??
4303+
raise ValueError(f"cannot insert {column}, already exists")
4304+
if not isinstance(loc, int):
4305+
raise TypeError("loc must be int")
4306+
43014307
value = self._sanitize_column(value)
4302-
self._mgr.insert(loc, column, value, allow_duplicates=allow_duplicates)
4308+
self._mgr.insert(loc, column, value)
43034309

43044310
def assign(self, **kwargs) -> DataFrame:
43054311
r"""

pandas/core/internals/array_manager.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -904,33 +904,26 @@ def iset(self, loc: Union[int, slice, np.ndarray], value):
904904
self.arrays[mgr_idx] = value_arr
905905
return
906906

907-
def insert(self, loc: int, item: Hashable, value, allow_duplicates: bool = False):
907+
def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None:
908908
"""
909909
Insert item at selected position.
910910
911911
Parameters
912912
----------
913913
loc : int
914914
item : hashable
915-
value : array_like
916-
allow_duplicates: bool
917-
If False, trying to insert non-unique item will raise
918-
915+
value : np.ndarray or ExtensionArray
919916
"""
920-
if not allow_duplicates and item in self.items:
921-
# Should this be a different kind of error??
922-
raise ValueError(f"cannot insert {item}, already exists")
923-
924-
if not isinstance(loc, int):
925-
raise TypeError("loc must be int")
926-
927917
# insert to the axis; this could possibly raise a TypeError
928918
new_axis = self.items.insert(loc, item)
929919

930920
value = extract_array(value, extract_numpy=True)
931921
if value.ndim == 2:
932922
if value.shape[0] == 1:
933-
value = value[0, :]
923+
# error: Invalid index type "Tuple[int, slice]" for
924+
# "Union[Any, ExtensionArray, ndarray]"; expected type
925+
# "Union[int, slice, ndarray]"
926+
value = value[0, :] # type: ignore[index]
934927
else:
935928
raise ValueError(
936929
f"Expected a 1D array, got an array with shape {value.shape}"

pandas/core/internals/managers.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -1208,9 +1208,7 @@ def value_getitem(placement):
12081208
# Newly created block's dtype may already be present.
12091209
self._known_consolidated = False
12101210

1211-
def insert(
1212-
self, loc: int, item: Hashable, value: ArrayLike, allow_duplicates: bool = False
1213-
):
1211+
def insert(self, loc: int, item: Hashable, value: ArrayLike) -> None:
12141212
"""
12151213
Insert item at selected position.
12161214
@@ -1219,17 +1217,7 @@ def insert(
12191217
loc : int
12201218
item : hashable
12211219
value : np.ndarray or ExtensionArray
1222-
allow_duplicates: bool
1223-
If False, trying to insert non-unique item will raise
1224-
12251220
"""
1226-
if not allow_duplicates and item in self.items:
1227-
# Should this be a different kind of error??
1228-
raise ValueError(f"cannot insert {item}, already exists")
1229-
1230-
if not isinstance(loc, int):
1231-
raise TypeError("loc must be int")
1232-
12331221
# insert to the axis; this could possibly raise a TypeError
12341222
new_axis = self.items.insert(loc, item)
12351223

0 commit comments

Comments
 (0)