Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
In dataframe insert why not change the loc argument to a keyword argument with a default value of -1 ? Since in many cases I don't care where the column will be positioned my idea is that if the loc argument has a negative value (as in the maybe future default case) the column will be inserted as the last column of the dataset, i.e. if loc<0: loc=len(columns)
. Then is also possible to remove the constraint 0 <= loc
.
Feature Description
In pandas/core/frame.py insert
definition
def insert(
self,
column: Hashable,
value: Scalar | AnyArrayLike,
loc: int = -1,
allow_duplicates: bool | lib.NoDefault = lib.no_default,
) -> None:
...
if not isinstance(loc, int):
raise TypeError("loc must be int")
if loc < 0:
loc = len(self.columns)
value = self._sanitize_column(value)
...
Alternative Solutions
I don't see any existing functionality or third-party package that can solve this issue
Additional Context
Usage example after the implementation:
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> df
col1 col2
0 1 3
1 2 4
>>> df.insert("newcol", [99, 99])
>>> df
col1 col2 newcol
0 1 3 99
1 2 4 99
If you find this feature useful I can try to implement it.