<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dat = pd.DataFrame(
    {'number': [2, 2, 3], 'string': ['a', 'a', 'b']},
    index=pd.date_range('2018-01-01', periods=3, freq='1s')
)
dat.rolling('2min').apply(lambda x: len(np.unique(x)))
                      number	string
2018-01-01 00:00:00	1.0	a
2018-01-01 00:00:01	1.0	a
2018-01-01 00:00:02	2.0	b"><pre class="notranslate"><code class="notranslate">dat = pd.DataFrame(
    {'number': [2, 2, 3], 'string': ['a', 'a', 'b']},
    index=pd.date_range('2018-01-01', periods=3, freq='1s')
)
dat.rolling('2min').apply(lambda x: len(np.unique(x)))
                      number	string
2018-01-01 00:00:00	1.0	a
2018-01-01 00:00:01	1.0	a
2018-01-01 00:00:02	2.0	b
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">What I am trying to do is counting how many unique values in a rolling window. This works well for the numeric column <em>dat.number</em> but the string column <em>dat.string</em> simply only returns what it was.</p>
<p dir="auto">In the above example, I expect to see the two columns in the output are the same as the number of unique values are 1, 1, 2 starting from the first row. However the string column returns a, a, b.</p>
<h4 dir="auto">Expected Output</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="                    number	string
2018-01-01 00:00:00	1.0	1.0
2018-01-01 00:00:01	1.0	1.0
2018-01-01 00:00:02	2.0	2.0"><pre class="notranslate"><code class="notranslate">                    number	string
2018-01-01 00:00:00	1.0	1.0
2018-01-01 00:00:01	1.0	1.0
2018-01-01 00:00:02	2.0	2.0
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.5.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.13.0-38-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: en_AU.UTF-8<br>
LOCALE: en_AU.UTF-8</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: None<br>
pip: 9.0.3<br>
setuptools: 39.0.1<br>
Cython: None<br>
numpy: 1.14.2<br>
scipy: 1.0.1<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.3.0<br>
sphinx: 1.7.2<br>
patsy: 0.5.0<br>
dateutil: 2.7.2<br>
pytz: 2018.3<br>
blosc: None<br>
bottleneck: None<br>
tables: 3.4.2<br>
numexpr: 2.6.4<br>
feather: None<br>
matplotlib: 2.2.2<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: 1.0.1<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">Hi the <code class="notranslate">Pandas</code> dream team.</p>
<p dir="auto">I think it would be nice if <code class="notranslate">rolling</code> could accept <code class="notranslate">strings</code> as well (see <a href="https://stackoverflow.com/questions/52657429/rolling-with-string-variables" rel="nofollow">https://stackoverflow.com/questions/52657429/rolling-with-string-variables</a>)</p>
<p dir="auto">With the abundance of textual data nowadays, we want <code class="notranslate">Pandas</code> to stay at the top of the curve!</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd
import numpy as np

df = pd.DataFrame({'mytime' : [pd.to_datetime('2018-01-01 14:34:12.340'),
                             pd.to_datetime('2018-01-01 14:34:13.0'),
                             pd.to_datetime('2018-01-01 14:34:15.342'),
                             pd.to_datetime('2018-01-01 14:34:16.42'),
                             pd.to_datetime('2018-01-01 14:34:28.742')],
                    'myvalue' : [1,2,np.NaN,3,1],
                    'mychart' : ['a','b','c','d','e']})

df.set_index('mytime', inplace = True)

df
Out[15]: 
                        mychart  myvalue
mytime                                  
2018-01-01 14:34:12.340       a      1.0
2018-01-01 14:34:13.000       b      2.0
2018-01-01 14:34:15.342       c      NaN
2018-01-01 14:34:16.420       d      3.0
2018-01-01 14:34:28.742       e      1.0"><pre class="notranslate"><code class="notranslate">import pandas as pd
import numpy as np

df = pd.DataFrame({'mytime' : [pd.to_datetime('2018-01-01 14:34:12.340'),
                             pd.to_datetime('2018-01-01 14:34:13.0'),
                             pd.to_datetime('2018-01-01 14:34:15.342'),
                             pd.to_datetime('2018-01-01 14:34:16.42'),
                             pd.to_datetime('2018-01-01 14:34:28.742')],
                    'myvalue' : [1,2,np.NaN,3,1],
                    'mychart' : ['a','b','c','d','e']})

df.set_index('mytime', inplace = True)

df
Out[15]: 
                        mychart  myvalue
mytime                                  
2018-01-01 14:34:12.340       a      1.0
2018-01-01 14:34:13.000       b      2.0
2018-01-01 14:34:15.342       c      NaN
2018-01-01 14:34:16.420       d      3.0
2018-01-01 14:34:28.742       e      1.0
</code></pre></div>
<p dir="auto">Here I want to concatenate the strings in mychart using the values in the last 2 seconds (not the last two observations).</p>
<p dir="auto">Unfortunately, both attempts below fail miserably</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
df.mychart.rolling(window = '2s', closed = 'right').apply(lambda x: ' '.join(x), raw = False)
df.mychart.rolling(window = '2s', closed = 'right').apply(lambda x: (x + ' ').cumsum(), raw = False)

TypeError: cannot handle this type -&gt; object"><pre class="notranslate"><code class="notranslate">
df.mychart.rolling(window = '2s', closed = 'right').apply(lambda x: ' '.join(x), raw = False)
df.mychart.rolling(window = '2s', closed = 'right').apply(lambda x: (x + ' ').cumsum(), raw = False)

TypeError: cannot handle this type -&gt; object
</code></pre></div>
<p dir="auto">What do you think?<br>
Thanks!</p>