Skip to content

API: Series.__setitem__ with Series value #51386

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

Open
jbrockmendel opened this issue Feb 14, 2023 · 5 comments
Open

API: Series.__setitem__ with Series value #51386

jbrockmendel opened this issue Feb 14, 2023 · 5 comments
Labels
Bug Deprecate Functionality to remove in pandas

Comments

@jbrockmendel
Copy link
Member

jbrockmendel commented Feb 14, 2023

orig = pd.Series(range(10))

key = pd.Series([6, 7])

ser = orig.copy()
ser[key._values] = key * 2
>>> ser
0     0
1     1
2     2
3     3
4     4
5     5
6    12
7    14
8     8
9     9
dtype: int64

ser = orig.copy()
ser.loc[key._values] = key * 2
>>> ser
0    0.0
1    1.0
2    2.0
3    3.0
4    4.0
5    5.0
6    NaN
7    NaN
8    8.0
9    9.0
dtype: float64

In .loc we do a .reindex on the value Series, but not in Series.__setitem__ (which goes through Series._set_values to mgr.setitem) using .iloc gets the __setitem__ behavior.

Maybe I just need some coffee, but it isn't obvious to me what the expected behavior is here cc @phofl

@jbrockmendel jbrockmendel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 14, 2023
@phofl
Copy link
Member

phofl commented Feb 14, 2023

Loc is correct I think, they should align by your index (at least in the DataFrame case when setting values). If there is no reason to treat Series differently, then I'd keep the loc behavior

@jbrockmendel
Copy link
Member Author

So you’d deprecate the series set item behavior or keep all three as-is?

@phofl
Copy link
Member

phofl commented Feb 14, 2023

If there is no reason that I am not aware of to keep the series set item behavior, I’d deprecate it, yes

@phofl phofl added the Deprecate Functionality to remove in pandas label Feb 16, 2023
@jbrockmendel
Copy link
Member Author

Possible duplicate of #37516?

@topper-123
Copy link
Contributor

I agree doing it the same as in loc.

A slightly related issue is that neither the doc string, see section "Setting values" nor the docs, see below the paragraph "Note that setting works as well:" explain that setting using series uses reindexing, it can catch some users off guard.

@topper-123 topper-123 removed the Needs Triage Issue that has not been reviewed by a pandas team member label May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Deprecate Functionality to remove in pandas
Projects
None yet
Development

No branches or pull requests

3 participants