ENH: add StringMethods (.str accessor) to Index, fixes #9068 by mortada · Pull Request #9667 · pandas-dev/pandas (original) (raw)

@jorisvandenbossche the return type is Index unless the dtype is bool, in which case the return type will be np.array.

if the method returns multiple elements it will be an Index of lists, and won't be a DataFrame. If the user specifies return_type='frame' it will raise. Here's an example:

In [1]: from pandas import Index
In [2]: idx = Index(['a b c', 'd e', 'f'])

In [3]: idx.str.split()
Out[3]: Index([['a', 'b', 'c'], ['d', 'e'], ['f']], dtype='object')

In [4]: idx.str.split(return_type='frame')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d22b631e0f0f> in <module>()
----> 1 idx.str.split(return_type='frame')

/Users/mortada_mehyar/code/github/pandas/pandas/core/strings.py in split(self, pat, n, return_type)
    987     @copy(str_split)
    988     def split(self, pat=None, n=-1, return_type='series'):
--> 989         result = str_split(self.series, pat, n=n, return_type=return_type)
    990         return self._wrap_result(result)
    991

/Users/mortada_mehyar/code/github/pandas/pandas/core/strings.py in str_split(arr, pat, n, return_type)
    652         raise ValueError("return_type must be {'series', 'frame'}")
    653     if return_type == 'frame' and isinstance(arr, Index):
--> 654         raise ValueError("return_type='frame' is not supported for string "
    655                          "methods on Index")
    656     if pat is None:

ValueError: return_type='frame' is not supported for string methods on Index