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 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
CodePudding user response:
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
CodePudding user response:
I eventually got this workaround but @ErnestBidouille answer was more of what I was looking for.
import polars as pl
df = df.with_column(pl.col('date_time').cast(pl.Date).cast(pl.Utf8))
print(df)
shape: (14, 1)
┌────────────┐
│ date_time │
│ --- │
│ str │
╞════════════╡
│ 2017-06-19 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2017-11-13 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2018-01-24 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2018-01-29 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤