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 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Other enhancements
- :class:`Series` reducers (e.g. ``min``, ``max``, ``sum``, ``mean``) will now successfully operate when the dtype is numeric and ``numeric_only=True`` is provided; previously this would raise a ``NotImplementedError`` (:issue:`47500`)
- :meth:`RangeIndex.union` now can return a :class:`RangeIndex` instead of a :class:`Int64Index` if the resulting values are equally spaced (:issue:`47557`, :issue:`43885`)
- :meth:`DataFrame.compare` now accepts an argument ``result_names`` to allow the user to specify the result's names of both left and right DataFrame which are being compared. This is by default ``'self'`` and ``'other'`` (:issue:`44354`)
- :meth:`Series.str.get` now raises when ``i`` is not a integer (:issue:`47911`)
Copy link
Member

Choose a reason for hiding this comment

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

Could you specify the error?


.. ---------------------------------------------------------------------------
.. _whatsnew_150.notable_bug_fixes:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,9 @@ def get(self, i):
5 None
dtype: object
"""
if not is_integer(i):
msg = f"i must be of integer type, not {type(i).__name__}"
raise TypeError(msg)
result = self._data.array._str_get(i)
return self._wrap_result(result)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,21 @@ 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)


@pytest.mark.parametrize("arg", [{1: 1}, [1, 2], (1, 2), "0"])
def test_get_with_non_integer_argument(arg):
# GH47911
s = Series(
[
"String",
(1, 2, 3),
["a", "b", "c"],
123,
-456,
{1: "Hello", "2": "World"}
]
)
msg = f"i must be of integer type, not {type(arg).__name__}"
with pytest.raises(TypeError, match=msg):
s.str.get(arg)