J
J
Joseph Goodman2020-07-17 13:09:42
Python
Joseph Goodman, 2020-07-17 13:09:42

How to parse dates to datetime format in Pandas?

5f11785793ae8684912462.jpeg
There is a dataframe in which the Order Date column is a string.
I want to convert it to datetime using the code below, but it doesn't work.

dates['Datka'] = pd.to_datetime(df['Order Date'], format='%m/%d/%Y %X')

Maybe I'm formatting it wrong somehow? Explain, please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-17
@lolaevv

You can immediately when reading the file:

from datetime import datetime

dateparser = lambda x: datetime.strptime(x, "%m/%d/%y %H:%M")

df = pd.read_csv('filename.csv', parse_dates = ['Order Date'], date_parser=dateparser)

Also corrected the format to match the data in the column

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question