Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions pandas-fast-flexible-intuitive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <packagename>`.

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
Expand All @@ -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.
```
3 changes: 3 additions & 0 deletions pandas-fast-flexible-intuitive/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy==2.5.5
pandas==3.1.2
tables==3.11.1
9 changes: 5 additions & 4 deletions pandas-fast-flexible-intuitive/tutorial/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]
Expand All @@ -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)
Expand All @@ -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)

Expand Down
Loading