File: categoricals.py

package info (click to toggle)
pandas 0.23.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 167,704 kB
  • sloc: python: 230,826; ansic: 11,317; sh: 682; makefile: 133
file content (195 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import warnings

import numpy as np
import pandas as pd
import pandas.util.testing as tm
try:
    from pandas.api.types import union_categoricals
except ImportError:
    try:
        from pandas.types.concat import union_categoricals
    except ImportError:
        pass

from .pandas_vb_common import setup # noqa


class Concat(object):

    goal_time = 0.2

    def setup(self):
        N = 10**5
        self.s = pd.Series(list('aabbcd') * N).astype('category')

        self.a = pd.Categorical(list('aabbcd') * N)
        self.b = pd.Categorical(list('bbcdjk') * N)

    def time_concat(self):
        pd.concat([self.s, self.s])

    def time_union(self):
        union_categoricals([self.a, self.b])


class Constructor(object):

    goal_time = 0.2

    def setup(self):
        N = 10**5
        self.categories = list('abcde')
        self.cat_idx = pd.Index(self.categories)
        self.values = np.tile(self.categories, N)
        self.codes = np.tile(range(len(self.categories)), N)

        self.datetimes = pd.Series(pd.date_range('1995-01-01 00:00:00',
                                                 periods=N / 10,
                                                 freq='s'))
        self.datetimes_with_nat = self.datetimes.copy()
        self.datetimes_with_nat.iloc[-1] = pd.NaT

        self.values_some_nan = list(np.tile(self.categories + [np.nan], N))
        self.values_all_nan = [np.nan] * len(self.values)
        self.values_all_int8 = np.ones(N, 'int8')

    def time_regular(self):
        pd.Categorical(self.values, self.categories)

    def time_fastpath(self):
        pd.Categorical(self.codes, self.cat_idx, fastpath=True)

    def time_datetimes(self):
        pd.Categorical(self.datetimes)

    def time_datetimes_with_nat(self):
        pd.Categorical(self.datetimes_with_nat)

    def time_with_nan(self):
        pd.Categorical(self.values_some_nan)

    def time_all_nan(self):
        pd.Categorical(self.values_all_nan)

    def time_from_codes_all_int8(self):
        pd.Categorical.from_codes(self.values_all_int8, self.categories)


class ValueCounts(object):

    goal_time = 0.2

    params = [True, False]
    param_names = ['dropna']

    def setup(self, dropna):
        n = 5 * 10**5
        arr = ['s%04d' % i for i in np.random.randint(0, n // 10, size=n)]
        self.ts = pd.Series(arr).astype('category')

    def time_value_counts(self, dropna):
        self.ts.value_counts(dropna=dropna)


class Repr(object):

    goal_time = 0.2

    def setup(self):
        self.sel = pd.Series(['s1234']).astype('category')

    def time_rendering(self):
        str(self.sel)


class SetCategories(object):

    goal_time = 0.2

    def setup(self):
        n = 5 * 10**5
        arr = ['s%04d' % i for i in np.random.randint(0, n // 10, size=n)]
        self.ts = pd.Series(arr).astype('category')

    def time_set_categories(self):
        self.ts.cat.set_categories(self.ts.cat.categories[::2])


class Rank(object):

    goal_time = 0.2

    def setup(self):
        N = 10**5
        ncats = 100

        self.s_str = pd.Series(tm.makeCategoricalIndex(N, ncats)).astype(str)
        self.s_str_cat = self.s_str.astype('category')
        with warnings.catch_warnings(record=True):
            self.s_str_cat_ordered = self.s_str.astype('category',
                                                       ordered=True)

        self.s_int = pd.Series(np.random.randint(0, ncats, size=N))
        self.s_int_cat = self.s_int.astype('category')
        with warnings.catch_warnings(record=True):
            self.s_int_cat_ordered = self.s_int.astype('category',
                                                       ordered=True)

    def time_rank_string(self):
        self.s_str.rank()

    def time_rank_string_cat(self):
        self.s_str_cat.rank()

    def time_rank_string_cat_ordered(self):
        self.s_str_cat_ordered.rank()

    def time_rank_int(self):
        self.s_int.rank()

    def time_rank_int_cat(self):
        self.s_int_cat.rank()

    def time_rank_int_cat_ordered(self):
        self.s_int_cat_ordered.rank()


class Isin(object):

    goal_time = 0.2

    params = ['object', 'int64']
    param_names = ['dtype']

    def setup(self, dtype):
        np.random.seed(1234)
        n = 5 * 10**5
        sample_size = 100
        arr = [i for i in np.random.randint(0, n // 10, size=n)]
        if dtype == 'object':
            arr = ['s%04d' % i for i in arr]
        self.sample = np.random.choice(arr, sample_size)
        self.series = pd.Series(arr).astype('category')

    def time_isin_categorical(self, dtype):
        self.series.isin(self.sample)


class IsMonotonic(object):

    def setup(self):
        N = 1000
        self.c = pd.CategoricalIndex(list('a' * N + 'b' * N + 'c' * N))
        self.s = pd.Series(self.c)

    def time_categorical_index_is_monotonic_increasing(self):
        self.c.is_monotonic_increasing

    def time_categorical_index_is_monotonic_decreasing(self):
        self.c.is_monotonic_decreasing

    def time_categorical_series_is_monotonic_increasing(self):
        self.s.is_monotonic_increasing

    def time_categorical_series_is_monotonic_decreasing(self):
        self.s.is_monotonic_decreasing