forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategoricals.py
91 lines (64 loc) · 2.23 KB
/
categoricals.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
from .pandas_vb_common import *
try:
from pandas.types.concat import union_categoricals
except ImportError:
pass
import string
class concat_categorical(object):
goal_time = 0.2
def setup(self):
self.s = pd.Series((list('aabbcd') * 1000000)).astype('category')
def time_concat_categorical(self):
concat([self.s, self.s])
class union_categorical(object):
goal_time = 0.2
def setup(self):
self.a = pd.Categorical((list('aabbcd') * 1000000))
self.b = pd.Categorical((list('bbcdjk') * 1000000))
def time_union_categorical(self):
union_categoricals([self.a, self.b])
class categorical_value_counts(object):
goal_time = 1
def setup(self):
n = 500000
np.random.seed(2718281)
arr = ['s%04d' % i for i in np.random.randint(0, n // 10, size=n)]
self.ts = Series(arr).astype('category')
def time_value_counts(self):
self.ts.value_counts(dropna=False)
def time_value_counts_dropna(self):
self.ts.value_counts(dropna=True)
class categorical_constructor(object):
goal_time = 0.2
def setup(self):
n = 5
N = 1e6
self.categories = list(string.ascii_letters[:n])
self.cat_idx = Index(self.categories)
self.values = np.tile(self.categories, N)
self.codes = np.tile(range(n), N)
def time_regular_constructor(self):
Categorical(self.values, self.categories)
def time_fastpath(self):
Categorical(self.codes, self.cat_idx, fastpath=True)
class categorical_constructor_with_datetimes(object):
goal_time = 0.2
def setup(self):
self.datetimes = pd.Series(pd.date_range(
'1995-01-01 00:00:00', periods=10000, freq='s'))
def time_datetimes(self):
Categorical(self.datetimes)
def time_datetimes_with_nat(self):
t = self.datetimes
t.iloc[-1] = pd.NaT
Categorical(t)
class categorical_rendering(object):
goal_time = 3e-3
def setup(self):
n = 1000
items = [str(i) for i in range(n)]
s = pd.Series(items, dtype='category')
df = pd.DataFrame({'C': s, 'data': np.random.randn(n)})
self.data = df[df.C == '20']
def time_rendering(self):
str(self.data.C)