We can extract the date from a datetime column in the following ways:
.dt accessornormalize() method.dt accessorThe .dt accessor method is used to access the datetime-like properties for the series.
Note: We can refer to .dt accessor to learn more about
.dtaccessor.
Let’s look at the code below:
import pandas as pddf = pd.DataFrame({"timestamp": ["2013-01-01 09:10:12", "2022-01-02 09:10:12", "2022-01-03 09:10:12", "2022-01-04 09:10:12"]})print(pd.to_datetime(df['timestamp']).dt.date)
pandas module.df with a timestamp column.timestamp column by accessing the date of dt accessor.normalize() methodThe normalize() method converts times to midnight. It sets all the values to 00:00:00.
Let’s look at the code below:
import pandas as pddf = pd.DataFrame({"timestamp": ["2013-01-01 09:10:12", "2022-01-02 09:10:12", "2022-01-03 09:10:12", "2022-01-04 09:10:12"]})print(pd.to_datetime(df['timestamp']).dt.normalize())
pandas module.df with a timestamp column.timestamp column by invoking the normalize() function of the dt accessor.