1

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.

2 Answers 2

2

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')

Or Series.dt.normalize:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I just want to convert the original dataframe column from an object to a date mentions in the question ,why didn't you save in df['TDate']?
1

This snippet will convert a timestamp to date

df['TDate']= pd.to_datetime(df['TDate']).dt.date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.