Skip to content

Commit 612cd05

Browse files
authored
TST: Benchmarks for CSV writing for various index variations (#39318)
1 parent 5d65e0a commit 612cd05

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

asv_bench/benchmarks/io/csv.py

+48
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,54 @@ def time_frame(self, obs):
7676
self.data.to_csv(self.fname)
7777

7878

79+
class ToCSVIndexes(BaseIO):
80+
81+
fname = "__test__.csv"
82+
83+
@staticmethod
84+
def _create_df(rows, cols):
85+
index_cols = {
86+
"index1": np.random.randint(0, rows, rows),
87+
"index2": np.full(rows, 1, dtype=np.int),
88+
"index3": np.full(rows, 1, dtype=np.int),
89+
}
90+
data_cols = {
91+
f"col{i}": np.random.uniform(0, 100000.0, rows) for i in range(cols)
92+
}
93+
df = DataFrame({**index_cols, **data_cols})
94+
return df
95+
96+
def setup(self):
97+
ROWS = 100000
98+
COLS = 5
99+
# For tests using .head(), create an initial dataframe with this many times
100+
# more rows
101+
HEAD_ROW_MULTIPLIER = 10
102+
103+
self.df_standard_index = self._create_df(ROWS, COLS)
104+
105+
self.df_custom_index_then_head = (
106+
self._create_df(ROWS * HEAD_ROW_MULTIPLIER, COLS)
107+
.set_index(["index1", "index2", "index3"])
108+
.head(ROWS)
109+
)
110+
111+
self.df_head_then_custom_index = (
112+
self._create_df(ROWS * HEAD_ROW_MULTIPLIER, COLS)
113+
.head(ROWS)
114+
.set_index(["index1", "index2", "index3"])
115+
)
116+
117+
def time_standard_index(self):
118+
self.df_standard_index.to_csv(self.fname)
119+
120+
def time_multiindex(self):
121+
self.df_head_then_custom_index.to_csv(self.fname)
122+
123+
def time_head_of_multiindex(self):
124+
self.df_custom_index_then_head.to_csv(self.fname)
125+
126+
79127
class StringIORewind:
80128
def data(self, stringio_object):
81129
stringio_object.seek(0)

0 commit comments

Comments
 (0)