timeit is a Python standard-library module for benchmarking small pieces of code. It runs the code repeatedly and reports the mean execution time, making alternative implementations comparable. section 12.10 section 12.6
Use timeit to compare equivalent pandas operations rather than relying on a single informal measurement:
- Compare boolean filtering with .loc, df.query, and df.eval. section 12.9
- Test increasingly selective conditions, from registration state through vehicle type, color, curb distance, and make. section 12.9
- Use %timeit for an expression or %%timeit for a cell in an interactive environment. section 12.10
Comparing equivalent filters
After loading and renaming the parking-data columns, time equivalent filters such as df.loc[df['state'] == 'NY'], df.query('state == "NY"'), and df[df.eval('state == "NY"')]. Extend the comparison with isin and compound conditions, then identify which approach runs fastest for each query. section 12.10 section 12.9
%%timeit
df.loc[(df['state'] == 'NY') |
(df['state'] == 'NJ') |
(df['state'] == 'CT')]
Inspect a cell-timing example that benchmarks a multi-condition .loc filter against equivalent query and eval forms. section 12.10
