Skip to content

Commit 1b65453

Browse files
committed
BENCH: Revise PR 21807 based on reviewer comments
* StringIO object rewinds in asv benchmarks now occur through an inherited data property
1 parent 28c393a commit 1b65453

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

asv_bench/benchmarks/io/csv.py

+27-26
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ def time_frame_date_formatting(self):
5454
self.data.to_csv(self.fname, date_format='%Y%m%d')
5555

5656

57-
class ReadCSVDInferDatetimeFormat(object):
57+
class StringIORewind(object):
58+
59+
@property
60+
def data(self):
61+
self.StringIO_input.seek(0)
62+
return self.StringIO_input
63+
64+
class ReadCSVDInferDatetimeFormat(StringIORewind):
5865

5966
goal_time = 0.2
6067
params = ([True, False], ['custom', 'iso8601', 'ymd'])
@@ -66,10 +73,9 @@ def setup(self, infer_datetime_format, format):
6673
'iso8601': '%Y-%m-%d %H:%M:%S',
6774
'ymd': '%Y%m%d'}
6875
dt_format = formats[format]
69-
self.data = StringIO('\n'.join(rng.strftime(dt_format).tolist()))
76+
self.StringIO_input = StringIO('\n'.join(rng.strftime(dt_format).tolist()))
7077

7178
def time_read_csv(self, infer_datetime_format, format):
72-
self.data.seek(0)
7379
read_csv(self.data, header=None, names=['foo'], parse_dates=['foo'],
7480
infer_datetime_format=infer_datetime_format)
7581

@@ -96,7 +102,7 @@ def time_skipprows(self, skiprows):
96102
read_csv(self.fname, skiprows=skiprows)
97103

98104

99-
class ReadUint64Integers(object):
105+
class ReadUint64Integers(StringIORewind):
100106

101107
goal_time = 0.2
102108

@@ -109,16 +115,16 @@ def setup(self):
109115
self.data2 = StringIO('\n'.join(arr.astype(str).tolist()))
110116

111117
def time_read_uint64(self):
112-
self.data1.seek(0)
113-
read_csv(self.data1, header=None, names=['foo'])
118+
self.StringIO_input = self.data1
119+
read_csv(self.data, header=None, names=['foo'])
114120

115121
def time_read_uint64_neg_values(self):
116-
self.data2.seek(0)
117-
read_csv(self.data2, header=None, names=['foo'])
122+
self.StringIO_input = self.data2
123+
read_csv(self.data, header=None, names=['foo'])
118124

119125
def time_read_uint64_na_values(self):
120-
self.data1.seek(0)
121-
read_csv(self.data1, header=None, names=['foo'],
126+
self.StringIO_input = self.data1
127+
read_csv(self.data, header=None, names=['foo'],
122128
na_values=self.na_values)
123129

124130

@@ -144,20 +150,19 @@ def time_thousands(self, sep, thousands):
144150
read_csv(self.fname, sep=sep, thousands=thousands)
145151

146152

147-
class ReadCSVComment(object):
153+
class ReadCSVComment(StringIORewind):
148154

149155
goal_time = 0.2
150156

151157
def setup(self):
152158
data = ['A,B,C'] + (['1,2,3 # comment'] * 100000)
153-
self.s_data = StringIO('\n'.join(data))
159+
self.StringIO_input = StringIO('\n'.join(data))
154160

155161
def time_comment(self):
156-
self.s_data.seek(0)
157-
read_csv(self.s_data, comment='#', header=None, names=list('abc'))
162+
read_csv(self.data, comment='#', header=None, names=list('abc'))
158163

159164

160-
class ReadCSVFloatPrecision(object):
165+
class ReadCSVFloatPrecision(StringIORewind):
161166

162167
goal_time = 0.2
163168
params = ([',', ';'], ['.', '_'], [None, 'high', 'round_trip'])
@@ -169,16 +174,14 @@ def setup(self, sep, decimal, float_precision):
169174
rows = sep.join(['0{}'.format(decimal) + '{}'] * 3) + '\n'
170175
data = rows * 5
171176
data = data.format(*floats) * 200 # 1000 x 3 strings csv
172-
self.s_data = StringIO(data)
177+
self.StringIO_input = StringIO(data)
173178

174179
def time_read_csv(self, sep, decimal, float_precision):
175-
self.s_data.seek(0)
176-
read_csv(self.s_data, sep=sep, header=None, names=list('abc'),
180+
read_csv(self.data, sep=sep, header=None, names=list('abc'),
177181
float_precision=float_precision)
178182

179183
def time_read_csv_python_engine(self, sep, decimal, float_precision):
180-
self.s_data.seek(0)
181-
read_csv(self.s_data, sep=sep, header=None, engine='python',
184+
read_csv(self.data, sep=sep, header=None, engine='python',
182185
float_precision=None, names=list('abc'))
183186

184187

@@ -200,7 +203,7 @@ def time_convert_direct(self):
200203
read_csv(self.fname, dtype='category')
201204

202205

203-
class ReadCSVParseDates(object):
206+
class ReadCSVParseDates(StringIORewind):
204207

205208
goal_time = 0.2
206209

@@ -213,14 +216,12 @@ def setup(self):
213216
"""
214217
two_cols = ['KORD,19990127'] * 5
215218
data = data.format(*two_cols)
216-
self.s_data = StringIO(data)
219+
self.StringIO_input = StringIO(data)
217220

218221
def time_multiple_date(self):
219-
self.s_data.seek(0)
220-
read_csv(self.s_data, sep=',', header=None,
222+
read_csv(self.data, sep=',', header=None,
221223
names=list(string.digits[:9]), parse_dates=[[1, 2], [1, 3]])
222224

223225
def time_baseline(self):
224-
self.s_data.seek(0)
225-
read_csv(self.s_data, sep=',', header=None, parse_dates=[1],
226+
read_csv(self.data, sep=',', header=None, parse_dates=[1],
226227
names=list(string.digits[:9]))

0 commit comments

Comments
 (0)