<jupyter_start><jupyter_text>STOP THE COUNT!Let us count only the votes emitted before 17:00.<jupyter_code>import pandas as pd
import dask.dataframe as dd
# Your cluster here.
client.restart()
def get_winner(count_per_region: pd.DataFrame) -> pd.Series:
    results = list()
    for region, df in count_per_region.groupby("region"):
        results.append(
            {"region": region,
             "winner": df.set_index("vote").result.idxmax()}
        )

    winner_per_region = pd.DataFrame(results)
    delegates_per_region = pd.read_csv("data/region_delegates.csv")
    winner_region_delegates = pd.merge(winner_per_region, delegates_per_region, on="region")
    return (winner_region_delegates
            .groupby("winner")
            .delegates.sum()
            .sort_values(ascending=False))    
data = dd.read_csv("data/votes_*.csv",
                   usecols=["timestamp", "region", "vote"],
                   parse_dates=["timestamp"],
                   dtype={"region": "category", "vote": "category"})
data["result"] = 1<jupyter_output><empty_output><jupyter_text>The one thing that changes is that here we will filter before aggregating.<jupyter_code>count_per_region_before_17 = (
    data[data.timestamp.dt.hour < 17]
    .groupby(["region", "vote"])
    .result.agg("count")
    .reset_index()
)<jupyter_output><empty_output><jupyter_text>Filtering is done the same as in pandas, by creating a maskDatetime accessors `.dt` also work for Dask dataframes<jupyter_code>count_per_region_before_17_computed = count_per_region_before_17.compute()
get_winner(count_per_region_before_17_computed)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1192214111922141, 'fraction_numerical': 0.009124087591240875, 'mean_word_length': 4.015243902439025, 'pattern_counts': {'":': 4, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '603567', 'license': 'MIT', 'n_tokens_mistral': 549, 'n_tokens_neox': 535, 'n_words': 102, 'path': '3_stop_the_count.ipynb', 'repo_name': 'ber2/pyday2020-counting-votes-with-dask', 'size': '1644'}