Skip to content

DOC/TST: Clarify Series.str.get supports passing hashable label #47918

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 7 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,15 +996,15 @@ def rpartition(self, sep=" ", expand=True):

def get(self, i):
"""
Extract element from each component at specified position.
Extract element from each component at specified position or with specified key.

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

Parameters
----------
i : int
Position of element to extract.
i : int or hashable dict label
Position or key of element to extract.

Returns
-------
Expand Down Expand Up @@ -1044,6 +1044,15 @@ def get(self, i):
4 NaN
5 None
dtype: object

Return element with given key

>>> s = pd.Series([{"name": "Hello", "value": "World"},
... {"name": "Goodbye", "value": "Planet"}])
>>> s.str.get('name')
0 Hello
1 Goodbye
dtype: object
"""
result = self._data.array._str_get(i)
return self._wrap_result(result)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,20 @@ def test_zfill_with_leading_sign():
value = Series(["-cat", "-1", "+dog"])
expected = Series(["-0cat", "-0001", "+0dog"])
tm.assert_series_equal(value.str.zfill(5), expected)


def test_get_with_dict_label():
# GH47911
s = Series(
[
{"name": "Hello", "value": "World"},
{"name": "Goodbye", "value": "Planet"},
{"value": "Sea"},
]
)
result = s.str.get("name")
expected = Series(["Hello", "Goodbye", None])
tm.assert_series_equal(result, expected)
result = s.str.get("value")
expected = Series(["World", "Planet", "Sea"])
tm.assert_series_equal(result, expected)