Skip to content

API: alignment behaviour of Series as assignment value in indexing #37516

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
jorisvandenbossche opened this issue Oct 30, 2020 · 2 comments
Open
Labels
API - Consistency Internal Consistency of API/Behavior Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@jorisvandenbossche
Copy link
Member

Follow up on #37427

Currently, there is an inconsistency between __getitem__ and loc related to how the RHS value in an assignment gets re-aligned or not.

In this example, the values in Series get just assigned as is, ignoring the index of the series (only requiring that the length matches the number of values in the indexer):

s = pd.Series(range(5))
idx = np.array([1, 4]) 

>>> s[idx] = Series([10, 11])   
>>> s   
0     0
1    10
2     2
3     3
4    11
dtype: int64

However, with loc we have a different behaviour: the Series gets basically aligned with s first, and then this realigned series is also indexed with idx, and those values get set:

s = pd.Series(range(5))
idx = np.array([1, 4]) 

>>> s.loc[idx] = Series([10, 11]) 
>>> s 
0     0.0
1    11.0
2     2.0
3     3.0
4     NaN
dtype: float64

you could write what basically happens more explicitly as:

s.loc[idx] = Series([10, 11]).reindex(s.index)[idx] 

To have the same behaviour as __getitem__, you need to assign with an array instead of series:

s = pd.Series(range(5))
idx = np.array([1, 4]) 

>>> s.loc[idx] = Series([10, 11]).values    
>>> s 
0     0
1    10
2     2
3     3
4    11
dtype: int64
@jorisvandenbossche jorisvandenbossche added Indexing Related to indexing on series/frames, not to indexes themselves API Design labels Oct 30, 2020
@jbrockmendel
Copy link
Member

cc @phofl i think this has a bearing on one of the topics you're working on

@phofl
Copy link
Member

phofl commented Nov 28, 2020

@jbrockmendel not really working on this right now. The underlying cause is the same as in #30439. Have to define how to move forward and if this is expected or a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants