Skip to content

Commit c2ea258

Browse files
mroeschkejreback
authored andcommitted
CLN: ASV replace (#18833)
1 parent a03bb08 commit c2ea258

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

asv_bench/benchmarks/replace.py

+42-54
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,58 @@
1-
from .pandas_vb_common import *
1+
import numpy as np
2+
import pandas as pd
23

4+
from .pandas_vb_common import setup # noqa
35

4-
class replace_fillna(object):
5-
goal_time = 0.2
6-
7-
def setup(self):
8-
self.N = 1000000
9-
try:
10-
self.rng = date_range('1/1/2000', periods=self.N, freq='min')
11-
except NameError:
12-
self.rng = DatetimeIndex('1/1/2000', periods=self.N, offset=datetools.Minute())
13-
self.date_range = DateRange
14-
self.ts = Series(np.random.randn(self.N), index=self.rng)
156

16-
def time_replace_fillna(self):
17-
self.ts.fillna(0.0, inplace=True)
7+
class FillNa(object):
188

19-
20-
class replace_large_dict(object):
219
goal_time = 0.2
10+
params = [True, False]
11+
param_names = ['inplace']
2212

23-
def setup(self):
24-
self.n = (10 ** 6)
25-
self.start_value = (10 ** 5)
26-
self.to_rep = {i: self.start_value + i for i in range(self.n)}
27-
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
28-
29-
def time_replace_large_dict(self):
30-
self.s.replace(self.to_rep, inplace=True)
13+
def setup(self, inplace):
14+
N = 10**6
15+
rng = pd.date_range('1/1/2000', periods=N, freq='min')
16+
data = np.random.randn(N)
17+
data[::2] = np.nan
18+
self.ts = pd.Series(data, index=rng)
3119

20+
def time_fillna(self, inplace):
21+
self.ts.fillna(0.0, inplace=inplace)
3222

33-
class replace_convert(object):
34-
goal_time = 0.5
23+
def time_replace(self, inplace):
24+
self.ts.replace(np.nan, 0.0, inplace=inplace)
3525

36-
def setup(self):
37-
self.n = (10 ** 3)
38-
self.to_ts = {i: pd.Timestamp(i) for i in range(self.n)}
39-
self.to_td = {i: pd.Timedelta(i) for i in range(self.n)}
40-
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
41-
self.df = DataFrame({'A': np.random.randint(self.n, size=(10 ** 3)),
42-
'B': np.random.randint(self.n, size=(10 ** 3))})
4326

44-
def time_replace_series_timestamp(self):
45-
self.s.replace(self.to_ts)
27+
class ReplaceDict(object):
4628

47-
def time_replace_series_timedelta(self):
48-
self.s.replace(self.to_td)
29+
goal_time = 0.2
30+
params = [True, False]
31+
param_names = ['inplace']
4932

50-
def time_replace_frame_timestamp(self):
51-
self.df.replace(self.to_ts)
33+
def setup(self, inplace):
34+
N = 10**5
35+
start_value = 10**5
36+
self.to_rep = dict(enumerate(np.arange(N) + start_value))
37+
self.s = pd.Series(np.random.randint(N, size=10**3))
5238

53-
def time_replace_frame_timedelta(self):
54-
self.df.replace(self.to_td)
39+
def time_replace_series(self, inplace):
40+
self.s.replace(self.to_rep, inplace=inplace)
5541

5642

57-
class replace_replacena(object):
58-
goal_time = 0.2
43+
class Convert(object):
5944

60-
def setup(self):
61-
self.N = 1000000
62-
try:
63-
self.rng = date_range('1/1/2000', periods=self.N, freq='min')
64-
except NameError:
65-
self.rng = DatetimeIndex('1/1/2000', periods=self.N, offset=datetools.Minute())
66-
self.date_range = DateRange
67-
self.ts = Series(np.random.randn(self.N), index=self.rng)
68-
69-
def time_replace_replacena(self):
70-
self.ts.replace(np.nan, 0.0, inplace=True)
45+
goal_time = 0.5
46+
params = (['DataFrame', 'Series'], ['Timestamp', 'Timedelta'])
47+
param_names = ['contructor', 'replace_data']
48+
49+
def setup(self, contructor, replace_data):
50+
N = 10**3
51+
data = {'Series': pd.Series(np.random.randint(N, size=N)),
52+
'DataFrame': pd.DataFrame({'A': np.random.randint(N, size=N),
53+
'B': np.random.randint(N, size=N)})}
54+
self.to_replace = {i: getattr(pd, replace_data) for i in range(N)}
55+
self.data = data[contructor]
56+
57+
def time_replace(self, contructor, replace_data):
58+
self.data.replace(self.to_replace)

0 commit comments

Comments
 (0)