Skip to content

rolling agg raise error #27601

@ytcpub

Description

@ytcpub

Code Sample, a copy-pastable example if possible

df = pd.DataFrame(
{'code':['1', '1', '1', '3'], 'desc':['hello', 'my name is', 'rock', 'bye']},
index=pd.date_range('2018-01-01', periods=4, freq='1s')
)
df.groupby(['code']).rolling('2s').agg({'desc': 'count'})

df.groupby(['code']).rolling('2s').agg({'desc': ' '.join})
df.groupby(['code']).agg({'desc': ' '.join})

#### Problem description i want a function like rolling with time and group concat string rows but when use rolling('2s').agg({'desc': ' '.join}) , it raise error, I reference https://github.com/pandas-dev/pandas/issues/20773, but that code not work, not the value but direct raise exception: TypeError: cannot handle this type -> object error log tail: d:\program\python37\lib\site-packages\pandas\core\window.py in apply(self, func, raw, args, kwargs) 1701 def apply(self, func, raw=None, args=(), kwargs={}): 1702 return super(Rolling, self).apply( -> 1703 func, raw=raw, args=args, kwargs=kwargs) 1704 1705 @Substitution(name='rolling') d:\program\python37\lib\site-packages\pandas\core\window.py in apply(self, func, raw, args, kwargs) 1010 1011 return self._apply(f, func, args=args, kwargs=kwargs, -> 1012 center=False, raw=raw) 1013 1014 def sum(self, *args, **kwargs): d:\program\python37\lib\site-packages\pandas\core\window.py in _apply(self, func, name, window, center, check_minp, **kwargs) 839 results = [] 840 for b in blocks: --> 841 values = self._prep_values(b.values) 842 843 if values.size == 0: d:\program\python37\lib\site-packages\pandas\core\window.py in _prep_values(self, values, kill_inf) 212 except (ValueError, TypeError): 213 raise TypeError("cannot handle this type -> {0}" --> 214 "".format(values.dtype)) 215 216 if kill_inf: TypeError: cannot handle this type -> object #### Expected Output code desc 2018-01-01 00:00:00 1 hello 2018-01-01 00:00:01 1 hello my name is 2018-01-01 00:00:02 1 my name is rock 2018-01-01 00:00:03 3 bye #### Output of ``pd.show_versions()`` INSTALLED VERSIONS ------------------ commit: None python: 3.7.2.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 158 Stepping 12, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.24.2 pytest: None pip: 19.0.3 setuptools: 40.6.2 Cython: None numpy: 1.16.2 scipy: 1.2.1 pyarrow: None xarray: None IPython: 7.3.0 sphinx: None patsy: None dateutil: 2.8.0 pytz: 2018.9 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 3.0.3 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml.etree: 4.3.2 bs4: 4.7.1 html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.10.1 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None gcsfs: None 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions