Repository: pandas-dev/pandas

Issue:
String methods get() does not insert NaNs on negative index out of range
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
df1 = pd.DataFrame([["1_2_3_4_5"], ["6_7_8_9_10"], ["11_12"]], columns=["test"])

# access using positive index
df1["test"].str.split("_").str[2]
# output:
# 0        3
# 1        8
# 2      NaN

# access using negative index
df1["test"].str.split("_").str[-3]
# output:
# IndexError: list index out of range
```
#### Problem description

Since accessing an array using a negative index is supported if all items in the Series are the correct length for it, and if you access using a positive index that is out of range for any item in the series instead outputs a NaN for that item, I would think that the behaviour should be unified and that access using a negative index also outputs NaNs if an item in the Series is shorter than expected.

#### Expected Output

```python
df1["test"].str.split("_").str[-3]
# output:
# 0        3
# 1        8
# 2      NaN
```

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]
INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-132-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.2.7
Cython: None
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: 1.1.14
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
Thx for the report. Yeah we should fix it to check negative boundary. PR is welcome.

https://github.com/pandas-dev/pandas/blob/f6d4d7078d49503adf990f0c159eb603ca1f0c1a/pandas/core/strings.py#L1263
I can submit a PR for this

Resolving patch:
diff --git a/pandas/core/strings.py b/pandas/core/strings.py
--- 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)
 
 


Test patch:
diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py
--- 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,