[Python] How to perform pandas reindex in polars

Discussão em 'Python' iniciado por Stack, Outubro 31, 2024.

  1. Stack

    Stack Membro Participativo

    In pandas, I can reindex() the dataframe using multi-index to make the date range consistent for each group.

    See example below using pandas:

    import pandas as pd
    import polars as pl

    df = pl.from_repr("""
    ┌────────────┬───────┬───────┐
    │ date ┆ group ┆ value │
    │ --- ┆ --- ┆ --- │
    │ date ┆ str ┆ i64 │
    ╞════════════╪═══════╪═══════╡
    │ 2022-01-01 ┆ A ┆ 10 │
    │ 2022-02-01 ┆ A ┆ 20 │
    │ 2022-03-01 ┆ A ┆ 30 │
    │ 2022-04-01 ┆ B ┆ 40 │
    │ 2022-05-01 ┆ B ┆ 50 │
    │ 2022-06-01 ┆ B ┆ 60 │
    └────────────┴───────┴───────┘
    """)

    df_pd = df.to_pandas().set_index(["group", "date"])

    new_index = pd.MultiIndex.from_product([df_pd.index.levels[0].tolist(), df_pd.index.levels[1].tolist()], names=["group", "date"])

    result = df_pd.reindex(new_index)

    with pl.Config(tbl_rows=13):
    pl.from_pandas(result.reset_index())


    which gives a result with both groups having the same date range:

    shape: (12, 3)
    ┌───────┬─────────────────────┬───────┐
    │ group ┆ date ┆ value │
    │ --- ┆ --- ┆ --- │
    │ str ┆ datetime[ns] ┆ f64 │
    ╞═══════╪═════════════════════╪═══════╡
    │ A ┆ 2022-01-01 00:00:00 ┆ 10.0 │
    │ A ┆ 2022-02-01 00:00:00 ┆ 20.0 │
    │ A ┆ 2022-03-01 00:00:00 ┆ 30.0 │
    │ A ┆ 2022-04-01 00:00:00 ┆ null │
    │ A ┆ 2022-05-01 00:00:00 ┆ null │
    │ A ┆ 2022-06-01 00:00:00 ┆ null │
    │ B ┆ 2022-01-01 00:00:00 ┆ null │
    │ B ┆ 2022-02-01 00:00:00 ┆ null │
    │ B ┆ 2022-03-01 00:00:00 ┆ null │
    │ B ┆ 2022-04-01 00:00:00 ┆ 40.0 │
    │ B ┆ 2022-05-01 00:00:00 ┆ 50.0 │
    │ B ┆ 2022-06-01 00:00:00 ┆ 60.0 │
    └───────┴─────────────────────┴───────┘


    Is there any way to produce the same result in polars directly?

    Continue reading...

Compartilhe esta Página