forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupby_test.py
142 lines (105 loc) · 3.22 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from collections import defaultdict
from numpy import nan
import numpy as np
from pandas import *
import pandas.lib as tseries
import pandas.core.groupby as gp
import pandas.util.testing as tm
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'])
"""
# data = {'A' : [0, 0, 0, 0, 1, 1, 1, 1, 1, 1., nan, nan],
# 'B' : ['A', 'B'] * 6,
# 'C' : np.random.randn(12)}
# df = DataFrame(data)
# df['C'][2:10:2] = nan
# single column
# grouped = df.drop(['B'], axis=1).groupby('A')
# exp = {}
# for cat, group in grouped:
# exp[cat] = group['C'].sum()
# exp = DataFrame({'C' : exp})
# result = grouped.sum()
# grouped = df.groupby(['A', 'B'])
# expd = {}
# for cat1, cat2, group in grouped:
# expd.setdefault(cat1, {})[cat2] = group['C'].sum()
# exp = DataFrame(expd).T.stack()
# result = grouped.sum()['C']
# print 'wanted'
# print exp
# print 'got'
# print result
# tm.N = 10000
# mapping = {'A': 0, 'C': 1, 'B': 0, 'D': 1}
# tf = lambda x: x - x.mean()
# df = tm.makeTimeDataFrame()
# ts = df['A']
# # grouped = df.groupby(lambda x: x.strftime('%m/%y'))
# grouped = df.groupby(mapping, axis=1)
# groupedT = df.T.groupby(mapping, axis=0)
# r1 = groupedT.transform(tf).T
# r2 = grouped.transform(tf)
# fillit = lambda x: x.fillna(method='pad')
# f = lambda x: x
# transformed = df.groupby(lambda x: x.strftime('%m/%y')).transform(lambda x: x)
# def ohlc(group):
# return Series([group[0], group.max(), group.min(), group[-1]],
# index=['open', 'high', 'low', 'close'])
# grouper = [lambda x: x.year, lambda x: x.month]
# dr = DateRange('1/1/2000', '1/1/2002')
# ts = Series(np.random.randn(len(dr)), index=dr)
# import string
# k = 20
# n = 1000
# keys = list(string.letters[:k])
# df = DataFrame({'A' : np.tile(keys, n),
# 'B' : np.repeat(keys[:k/2], n * 2),
# 'C' : np.random.randn(k * n)})
# def f():
# for x in df.groupby(['A', 'B']):
# pass
a = np.arange(100).repeat(100)
b = np.tile(np.arange(100), 100)
index = MultiIndex.from_arrays([a, b])
s = Series(np.random.randn(len(index)), index)
df = DataFrame({'A' : s})
df['B'] = df.index.get_level_values(0)
df['C'] = df.index.get_level_values(1)
def f():
for x in df.groupby(['B', 'B']):
pass