The easiest way is to use to_datetime:
Copydf['col'] = pd.to_datetime(df['col'])
It also offers a dayfirst argument for European times (but beware this isn't strict).
Here it is in action:
CopyIn [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0 2005-05-23 00:00:00
dtype: datetime64[ns]
You can pass a specific format:
CopyIn [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0 2005-05-23
dtype: datetime64[ns]
Answer from Andy Hayden on Stack OverflowThe easiest way is to use to_datetime:
Copydf['col'] = pd.to_datetime(df['col'])
It also offers a dayfirst argument for European times (but beware this isn't strict).
Here it is in action:
CopyIn [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0 2005-05-23 00:00:00
dtype: datetime64[ns]
You can pass a specific format:
CopyIn [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0 2005-05-23
dtype: datetime64[ns]
If your date column is a string of the format '2017-01-01' you can use pandas astype to convert it to datetime.
Copydf['date'] = df['date'].astype('datetime64[ns]')
or use datetime64[D] if you want Day precision and not nanoseconds
Copyprint(type(df['date'].iloc[0]))
yields
Copy<class 'pandas._libs.tslib.Timestamp'>
the same as when you use pandas.to_datetime
You can try it with other formats then '%Y-%m-%d' but at least this works.
Pandas convert Object to Datetime
The format in your error isn't the same as the line above, no space between `%A,` and `%b`. You sure there isn't a typo in your original format. This conversion isn't really going to work without some year information though.
x = ['Thursday, Mar 9', 'Wednesday, Mar 10'] pd.to_datetime(x, format='%A, %b %d') DatetimeIndex(['1900-03-09', '1900-03-10'], dtype='datetime64[ns]', freq=None)
More on reddit.com
Can a pandas column be quickly converted to datetime if the column values contain letters?
Pandas question - Multiple Datetime columns to Dates
how to convert pandas columns of dates formatted like "2020-01-02 23:59:56.078191" to datetime object, then to ms epoch?
Videos
Good morning, I have been struggling with converting a pandas dataframe column from Object type to Datetime. The dates in the column are in the following format:
Thursday, Mar 9
I have tried several variations of the following code:
df['date']=pd.to_datetime(df['date'], format='%A, %b %d')
Every different variation of the above code returns an error saying
the errortime data 'Thursday, Mar 9' does not match format '%A,%b %d'
I feel like this should be simple but I'm banging my head against the wall trying to get over this. Thanks in advance for any help
The format in your error isn't the same as the line above, no space between `%A,` and `%b`. You sure there isn't a typo in your original format. This conversion isn't really going to work without some year information though.
x = ['Thursday, Mar 9', 'Wednesday, Mar 10']
pd.to_datetime(x, format='%A, %b %d')
DatetimeIndex(['1900-03-09', '1900-03-10'], dtype='datetime64[ns]', freq=None)
I could be wrong, but the %d means
%d Day of the month as a zero-padded decimal number.
So the 9 should be 09
There is the %-d option
%-d Day of the month as a decimal number. (Platform specific)
More info about time formats here
I have a pandas column with ~100k rows that contains date strings. I'm attempting to convert the strings to datetime objects, but the strings currently contain a letter. Here's an example:
'2020-11-10T02:00:12.000'
Currently, I'm updating the column ('created_date') row by row to remove the letter, like so:
for date in df.created_date:
df.created_date.loc[date] = date[0:10] + ' ' + date[11:] This would allow me to convert the column values to datetime objects with the following code:
df['created_date'] = pd.to_datetime(
df['created_date'],
format='%Y-%m-%d %H:%M:%S.%f'
) However, that first block of code is taking forever to run because there are so many rows. Is there a way to use pd.to_datetime, without that preprocessing step, to accomplish this task more quickly?
Thanks in advance for any suggestions!