Rolling sum with strings

Rolling works only with numbers:

def _prep_values(self, values=None, kill_inf=True):
        if values is None:
            values = getattr(self._selected_obj, 'values', self._selected_obj)
        # GH #12373 : rolling functions error on float32 data
        # make sure the data is coerced to float64
        if is_float_dtype(values.dtype):
            values = ensure_float64(values)
        elif is_integer_dtype(values.dtype):
            values = ensure_float64(values)
        elif needs_i8_conversion(values.dtype):
            raise NotImplementedError...
    ...
    ...

So you should construct it manually. Here is one of the possible variants with simple list comprehensions (maybe there is a more Pandas-ish way exists):

df = pd.DataFrame({'col1':list('some_string')})
pd.Series([
    ''.join(df.col1.values[max(i-2, 0): i+1])
    for i in range(len(df.col1.values))
])
0       s
1      so
2     som
3     ome
4     me_
5     e_s
6     _st
7     str
8     tri
9     rin
10    ing
dtype: object

Using pd.Series.cumsum seems like working (although bit of inefficient):

df['col1'].cumsum().str[-3:]

Output:

0       s
1      so
2     som
3     ome
4     me_
5     e_s
6     _st
7     str
8     tri
9     rin
10    ing
Name: col1, dtype: object

How about shifting the series?

df.col1.shift(2).fillna('') + df.col1.shift().fillna('') + df.col1

Generalizing to any number:

pd.concat([df.col1.shift(i).fillna('') for i in range(3)], axis=1).sum(axis=1)

Tags:

Python

Pandas