-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Default negative location in pandas insert #49496
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
Comments
take |
I think this would be a better API, but it is backward incompatible, unfortunately. Could be done by wrapping the method in a |
Thank you for replying! I don't know how to handle the deprecation cycle you mentioned. If you think that this edit is worthwhile, I can try to implement it (with a bit of guidance) |
If we were to do this, we could do it like this in pandas 2.x: @deprecate_nonkeyword_arguments(version=None, allowed_args=[])
def insert(
self,
loc: int,
column: Hashable,
value: Scalar | AnyArrayLike,
allow_duplicates: bool | lib.NoDefault = lib.no_default,
) -> None:
... Then in pandas 3.0 change the signature to: def insert(
self,
column: Hashable,
value: Scalar | AnyArrayLike,
*,
loc: int = -1,
allow_duplicates: bool | lib.NoDefault = lib.no_default,
) -> None:
... This will somewhat disruptive for the 2.x cycle, so IDK if other core devs would prefer to keep the current signature? |
@pandas-dev/pandas-core. |
I am -1 on this for two reasons:
|
Agreed with @bashtage; I don't see the value of changing this. |
I'm -1 as well. Based on the example, the easy way to accomplish this using >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> df
col1 col2
0 1 3
1 2 4
>>> df.insert(len(df.columns), "newcol", [99, 99])
>>> df
col1 col2 newcol
0 1 3 99
1 2 4 99 |
Ok, there's probably not support for this change, but I'll leave it open a while more, to see if it facilitates more discussion |
I didn't read the OP carefully enough; I would support negative indices for the If we are to do this, then negative indices should behave in the same manner that Python treats them rather than as mentioned in the OP; e.g.
|
Since it appears there was overall negative sentiment about this feature, as well as inactivity, closing |
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 constraint0 <= loc
.Feature Description
In pandas/core/frame.py
insert
definitionAlternative Solutions
I don't see any existing functionality or third-party package that can solve this issue
Additional Context
Usage example after the implementation:
If you find this feature useful I can try to implement it.
The text was updated successfully, but these errors were encountered: