Skip to content

DOC: Updated the docstring of pandas.Series.str.get #20667

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

Merged
merged 3 commits into from
Apr 24, 2018
Merged
Changes from 1 commit
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
21 changes: 20 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,17 +1651,36 @@ def str_translate(arr, table, deletechars=None):

def str_get(arr, i):
"""
Extract element from each component at specified position.

Extract element from lists, tuples, or strings in each element in the
Series/Index.

Parameters
----------
i : int
Integer index (location)
Position of element to extract.

Returns
-------
items : Series/Index of objects

Examples
--------
>>> s = pd.Series(["String", (1, 2, 3), ["a", "b", "c"], 123])
>>> s
0 String
1 (1, 2, 3)
2 [a, b, c]
3 123
dtype: object

>>> s.str.get(1)
0 t
1 2
2 b
3 NaN
dtype: object
Copy link
Member

Choose a reason for hiding this comment

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

Looks good. May be we could a dictionary to the Series too, and an example indexing by a negative number. I'm not sure, as the example could be too complex for the simplicity of the function. But it'd illustrate the different behaviors.

It'd also help to have an explanation before the example, saying something like "When the value is not an iterable, or a dict where the index is not in the keys, str.get returns NaN".

If you do it you may encounter this bug: #20671. I'd wait until it's fixed, otherwise you'd have to use an index that is a key in the dictionary, and not illustrate the case when the index is not a key, which raises a KeyError right now.

"""
f = lambda x: x[i] if len(x) > i >= -len(x) else np.nan
return _na_map(f, arr)
Expand Down