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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
"""
These benchmarks are for Series and DataFrame indexing methods. For the
lower-level methods directly on Index and subclasses, see index_object.py,
indexing_engine.py, and index_cached.py
"""
import warnings
import numpy as np
from pandas import (
CategoricalIndex,
DataFrame,
Float64Index,
IndexSlice,
Int64Index,
IntervalIndex,
MultiIndex,
Series,
UInt64Index,
concat,
date_range,
option_context,
period_range,
)
from .pandas_vb_common import tm
class NumericSeriesIndexing:
params = [
(Int64Index, UInt64Index, Float64Index),
("unique_monotonic_inc", "nonunique_monotonic_inc"),
]
param_names = ["index_dtype", "index_structure"]
def setup(self, index, index_structure):
N = 10 ** 6
indices = {
"unique_monotonic_inc": index(range(N)),
"nonunique_monotonic_inc": index(
list(range(55)) + [54] + list(range(55, N - 1))
),
}
self.data = Series(np.random.rand(N), index=indices[index_structure])
self.array = np.arange(10000)
self.array_list = self.array.tolist()
def time_getitem_scalar(self, index, index_structure):
self.data[800000]
def time_getitem_slice(self, index, index_structure):
self.data[:800000]
def time_getitem_list_like(self, index, index_structure):
self.data[[800000]]
def time_getitem_array(self, index, index_structure):
self.data[self.array]
def time_getitem_lists(self, index, index_structure):
self.data[self.array_list]
def time_iloc_array(self, index, index_structure):
self.data.iloc[self.array]
def time_iloc_list_like(self, index, index_structure):
self.data.iloc[[800000]]
def time_iloc_scalar(self, index, index_structure):
self.data.iloc[800000]
def time_iloc_slice(self, index, index_structure):
self.data.iloc[:800000]
def time_loc_array(self, index, index_structure):
self.data.loc[self.array]
def time_loc_list_like(self, index, index_structure):
self.data.loc[[800000]]
def time_loc_scalar(self, index, index_structure):
self.data.loc[800000]
def time_loc_slice(self, index, index_structure):
self.data.loc[:800000]
class NonNumericSeriesIndexing:
params = [
("string", "datetime", "period"),
("unique_monotonic_inc", "nonunique_monotonic_inc", "non_monotonic"),
]
param_names = ["index_dtype", "index_structure"]
def setup(self, index, index_structure):
N = 10 ** 6
if index == "string":
index = tm.makeStringIndex(N)
elif index == "datetime":
index = date_range("1900", periods=N, freq="s")
elif index == "period":
index = period_range("1900", periods=N, freq="s")
index = index.sort_values()
assert index.is_unique and index.is_monotonic_increasing
if index_structure == "nonunique_monotonic_inc":
index = index.insert(item=index[2], loc=2)[:-1]
elif index_structure == "non_monotonic":
index = index[::2].append(index[1::2])
assert len(index) == N
self.s = Series(np.random.rand(N), index=index)
self.lbl = index[80000]
# warm up index mapping
self.s[self.lbl]
def time_getitem_label_slice(self, index, index_structure):
self.s[: self.lbl]
def time_getitem_pos_slice(self, index, index_structure):
self.s[:80000]
def time_getitem_scalar(self, index, index_structure):
self.s[self.lbl]
def time_getitem_list_like(self, index, index_structure):
self.s[[self.lbl]]
class DataFrameStringIndexing:
def setup(self):
index = tm.makeStringIndex(1000)
columns = tm.makeStringIndex(30)
with warnings.catch_warnings(record=True):
self.df = DataFrame(np.random.randn(1000, 30), index=index, columns=columns)
self.idx_scalar = index[100]
self.col_scalar = columns[10]
self.bool_indexer = self.df[self.col_scalar] > 0
self.bool_obj_indexer = self.bool_indexer.astype(object)
self.boolean_indexer = (self.df[self.col_scalar] > 0).astype("boolean")
def time_loc(self):
self.df.loc[self.idx_scalar, self.col_scalar]
def time_getitem_scalar(self):
self.df[self.col_scalar][self.idx_scalar]
def time_boolean_rows(self):
self.df[self.bool_indexer]
def time_boolean_rows_object(self):
self.df[self.bool_obj_indexer]
def time_boolean_rows_boolean(self):
self.df[self.boolean_indexer]
class DataFrameNumericIndexing:
def setup(self):
self.idx_dupe = np.array(range(30)) * 99
self.df = DataFrame(np.random.randn(100000, 5))
self.df_dup = concat([self.df, 2 * self.df, 3 * self.df])
self.bool_indexer = [True] * 50000 + [False] * 50000
def time_iloc_dups(self):
self.df_dup.iloc[self.idx_dupe]
def time_loc_dups(self):
self.df_dup.loc[self.idx_dupe]
def time_iloc(self):
self.df.iloc[:100, 0]
def time_loc(self):
self.df.loc[:100, 0]
def time_bool_indexer(self):
self.df[self.bool_indexer]
class Take:
params = ["int", "datetime"]
param_names = ["index"]
def setup(self, index):
N = 100000
indexes = {
"int": Int64Index(np.arange(N)),
"datetime": date_range("2011-01-01", freq="S", periods=N),
}
index = indexes[index]
self.s = Series(np.random.rand(N), index=index)
self.indexer = [True, False, True, True, False] * 20000
def time_take(self, index):
self.s.take(self.indexer)
class MultiIndexing:
def setup(self):
mi = MultiIndex.from_product([range(1000), range(1000)])
self.s = Series(np.random.randn(1000000), index=mi)
self.df = DataFrame(self.s)
n = 100000
with warnings.catch_warnings(record=True):
self.mdt = DataFrame(
{
"A": np.random.choice(range(10000, 45000, 1000), n),
"B": np.random.choice(range(10, 400), n),
"C": np.random.choice(range(1, 150), n),
"D": np.random.choice(range(10000, 45000), n),
"x": np.random.choice(range(400), n),
"y": np.random.choice(range(25), n),
}
)
self.idx = IndexSlice[20000:30000, 20:30, 35:45, 30000:40000]
self.mdt = self.mdt.set_index(["A", "B", "C", "D"]).sort_index()
def time_index_slice(self):
self.mdt.loc[self.idx, :]
class IntervalIndexing:
def setup_cache(self):
idx = IntervalIndex.from_breaks(np.arange(1000001))
monotonic = Series(np.arange(1000000), index=idx)
return monotonic
def time_getitem_scalar(self, monotonic):
monotonic[80000]
def time_loc_scalar(self, monotonic):
monotonic.loc[80000]
def time_getitem_list(self, monotonic):
monotonic[80000:]
def time_loc_list(self, monotonic):
monotonic.loc[80000:]
class CategoricalIndexIndexing:
params = ["monotonic_incr", "monotonic_decr", "non_monotonic"]
param_names = ["index"]
def setup(self, index):
N = 10 ** 5
values = list("a" * N + "b" * N + "c" * N)
indices = {
"monotonic_incr": CategoricalIndex(values),
"monotonic_decr": CategoricalIndex(reversed(values)),
"non_monotonic": CategoricalIndex(list("abc" * N)),
}
self.data = indices[index]
self.int_scalar = 10000
self.int_list = list(range(10000))
self.cat_scalar = "b"
self.cat_list = ["a", "c"]
def time_getitem_scalar(self, index):
self.data[self.int_scalar]
def time_getitem_slice(self, index):
self.data[: self.int_scalar]
def time_getitem_list_like(self, index):
self.data[[self.int_scalar]]
def time_getitem_list(self, index):
self.data[self.int_list]
def time_getitem_bool_array(self, index):
self.data[self.data == self.cat_scalar]
def time_get_loc_scalar(self, index):
self.data.get_loc(self.cat_scalar)
def time_get_indexer_list(self, index):
self.data.get_indexer(self.cat_list)
class MethodLookup:
def setup_cache(self):
s = Series()
return s
def time_lookup_iloc(self, s):
s.iloc
def time_lookup_loc(self, s):
s.loc
class GetItemSingleColumn:
def setup(self):
self.df_string_col = DataFrame(np.random.randn(3000, 1), columns=["A"])
self.df_int_col = DataFrame(np.random.randn(3000, 1))
def time_frame_getitem_single_column_label(self):
self.df_string_col["A"]
def time_frame_getitem_single_column_int(self):
self.df_int_col[0]
class IndexSingleRow:
params = [True, False]
param_names = ["unique_cols"]
def setup(self, unique_cols):
arr = np.arange(10 ** 7).reshape(-1, 10)
df = DataFrame(arr)
dtypes = ["u1", "u2", "u4", "u8", "i1", "i2", "i4", "i8", "f8", "f4"]
for i, d in enumerate(dtypes):
df[i] = df[i].astype(d)
if not unique_cols:
# GH#33032 single-row lookups with non-unique columns were
# 15x slower than with unique columns
df.columns = ["A", "A"] + list(df.columns[2:])
self.df = df
def time_iloc_row(self, unique_cols):
self.df.iloc[10000]
def time_loc_row(self, unique_cols):
self.df.loc[10000]
class AssignTimeseriesIndex:
def setup(self):
N = 100000
idx = date_range("1/1/2000", periods=N, freq="H")
self.df = DataFrame(np.random.randn(N, 1), columns=["A"], index=idx)
def time_frame_assign_timeseries_index(self):
self.df["date"] = self.df.index
class InsertColumns:
def setup(self):
self.N = 10 ** 3
self.df = DataFrame(index=range(self.N))
def time_insert(self):
np.random.seed(1234)
for i in range(100):
self.df.insert(0, i, np.random.randn(self.N), allow_duplicates=True)
def time_assign_with_setitem(self):
np.random.seed(1234)
for i in range(100):
self.df[i] = np.random.randn(self.N)
class ChainIndexing:
params = [None, "warn"]
param_names = ["mode"]
def setup(self, mode):
self.N = 1000000
def time_chained_indexing(self, mode):
with warnings.catch_warnings(record=True):
with option_context("mode.chained_assignment", mode):
df = DataFrame({"A": np.arange(self.N), "B": "foo"})
df2 = df[df.A > self.N // 2]
df2["C"] = 1.0
from .pandas_vb_common import setup # noqa: F401 isort:skip
|