Skip to content

Commit 074a06a

Browse files
jbrockmendelroberthdevries
authored andcommitted
REF: de-duplicate factorize and duplicated code (pandas-dev#32216)
1 parent 6df88c3 commit 074a06a

File tree

2 files changed

+35
-70
lines changed

2 files changed

+35
-70
lines changed

asv_bench/benchmarks/algorithms.py

+35-56
Original file line numberDiff line numberDiff line change
@@ -31,83 +31,62 @@ def time_maybe_convert_objects(self):
3131

3232
class Factorize:
3333

34-
params = [[True, False], ["int", "uint", "float", "string"]]
35-
param_names = ["sort", "dtype"]
36-
37-
def setup(self, sort, dtype):
38-
N = 10 ** 5
39-
data = {
40-
"int": pd.Int64Index(np.arange(N).repeat(5)),
41-
"uint": pd.UInt64Index(np.arange(N).repeat(5)),
42-
"float": pd.Float64Index(np.random.randn(N).repeat(5)),
43-
"string": tm.makeStringIndex(N).repeat(5),
44-
}
45-
self.idx = data[dtype]
46-
47-
def time_factorize(self, sort, dtype):
48-
self.idx.factorize(sort=sort)
49-
50-
51-
class FactorizeUnique:
52-
53-
params = [[True, False], ["int", "uint", "float", "string"]]
54-
param_names = ["sort", "dtype"]
34+
params = [
35+
[True, False],
36+
[True, False],
37+
["int", "uint", "float", "string", "datetime64[ns]", "datetime64[ns, tz]"],
38+
]
39+
param_names = ["unique", "sort", "dtype"]
5540

56-
def setup(self, sort, dtype):
41+
def setup(self, unique, sort, dtype):
5742
N = 10 ** 5
5843
data = {
5944
"int": pd.Int64Index(np.arange(N)),
6045
"uint": pd.UInt64Index(np.arange(N)),
61-
"float": pd.Float64Index(np.arange(N)),
46+
"float": pd.Float64Index(np.random.randn(N)),
6247
"string": tm.makeStringIndex(N),
63-
}
64-
self.idx = data[dtype]
65-
assert self.idx.is_unique
66-
67-
def time_factorize(self, sort, dtype):
48+
"datetime64[ns]": pd.date_range("2011-01-01", freq="H", periods=N),
49+
"datetime64[ns, tz]": pd.date_range(
50+
"2011-01-01", freq="H", periods=N, tz="Asia/Tokyo"
51+
),
52+
}[dtype]
53+
if not unique:
54+
data = data.repeat(5)
55+
self.idx = data
56+
57+
def time_factorize(self, unique, sort, dtype):
6858
self.idx.factorize(sort=sort)
6959

7060

7161
class Duplicated:
7262

73-
params = [["first", "last", False], ["int", "uint", "float", "string"]]
74-
param_names = ["keep", "dtype"]
75-
76-
def setup(self, keep, dtype):
77-
N = 10 ** 5
78-
data = {
79-
"int": pd.Int64Index(np.arange(N).repeat(5)),
80-
"uint": pd.UInt64Index(np.arange(N).repeat(5)),
81-
"float": pd.Float64Index(np.random.randn(N).repeat(5)),
82-
"string": tm.makeStringIndex(N).repeat(5),
83-
}
84-
self.idx = data[dtype]
85-
# cache is_unique
86-
self.idx.is_unique
87-
88-
def time_duplicated(self, keep, dtype):
89-
self.idx.duplicated(keep=keep)
90-
91-
92-
class DuplicatedUniqueIndex:
93-
94-
params = ["int", "uint", "float", "string"]
95-
param_names = ["dtype"]
63+
params = [
64+
[True, False],
65+
["first", "last", False],
66+
["int", "uint", "float", "string", "datetime64[ns]", "datetime64[ns, tz]"],
67+
]
68+
param_names = ["unique", "keep", "dtype"]
9669

97-
def setup(self, dtype):
70+
def setup(self, unique, keep, dtype):
9871
N = 10 ** 5
9972
data = {
10073
"int": pd.Int64Index(np.arange(N)),
10174
"uint": pd.UInt64Index(np.arange(N)),
10275
"float": pd.Float64Index(np.random.randn(N)),
10376
"string": tm.makeStringIndex(N),
104-
}
105-
self.idx = data[dtype]
77+
"datetime64[ns]": pd.date_range("2011-01-01", freq="H", periods=N),
78+
"datetime64[ns, tz]": pd.date_range(
79+
"2011-01-01", freq="H", periods=N, tz="Asia/Tokyo"
80+
),
81+
}[dtype]
82+
if not unique:
83+
data = data.repeat(5)
84+
self.idx = data
10685
# cache is_unique
10786
self.idx.is_unique
10887

109-
def time_duplicated_unique(self, dtype):
110-
self.idx.duplicated()
88+
def time_duplicated(self, unique, keep, dtype):
89+
self.idx.duplicated(keep=keep)
11190

11291

11392
class Hashing:

asv_bench/benchmarks/timeseries.py

-14
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,6 @@ def time_reest_datetimeindex(self, tz):
9191
self.df.reset_index()
9292

9393

94-
class Factorize:
95-
96-
params = [None, "Asia/Tokyo"]
97-
param_names = "tz"
98-
99-
def setup(self, tz):
100-
N = 100000
101-
self.dti = date_range("2011-01-01", freq="H", periods=N, tz=tz)
102-
self.dti = self.dti.repeat(5)
103-
104-
def time_factorize(self, tz):
105-
self.dti.factorize()
106-
107-
10894
class InferFreq:
10995

11096
params = [None, "D", "B"]

0 commit comments

Comments
 (0)