diff --git a/pandas-fast-flexible-intuitive/README.md b/pandas-fast-flexible-intuitive/README.md index a212d0635a..8c692bdd92 100644 --- a/pandas-fast-flexible-intuitive/README.md +++ b/pandas-fast-flexible-intuitive/README.md @@ -4,9 +4,15 @@ Corresponding data for ["Fast, Flexible, Easy and Intuitive: How to Speed Up You ## Running the Script +Install the pinned dependencies first: + +```bash +$ python -m pip install -r requirements.txt +``` + The module `tutorial/__main__.py` is the script that mimics the tutorial code. A `__main__.py` script has the effect of executing when the containing package is run as `python -m `. -Here is an example output (MacBook Pro, 3.1 GHz Intel Core i5): +Here is an example output (Python 3.14, pandas 3.1.2): ```bash pandas-fast-flexible-intuitive$ python3 -m tutorial @@ -17,32 +23,32 @@ Companion code to https://realpython.com/fast-flexible-pandas. Note: most of the functions here modify a Pandas DataFrame in-place, which is generally not great practice but used with caution here. -Python version: 3.6.6 -Pandas version: 0.23.2 +Python version: 3.14.10 +Pandas version: 3.1.2 Timing code ... Best of 3 trials with 10 function calls per trial: -Function `convert` ran in average of 1.348 seconds. +Function `convert` ran in average of 0.392 seconds. Best of 3 trials with 100 function calls per trial: -Function `convert_with_format` ran in average of 0.025 seconds. +Function `convert_with_format` ran in average of 0.016 seconds. Best of 2 trials with 10 function calls per trial: -Function `apply_tariff_loop` ran in average of 3.313 seconds. +Function `apply_tariff_loop` ran in average of 0.643 seconds. Best of 3 trials with 50 function calls per trial: -Function `apply_tariff_iterrows` ran in average of 0.650 seconds. +Function `apply_tariff_iterrows` ran in average of 0.384 seconds. Best of 3 trials with 100 function calls per trial: -Function `apply_tariff_withapply` ran in average of 0.202 seconds. +Function `apply_tariff_withapply` ran in average of 0.065 seconds. Best of 3 trials with 1000 function calls per trial: -Function `apply_tariff_isin` ran in average of 0.004 seconds. +Function `apply_tariff_isin` ran in average of 0.002 seconds. Best of 3 trials with 1000 function calls per trial: Function `apply_tariff_cut` ran in average of 0.001 seconds. Best of 3 trials with 1000 function calls per trial: -Function `apply_tariff_digitize` ran in average of 0.001 seconds. +Function `apply_tariff_digitize` ran in average of 0.000 seconds. ``` diff --git a/pandas-fast-flexible-intuitive/requirements.txt b/pandas-fast-flexible-intuitive/requirements.txt new file mode 100644 index 0000000000..45327b2be0 --- /dev/null +++ b/pandas-fast-flexible-intuitive/requirements.txt @@ -0,0 +1,3 @@ +numpy==2.5.5 +pandas==3.1.2 +tables==3.11.1 diff --git a/pandas-fast-flexible-intuitive/tutorial/__main__.py b/pandas-fast-flexible-intuitive/tutorial/__main__.py index 5507ae0b38..ede7d66594 100644 --- a/pandas-fast-flexible-intuitive/tutorial/__main__.py +++ b/pandas-fast-flexible-intuitive/tutorial/__main__.py @@ -12,7 +12,7 @@ from .timer import timeit -# Two functions for conversion of object dtype to datetime +# Two functions for conversion of str dtype to datetime # Note that you could also specify `parse_dates` within `pd.read_csv()` @timeit(repeat=3, number=10) def convert(df, column_name): @@ -98,7 +98,9 @@ def apply_tariff_cut(df): cents_per_kwh = pd.cut( x=df.index.hour, bins=[0, 7, 17, 24], - include_lowest=True, + # Close bins on the left, [0, 7), [7, 17), [17, 24), so that + # hours 7 and 17 land in the same tariff as the other functions. + right=False, labels=[12, 20, 28], ).astype(int) df["cost_cents"] = cents_per_kwh * df["energy_kwh"] @@ -123,7 +125,7 @@ def main(): apply_tariff_iterrows(df) apply_tariff_withapply(df) - df.set_index("date_time", inplace=True) + df = df.set_index("date_time") apply_tariff_isin(df) apply_tariff_cut(df) @@ -141,7 +143,6 @@ def main(): import platform import sys - pd.set_option("mode.chained_assignment", None) pd.set_option("compute.use_bottleneck", True) pd.set_option("compute.use_numexpr", True)