์๋ ํ์ธ์,
๊ธฐ๋ณธ ์ง๊ณ ๋ฐ ๋ถ์ ์ํ ์ค์ด๋ฉฐ ์ด์ํ ๋ฒ๊ทธ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ค์์ reprex์
๋๋ค.
import pandas as pd
import numpy as np
idx2=[pd.to_datetime('2016-08-31 22:08:12.000') ,
pd.to_datetime('2016-08-31 22:09:12.200'),
pd.to_datetime('2016-08-31 22:20:12.400')]
test=pd.DataFrame({'quant':[1.0,1.0,3.0],
'quant2':[1.0,1.0,3.0],
'time2':[pd.to_datetime('2016-08-31 22:08:12.000') ,
pd.to_datetime('2016-08-31 22:09:12.200'),
pd.to_datetime('2016-08-31 22:20:12.400')]},
index=idx2)
test.reset_index(inplace = True)
test
Out[22]:
index quant quant2 time2
0 2016-08-31 22:08:12.000 1.0 1.0 2016-08-31 22:08:12.000
1 2016-08-31 22:09:12.200 1.0 1.0 2016-08-31 22:09:12.200
2 2016-08-31 22:20:12.400 3.0 3.0 2016-08-31 22:20:12.400
df= test.groupby(pd.Grouper(key='time2', freq='1T', closed = 'left', label = 'left'),as_index = False).agg(
{'quant' : 'sum',
'quant2' : 'sum'})
์ค๋ค
File "<ipython-input-20-c09863316397>", line 19, in <module>
'quant2' : 'sum'})
File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 4036, in aggregate
return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 3491, in aggregate
self._insert_inaxis_grouper_inplace(result)
File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 4090, in _insert_inaxis_grouper_inplace
self.grouper.get_group_levels(),
File "C:\\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 1911, in get_group_levels
if not self.compressed and len(self.groupings) == 1:
AttributeError: 'BinGrouper' object has no attribute 'compressed'
์์๋๋ ์ผ์
๋๊น? as_index = False
์์์ด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
@randomgambit : ์ ๊ณ ํด ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! ์ํ๊น๊ฒ๋ df
์ด ๋ฌด์์ธ์ง ์ง์ ํ์ง ์์๊ธฐ ๋๋ฌธ์์ด๋ฅผ ๋ณต์ ํ ์ ์์ต๋๋ค. ์ํ ์ฝ๋์ ์ ๊ณต ํ ์ ์์ต๋๊น?
@randomgambit : ๊ฐ์ฌํฉ๋๋ค! ํด๋น ์ฝ๋๋ฅผ ์ด๊ธฐ ๋ฌธ์ ๋ณด๊ณ ์๋ก ์ฎ๊ฒจ ์ฃผ์๊ฒ ์ต๋๊น? ๊ทธ๋ ๊ฒํ๋ฉด ์ฐ๋ฆฌ๊ฐ ํ ๋์๋ณด๊ธฐ์๋ ๋ ์ฝ๊ฒ ์ฝ์ ์ ์์ต๋๋ค.
@gfyoung ์ด๋ค ์์ด๋์ด?
cc @jreback
TimeGrouper๋ฅผ ์ฌ์ฉํ ๋๋ as_index
๊ฐ ์ง์๋์ง ์์ต๋๋ค.
๊ทํ์ ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
test.resample('1T', on='time2').sum()
๋๋ ์ด๊ฒ์ด ํจ๊ณผ๊ฐ ์๋ค๊ณ ์๊ฐํ๋ค.
๋๊ตฐ๊ฐ dibs๋ฅผ ํธ์ถ ํ ์ง 3 ๋
์ด ์ง๋ฌ์ง ๋ง ๋น ๋ฅธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ํ๋ค๋ฉด ์ง๊ณ ํ .reset_index()
ํ์ฌ ์ธ๋ฑ์ค col์ ์์ฒด ์ด์ ํ๋ก์ ์
ํ ์์์์ต๋๋ค.
df.groupby(pd.Grouper(key='date_col', freq='1d')).count().reset_index()
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@randomgambit : ๊ฐ์ฌํฉ๋๋ค! ํด๋น ์ฝ๋๋ฅผ ์ด๊ธฐ ๋ฌธ์ ๋ณด๊ณ ์๋ก ์ฎ๊ฒจ ์ฃผ์๊ฒ ์ต๋๊น? ๊ทธ๋ ๊ฒํ๋ฉด ์ฐ๋ฆฌ๊ฐ ํ ๋์๋ณด๊ธฐ์๋ ๋ ์ฝ๊ฒ ์ฝ์ ์ ์์ต๋๋ค.