Search code examples
pythondatepython-polars

How to calculate the month begin and end month date from date in polars?


Is there an efficient way to get the month end date on a date column. Like if date =‘2023-02-13” to return “2023-02-28”, also beginning of the month would be great as well. Thanks!

df = pl.DataFrame({'DateColumn': ['2022-02-13']})

test_df = df.with_columns([
    pl.col('DateColumn').str.strptime(pl.Date).cast(pl.Date)
]
)

┌────────────┐
│ DateColumn │
│ ---        │
│ date       │
╞════════════╡
│ 2022-02-13 │
└────────────┘

Two new columns would be perfect.


Solution

  • [Update]: Polars has since added .month_start() and .month_end() methods.

    See the answer from @n-maks


    You could use .truncate and .offset_by

    test_df.with_columns(
       MonthStart = pl.col("DateColumn").dt.truncate("1mo"),
       MonthEnd = pl.col("DateColumn").dt.offset_by("1mo").dt.truncate("1mo").dt.offset_by("-1d")
    )
    
    shape: (1, 3)
    ┌────────────┬────────────┬────────────┐
    │ DateColumn | MonthStart | MonthEnd   │
    │ ---        | ---        | ---        │
    │ date       | date       | date       │
    ╞════════════╪════════════╪════════════╡
    │ 2022-02-13 | 2022-02-01 | 2022-02-28 │
    └────────────┴────────────┴────────────┘