forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupby_test.py
56 lines (40 loc) · 1.2 KB
/
groupby_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from collections import defaultdict
import numpy as np
from pandas import *
import pandas._tseries as tseries
import pandas.core.groupby as gp
reload(gp)
k = 1000
values = np.random.randn(8 * k)
key1 = np.array(['foo', 'bar', 'baz', 'bar', 'foo', 'baz', 'bar', 'baz'] * k,
dtype=object)
key2 = np.array(['b', 'b', 'b', 'b', 'a', 'a', 'a', 'a' ] * k,
dtype=object)
shape, labels, idicts = gp.labelize(key1, key2)
print tseries.group_labels(key1)
# print shape
# print labels
# print idicts
result = tseries.group_aggregate(values, labels, shape)
print tseries.groupby_indices(key2)
df = DataFrame({'key1' : key1,
'key2' : key2,
'v1' : values,
'v2' : values})
k1 = df['key1']
k2 = df['key2']
# del df['key1']
# del df['key2']
# r2 = gp.multi_groupby(df, np.sum, k1, k2)
# print result
gen = gp.generate_groups(df['v1'], labels, shape, axis=1,
factory=DataFrame)
res = defaultdict(dict)
for a, gen1 in gen:
for b, group in gen1:
print a, b
print group
# res[b][a] = group['values'].sum()
res[b][a] = group.sum()
res = DataFrame(res)
grouped = df.groupby(['key1', 'key2'])