Skip to content

DOC: added examples for index assignment using Series #46064

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

Closed
Closed
Changes from 3 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
32 changes: 32 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,38 @@ def loc(self) -> _LocIndexer:
8 4 5
9 7 8

**Set values with a Series**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ok but really not very idiomatic e.g. these should be on the __setitem__ doc-string e.g.
df['shield'] =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is the docstring of .loc, that's why is using this idiom.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure but these are not idiomatic patterns for setting a column; sure if you have a mask then its the idimoatic way, but NOT for a column.

Copy link
Author

@marco-georgaklis marco-georgaklis Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, new contributor here. Where exactly should I be moving this documentation? Is it within this file (indexing.py)?


Setting column values using a Series aligns along the index values of
the Series and DataFrame

>>> df.loc[:, 'shield'] = pd.Series({7: 8, 8: 10, 9: 13})
>>> df
max_speed shield
7 1 8
8 4 10
9 7 13

Where the index values are not found in the Series index, the values
will be NaN

>>> df.loc[:, 'shield'] = pd.Series({8: 10})
>>> df
max_speed shield
7 1 NaN
8 4 10.0
9 7 NaN

Setting value to Series with a missing index leaves the DataFrame
unchanged

>>> df.loc[:, 'shield'] = pd.Series({11: 14})
>>> df
max_speed shield
7 1 NaN
8 4 NaN
9 7 NaN

**Getting values with a MultiIndex**

A number of examples using a DataFrame with a MultiIndex
Expand Down