pabneedshelp
pabneedshelp

Reputation: 1

How do I format date using pandas?

My data 'df' shows data 'Date' as 1970-01-01 00:00:00.019990103 when this is formatted to date_to using pandas. How do I show the date as 01/03/1999?

Upvotes: 0

Views: 60

Answers (2)

Valdi_Bo
Valdi_Bo

Reputation: 30971

I see that the actual date is in last 10 chars of your source string.

To convert such strings to a Timestamp (ignoring the starting part), run:

df.Date = df.Date.apply(lambda src: pd.to_datetime(src[-8:]))

It is worth to consider to keep this date just as Timestamp, as it simplifies operations on date / time and apply your formatting only in printouts.

But if you want to have this date as a string in "your" format, in the "original" column, perform the second conversion (Timestamp to string):

df.Date = df.Date.dt.strftime('%m/%d/%Y')

Upvotes: 0

agm
agm

Reputation: 327

consider LoneWanderer's comment for next time and show some of the code that you have tried.

I would try this:

from datetime import datetime

now = datetime.now()

print(now.strftime('%d/%m/%Y'))

You can print now to see that is in the same format that you have and after that is formatted to the format required.

Upvotes: 1

Related Questions