Skip to content

REF: move repeated logic from Manager.insert to DataFrame.insert #40618

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
Mar 25, 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
8 changes: 7 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
19 changes: 6 additions & 13 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,33 +904,26 @@ 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.

Parameters
----------
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}"
Expand Down
14 changes: 1 addition & 13 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)

Expand Down