Skip to content

Series.str[...] should maybe default to NA if some list/tuple does not contain enough values #3223

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

Closed
wesm opened this issue Mar 30, 2013 · 2 comments
Assignees
Milestone

Comments

@wesm
Copy link
Member

wesm commented Mar 30, 2013

No description provided.

@ghost ghost assigned wesm Mar 30, 2013
@wesm
Copy link
Member Author

wesm commented Apr 8, 2013

Anyone have an opinion on this? Here is an example:

In [8]: s = Series([(1, 2), (1,), (3,4,5)])

In [9]: s.str[0]
Out[9]: 
0    1
1    1
2    3
dtype: int64

In [10]: s.str[1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-946f1b211e37> in <module>()
----> 1 s.str[1]

/Users/wesm/code/pandas/pandas/core/strings.pyc in __getitem__(self, key)
    660                               step=key.step)
    661         else:
--> 662             return self.get(key)
    663 
    664     def _wrap_result(self, result):

/Users/wesm/code/pandas/pandas/core/strings.pyc in get(self, i)
    678     @copy(str_get)
    679     def get(self, i):
--> 680         result = str_get(self.series, i)
    681         return self._wrap_result(result)
    682 

/Users/wesm/code/pandas/pandas/core/strings.pyc in str_get(arr, i)
    561     """
    562     f = lambda x: x[i]
--> 563     return _na_map(f, arr)
    564 
    565 

/Users/wesm/code/pandas/pandas/core/strings.pyc in _na_map(f, arr, na_result)
     87 def _na_map(f, arr, na_result=np.nan):
     88     # should really _check_ for NA
---> 89     return _map(f, arr, na_mask=True, na_value=na_result)
     90 
     91 

/Users/wesm/code/pandas/pandas/core/strings.pyc in _map(f, arr, na_mask, na_value)
     96         mask = isnull(arr)
     97         try:
---> 98             result = lib.map_infer_mask(arr, f, mask.view(np.uint8))
     99         except (TypeError, AttributeError):
    100             def g(x):

/Users/wesm/code/pandas/pandas/lib.so in pandas.lib.map_infer_mask (pandas/lib.c:41222)()

/Users/wesm/code/pandas/pandas/core/strings.pyc in <lambda>(x)
    560     items : array
    561     """
--> 562     f = lambda x: x[i]
    563     return _na_map(f, arr)
    564 

IndexError: tuple index out of range

@jreback
Copy link
Contributor

jreback commented Apr 8, 2013

I agree, should default to return na, as you are viewing the Series effectiviely like a Frame here

also note that this is another case of the above (just slightly different exception)

In [6]: s = pd.Series(["foo","b","bar"])

In [7]: s
Out[7]: 
0    foo
1      b
2    bar

In [8]: s.str[0]
Out[8]: 
0    f
1    b
2    b

In [9]: s.str[1]

IndexError: string index out of range

@wesm wesm closed this as completed in 2c81846 Apr 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants