Search code examples
pythondataframedata-manipulationpython-polars

How to transform polars datetime column into a string column?


I'm trying to change a datetime column to a string column using polars library. I only want the dates on the new column:

import polars as pl

df
shape: (139878, 1)
┌─────────────────────┐
│ date_time           │
│ ---                 │
│ datetime[ns]        │
╞═════════════════════╡
│ 2007-04-19 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2007-05-02 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2007-05-03 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2007-05-03 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤

The solution below is including the time, I just need the date.

df.with_column(pl.col('date_time').cast(pl.Utf8))
shape: (14, 1)
┌───────────────────────────────┐
│ date_time                     │
│ ---                           │
│ str                           │
╞═══════════════════════════════╡
│ 2017-06-19 00:00:00.000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2017-11-13 00:00:00.000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2018-01-24 00:00:00.000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2018-01-29 00:00:00.000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤

Solution

  • You should try this:

    # Polars
    df = df.with_columns(df['date_time'].dt.strftime('%Y-%m-%d'))
    
    # Pandas
    df['date_time'] = df['date_time'].dt.strftime('%Y-%m-%d')
    

    Edit: added Polars