import%20marimo%0A%0A__generated_with%20%3D%20%220.13.15%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20path%20%3D%20Path(__file__).parent%0A%0A%20%20%20%20g%20%3D%20pl.read_csv(str(path%20%2F%20%22public%22%20%2F%20%22girls.csv%22))%0A%20%20%20%20b%20%3D%20pl.read_csv(str(path%20%2F%20%22public%22%20%2F%20%22boys.csv%22))%0A%0A%0A%40app.function%0Adef%20age(ts%3A%20pl.Series)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Calculate%20the%20'age'%20of%20a%20name%20based%20on%20its%20distribution%20over%20time.%0A%0A%20%20%20%20This%20function%20determines%20the%20age%20of%20a%20name%20by%20finding%20the%20index%20at%20which%0A%20%20%20%20the%20cumulative%20distribution%20of%20the%20name%20reaches%2050%25.%20This%20represents%20the%0A%20%20%20%20point%20in%20time%20when%20half%20of%20all%20babies%20with%20this%20name%20had%20been%20born.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20ts%3A%20A%20polars%20Series%20containing%20the%20distribution%20of%20a%20name%20over%20time%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20An%20integer%20representing%20the%20'age'%20of%20the%20name%20(index%20at%2050%25%20cumulative%20distribution)%0A%20%20%20%20%22%22%22%0A%20%20%20%20%23%20Polars%20equivalent%20of%20dropna%20and%20sum%0A%20%20%20%20%23%20Convert%20to%20numpy%20for%20easier%20manipulation%0A%20%20%20%20ts_filtered%20%3D%20ts.drop_nulls().to_numpy()%0A%0A%20%20%20%20%23%20Get%20into%20probabilities%0A%20%20%20%20p%20%3D%20ts_filtered%20%2F%20ts_filtered.sum()%0A%0A%20%20%20%20%23%20Accumulate%20all%20the%20probabilities%0A%20%20%20%20a%20%3D%20np.cumsum(p)%0A%0A%20%20%20%20%23%20Get%20the%20first%20index%20where%20at%20least%2050%25%20of%20the%20babies%20have%20been%20born%0A%20%20%20%20%23%20This%20is%20a%20bit%20more%20complex%20in%20polars%20than%20pandas%0A%20%20%20%20%23%20We%20need%20to%20find%20the%20first%20index%20where%20the%20cumulative%20sum%20is%20%3E%3D%200.5%0A%20%20%20%20first_idx%20%3D%20np.where(a%20%3E%3D%200.5)%5B0%5D%5B0%5D%0A%0A%20%20%20%20%23%20Return%20the%20year%20(assuming%20the%20first%20column%20is%20the%20year)%0A%20%20%20%20%23%20This%20might%20need%20adjustment%20based%20on%20the%20actual%20data%20structure%0A%20%20%20%20return%20first_idx%20%2B%201%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Polars%20equivalent%20of%20apply%20and%20sort_values%0A%20%20%20%20%23%20Apply%20age%20function%20to%20each%20column3%20and%20sort%0A%20%20%20%20_result%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22column%22%3A%20b.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20%5Bage(b%5Bcol%5D)%20for%20col%20in%20b.columns%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).sort(%22age%22)%0A%0A%20%20%20%20print(_result)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Polars%20equivalent%20of%20apply%20and%20sort_values%0A%20%20%20%20%23%20Apply%20age%20function%20to%20each%20column3%20and%20sort%0A%20%20%20%20_result%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22column%22%3A%20g.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20%5Bage(g%5Bcol%5D)%20for%20col%20in%20g.columns%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).sort(%22age%22)%0A%0A%20%20%20%20print(_result)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Extract%20the%20Adolf%20column%20and%20filter%20out%20null%20values%0A%20%20%20%20adolf_data%20%3D%20b.select(%5B%22year%22%2C%20%22Adolf%22%5D).filter(pl.col(%22Adolf%22).is_not_null())%0A%0A%20%20%20%20%23%20Convert%20to%20lists%0A%20%20%20%20x%20%3D%20adolf_data%5B%22year%22%5D.to_list()%0A%20%20%20%20y%20%3D%20adolf_data%5B%22Adolf%22%5D.to_list()%0A%0A%20%20%20%20%23%20Create%20line%20chart%0A%20%20%20%20fig%20%3D%20go.Figure()%0A%20%20%20%20fig.add_trace(go.Scatter(x%3Dx%2C%20y%3Dy%2C%20mode%3D%22lines%22%2C%20name%3D%22Adolf%22))%0A%0A%20%20%20%20%23%20Add%20trace%20for%20Adolf%20data%0A%20%20%20%20%23%20fig.add_trace(go.Scatter(y%3Dadolf_data%2C%20mode%3D%22lines%22%2C%20name%3D%22Adolf%22))%0A%0A%20%20%20%20%23%20Update%20layout%0A%20%20%20%20fig.update_layout(title%3D%22Adolf%22%2C%20xaxis_title%3D%22Index%22%2C%20yaxis_title%3D%22Value%22%2C%20width%3D800%2C%20height%3D600)%0A%0A%20%20%20%20fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Polars%20equivalent%20of%20truncate%20and%20sum%0A%20%20%20%20%23%20Filter%20the%20data%20to%20include%20only%20years%20after%201946%0A%20%20%20%20%23%20Assuming%20the%20first%20column%20is%20the%20year%20column%0A%20%20%20%20year_col%20%3D%20b.columns%5B0%5D%0A%0A%20%20%20%20%23%20Filter%20the%20Adolf%20column%20for%20years%20after%201946%0A%20%20%20%20adolf_after_1946%20%3D%20b.filter(pl.col(year_col)%20%3E%201946).select(%22Adolf%22)%0A%0A%20%20%20%20%23%20Sum%20the%20values%0A%20%20%20%20adolf_sum%20%3D%20adolf_after_1946.select(pl.sum(%22Adolf%22)).item()%0A%0A%20%20%20%20print(f%22Sum%20of%20Adolf%20after%201946%3A%20%7Badolf_sum%7D%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
a4cb5f5ed51c0ea789e1cc26eedda2a3e6a7732186e98e2039fb8cdfc5f72885