Skip to content

Commit d5b4b33

Browse files
authored
DOC/TST: Clarify Series.str.get supports passing hashable label (#47918)
* gh 47911 * pre-commit issue * add test and fix doc * modified comment * pep 8 * add more elements
1 parent a6c9aa7 commit d5b4b33

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pandas/core/strings/accessor.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -997,15 +997,15 @@ def rpartition(self, sep=" ", expand=True):
997997

998998
def get(self, i):
999999
"""
1000-
Extract element from each component at specified position.
1000+
Extract element from each component at specified position or with specified key.
10011001
1002-
Extract element from lists, tuples, or strings in each element in the
1002+
Extract element from lists, tuples, dict, or strings in each element in the
10031003
Series/Index.
10041004
10051005
Parameters
10061006
----------
1007-
i : int
1008-
Position of element to extract.
1007+
i : int or hashable dict label
1008+
Position or key of element to extract.
10091009
10101010
Returns
10111011
-------
@@ -1045,6 +1045,15 @@ def get(self, i):
10451045
4 NaN
10461046
5 None
10471047
dtype: object
1048+
1049+
Return element with given key
1050+
1051+
>>> s = pd.Series([{"name": "Hello", "value": "World"},
1052+
... {"name": "Goodbye", "value": "Planet"}])
1053+
>>> s.str.get('name')
1054+
0 Hello
1055+
1 Goodbye
1056+
dtype: object
10481057
"""
10491058
result = self._data.array._str_get(i)
10501059
return self._wrap_result(result)

pandas/tests/strings/test_strings.py

+17
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,20 @@ def test_zfill_with_leading_sign():
828828
value = Series(["-cat", "-1", "+dog"])
829829
expected = Series(["-0cat", "-0001", "+0dog"])
830830
tm.assert_series_equal(value.str.zfill(5), expected)
831+
832+
833+
def test_get_with_dict_label():
834+
# GH47911
835+
s = Series(
836+
[
837+
{"name": "Hello", "value": "World"},
838+
{"name": "Goodbye", "value": "Planet"},
839+
{"value": "Sea"},
840+
]
841+
)
842+
result = s.str.get("name")
843+
expected = Series(["Hello", "Goodbye", None])
844+
tm.assert_series_equal(result, expected)
845+
result = s.str.get("value")
846+
expected = Series(["World", "Planet", "Sea"])
847+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)