Skip to content

Commit 2c81846

Browse files
committed
ENH: silently return NA in String.str.get when values are too short. close #3223
1 parent 8879c85 commit 2c81846

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pandas 0.11.0
129129
- Treat boolean values as integers (values 1 and 0) for numeric
130130
operations. (GH2641_)
131131
- Add ``time()`` method to DatetimeIndex (GH3180_)
132+
- Return NA when using Series.str[...] for values that are not long enough
133+
(GH3223_)
132134

133135
**API Changes**
134136

@@ -382,6 +384,7 @@ pandas 0.11.0
382384
.. _GH3216: https://github.com/pydata/pandas/issues/3216
383385
.. _GH3222: https://github.com/pydata/pandas/issues/3222
384386
.. _GH2641: https://github.com/pydata/pandas/issues/2641
387+
.. _GH3223: https://github.com/pydata/pandas/issues/3223
385388
.. _GH3238: https://github.com/pydata/pandas/issues/3238
386389
.. _GH3258: https://github.com/pydata/pandas/issues/3258
387390
.. _GH3283: https://github.com/pydata/pandas/issues/3283

pandas/core/strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def str_get(arr, i):
559559
-------
560560
items : array
561561
"""
562-
f = lambda x: x[i]
562+
f = lambda x: x[i] if len(x) > i else np.nan
563563
return _na_map(f, arr)
564564

565565

pandas/tests/test_strings.py

+13
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,19 @@ def test_string_slice_get_syntax(self):
754754
expected = s.str.slice(stop=3)
755755
assert_series_equal(result, expected)
756756

757+
def test_string_slice_out_of_bounds(self):
758+
s = Series([(1, 2), (1,), (3,4,5)])
759+
760+
result = s.str[1]
761+
expected = Series([2, np.nan, 4])
762+
763+
assert_series_equal(result, expected)
764+
765+
s = Series(['foo', 'b', 'ba'])
766+
result = s.str[1]
767+
expected = Series(['o', np.nan, 'a'])
768+
assert_series_equal(result, expected)
769+
757770
def test_match_findall_flags(self):
758771
data = {'Dave': '[email protected]', 'Steve': '[email protected]',
759772
'Rob': '[email protected]', 'Wes': np.nan}

0 commit comments

Comments
 (0)