Reputation: 6668
I am using python version 3.7.
I have a dataframe, df that contains one column called TDate.
The column looks like below.
2019-01-01 00:00:00
2019-01-02 00:00:00
2019-01-03 00:00:00
2019-01-04 00:00:00
When I do df.dtypes it tell me the column is of type object.
I then have the line below,
myDates = pd.to_datetime(df['TDate'])
So myDates is a pandas Series.
However if I access one element of this myDates series and check the type it tells me it is a libs.tslib.timestamps.Timestamp
I just want to convert the original dataframe column from an object to a date in the format yyyy-mm-dd. What is the best way of doing this?
I looked at converting a timestamp to a date but that didn't work as the timestamp is a string literal not a integer.
Upvotes: 1
Views: 965
Reputation: 3739
This snippet will convert a timestamp to date
df['TDate']= pd.to_datetime(df['TDate']).dt.date
Upvotes: 1
Reputation: 862441
Use Series.dt.floor
for remove time information from column (or better it is set to default 00:00:00
value in each datetime):
myDates = pd.to_datetime(df['TDate']).dt.floor('d')
#for save to same column
#df['TDate'] = pd.to_datetime(df['TDate']).dt.floor('d')
myDates = pd.to_datetime(df['TDate']).dt.normalize()
#for save to same column
#df['TDate'] = pd.to_datetime(df['TDate']).dt.normalize()
If use:
myDates = pd.to_datetime(df['TDate']).dt.date
then output is python dates objects, so most datetimelike functions failed.
Upvotes: 2