Home >Backend Development >Python Tutorial >How to Extract Only the Date Component from a Pandas Datetime Series?
Parsing Dates with pandas: Keeping Only the Date Component
When using pandas.to_datetime to parse dates, pandas by default represents them as datetime64[ns], even if the dates are daily-only. This can lead to undesired appending of "00:00:00" when writing the data to CSV.
Converting to datetime.date or datetime64[D]
To convert the dates to datetime.date or datetime64[D], there are a few options:
df['just_date'] = df['dates'].dt.date
This will return datetime.date objects with an object dtype.
df['normalised_date'] = df['dates'].dt.normalize()
This will set the time component to midnight (00:00:00), but the display will still show only the date value.
Precision Specification:
pandas.to_datetime does not yet support precision specification. However, the aforementioned methods provide alternative ways to obtain the desired date representation.
The above is the detailed content of How to Extract Only the Date Component from a Pandas Datetime Series?. For more information, please follow other related articles on the PHP Chinese website!