|
1 | 1 | """ |
2 | | -Timing how long it takes to do a complex string replace on a whole column |
3 | | -with various methods |
| 2 | +Plotting how long it takes to do a complex string replace on a whole column |
| 3 | +with various methods. |
| 4 | +
|
| 5 | +In the dataset, in the `place_of_publication` column, you've got entries like |
| 6 | +these: |
| 7 | +
|
| 8 | +London |
| 9 | +London; Virtue & Yorston |
| 10 | +Oxford |
| 11 | +pp. 40. G. Bryan & Co: Oxford, 1898 |
| 12 | +Plymouth |
| 13 | +pp. 40. W. Cann: Plymouth, [1876?] |
| 14 | +
|
| 15 | +Most of these are just city names, but some have additional and unwanted |
| 16 | +information. For these, you want to detect if it has one of the city names, |
| 17 | +replacing the whole value with just the city name. |
4 | 18 | """ |
5 | 19 |
|
6 | | -import codetiming |
7 | 20 | import pandas as pd |
| 21 | +import perfplot |
8 | 22 |
|
9 | 23 | books = pd.read_csv("resources/books.csv") |
10 | 24 |
|
11 | 25 | CITIES = ["London", "Plymouth", "Oxford", "Boston"] |
12 | 26 |
|
13 | 27 |
|
14 | | -def clean_pub_replace(df): |
15 | | - def clean_pub_replace_inner(df): |
16 | | - col = df["place_of_publication"] |
17 | | - for city in CITIES: |
18 | | - col = col.replace(rf".*{city}.*", city, regex=True) |
19 | | - return col |
| 28 | +def _replace_city(text): |
| 29 | + for city in CITIES: |
| 30 | + if city in text: |
| 31 | + return city |
| 32 | + return text |
20 | 33 |
|
21 | | - return df.assign(place_of_publication=clean_pub_replace_inner) |
22 | 34 |
|
| 35 | +def clean_pub_replace(df): |
| 36 | + col = df["place_of_publication"] |
| 37 | + for city in CITIES: |
| 38 | + col = col.replace(rf".*{city}.*", city, regex=True) |
| 39 | + return col |
23 | 40 |
|
24 | | -def clean_pub_apply(df): |
25 | | - def clean_pub_apply_inner(df): |
26 | | - col = df["place_of_publication"] |
27 | | - for city in CITIES: |
28 | | - col = col.apply(lambda val: city if city in val else val) |
29 | | - return col |
30 | 41 |
|
31 | | - return df.assign(place_of_publication=clean_pub_apply_inner) |
| 42 | +def clean_pub_itertuples(df): |
| 43 | + return [_replace_city(row.place_of_publication) for row in df.itertuples()] |
32 | 44 |
|
33 | 45 |
|
34 | 46 | def clean_pub_iterrows(df): |
35 | | - def clean_pub_iterrows_inner(df): |
36 | | - col = [] |
37 | | - for _, row in df.iterrows(): |
38 | | - place = row["place_of_publication"] |
39 | | - |
40 | | - for name in CITIES: |
41 | | - place = name if name in place else place |
42 | | - |
43 | | - col.append(place) |
| 47 | + return [ |
| 48 | + _replace_city(row["place_of_publication"]) for _, row in df.iterrows() |
| 49 | + ] |
44 | 50 |
|
45 | | - return col |
46 | 51 |
|
47 | | - return df.assign(place_of_publication=clean_pub_iterrows_inner) |
48 | | - |
49 | | - |
50 | | -def clean_pub_itertuples(df): |
51 | | - def clean_pub_itertuples_inner(df): |
52 | | - col = [] |
53 | | - for row in df.itertuples(): |
54 | | - place = row.place_of_publication |
55 | | - for name in CITIES: |
56 | | - place = name if name in place else place |
57 | | - |
58 | | - col.append(place) |
59 | | - return col |
60 | | - |
61 | | - return df.assign(place_of_publication=clean_pub_itertuples_inner) |
| 52 | +def clean_pub_apply(df): |
| 53 | + col = df["place_of_publication"] |
| 54 | + for city in CITIES: |
| 55 | + col = col.apply(lambda val: city if city in val else val) |
| 56 | + return col |
62 | 57 |
|
63 | 58 |
|
64 | 59 | def clean_pub_list_comp(df): |
65 | | - def replace_city(text): |
66 | | - for city in CITIES: |
67 | | - if city in text: |
68 | | - return city |
69 | | - |
70 | | - return text |
71 | | - |
72 | | - def clean_pub_list_comp_inner(df): |
73 | | - return [replace_city(place) for place in df["place_of_publication"]] |
74 | | - |
75 | | - return df.assign(place_of_publication=clean_pub_list_comp_inner) |
76 | | - |
77 | | - |
78 | | -for f in [ |
79 | | - clean_pub_apply, |
80 | | - clean_pub_iterrows, |
81 | | - clean_pub_itertuples, |
82 | | - clean_pub_replace, |
83 | | - clean_pub_list_comp, |
84 | | -]: |
85 | | - with codetiming.Timer( |
86 | | - name=f.__name__, text="{name:20}: {milliseconds:.2f} ms" |
87 | | - ): |
88 | | - print(f(books).head()) |
| 60 | + return [_replace_city(place) for place in df["place_of_publication"]] |
| 61 | + |
| 62 | + |
| 63 | +def get_books(n): |
| 64 | + books = pd.read_csv("resources/books.csv") |
| 65 | + if n < len(books): |
| 66 | + return books.iloc[:n] |
| 67 | + return pd.concat([books for _ in range((n // len(books)) + 1)]).iloc[:n] |
| 68 | + |
| 69 | + |
| 70 | +perfplot.live( |
| 71 | + setup=lambda n: get_books(n), |
| 72 | + kernels=[ |
| 73 | + clean_pub_replace, |
| 74 | + clean_pub_itertuples, |
| 75 | + clean_pub_iterrows, |
| 76 | + clean_pub_apply, |
| 77 | + clean_pub_list_comp, |
| 78 | + ], |
| 79 | + labels=["replace", "itertuples", "iterrows", "apply", "list comp"], |
| 80 | + n_range=[i**2 for i in range(1, 40, 2)], |
| 81 | + equality_check=None, |
| 82 | + logy=True, |
| 83 | +) |
0 commit comments