From 1f325ccdd82f71c2ec7ecd66976643deba88d12c Mon Sep 17 00:00:00 2001 From: Bob Haffner Date: Mon, 2 Oct 2017 08:17:24 -0500 Subject: [PATCH] allow neg index on str_get --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/strings.py | 2 +- pandas/tests/test_strings.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index d69a5c22acc03..a5d5dc28e177d 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -668,6 +668,7 @@ Indexing - Bug in ``IntervalIndex`` where performing a scalar lookup fails for included right endpoints of non-overlapping monotonic decreasing indexes (:issue:`16417`, :issue:`17271`) - Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` when no valid entry (:issue:`17400`) - Bug in :func:`Series.rename` when called with a `callable`, incorrectly alters the name of the `Series`, rather than the name of the `Index`. (:issue:`17407`) +- Bug in :func:`String.str_get` raises `index out of range` error instead of inserting NaNs when using a negative index. (:issue:`17704`) I/O ^^^ diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 021f88d1aec00..abef6f6086dbd 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1260,7 +1260,7 @@ def str_get(arr, i): ------- items : Series/Index of objects """ - f = lambda x: x[i] if len(x) > i else np.nan + f = lambda x: x[i] if len(x) > i >= -len(x) else np.nan return _na_map(f, arr) diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index ec2b0b75b9eed..f1b97081b6d93 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -2484,6 +2484,19 @@ def test_get(self): expected = Series([u('b'), u('d'), np.nan, u('g')]) tm.assert_series_equal(result, expected) + # bounds testing + values = Series(['1_2_3_4_5', '6_7_8_9_10', '11_12']) + + # positive index + result = values.str.split('_').str.get(2) + expected = Series(['3', '8', np.nan]) + tm.assert_series_equal(result, expected) + + # negative index + result = values.str.split('_').str.get(-3) + expected = Series(['3', '8', np.nan]) + tm.assert_series_equal(result, expected) + def test_more_contains(self): # PR #1179 s = Series(['A', 'B', 'C', 'Aaba', 'Baca', '', NA,