I use pandas.to_datetime
to parse the dates in my data. Pandas by default represents the dates with datetime64[ns]
even though the dates are all daily only.
I wonder whether there is an elegant/clever way to convert the dates to datetime.date
or datetime64[D]
so that, when I write the data to CSV, the dates are not appended with 00:00:00
. I know I can convert the type manually element-by-element:
[dt.to_datetime().date() for dt in df.dates]
But this is really slow since I have many rows and it sort of defeats the purpose of using pandas.to_datetime
. Is there a way to convert the dtype
of the entire column at once? Or alternatively, does pandas.to_datetime
support a precision specification so that I can get rid of the time part while working with daily data?
Since version 0.15.0
this can now be easily done using .dt
to access just the date component:
df['just_date'] = df['dates'].dt.date
The above returns a datetime.date
dtype, if you want to have a datetime64
then you can just normalize
the time component to midnight so it sets all the values to 00:00:00
:
df['normalised_date'] = df['dates'].dt.normalize()
This keeps the dtype
as datetime64
, but the display shows just the date
value.