Reputation: 122
My dataset has values in date column as in following format:
date
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
[datetime.date(2017, 2, 17)]
I want to convert them into datatype of datetime so that I cant perform time series analysis over it.
i have written in following way: but its giving me value error
df_scores['date']=pd.to_datetime(df_scores['date'],format='[datetime.date("%Y, %m, %d")]')
Upvotes: 1
Views: 450
Reputation: 1958
This should work:
df_scores['date']=pd.to_datetime(df_scores['date'],format="[datetime.date(%Y, %m, %d)]")
Upvotes: 0
Reputation: 402263
If your column is a column of date
objects in a list, this should suffice -
df.date = pd.to_datetime(df_scores['date'].str[0], errors='coerce')
If you have a column of strings, you could use str.findall
to extract the date artifacts, and str.join
to join them in a format that to_datetime
understands.
i = df.date.astype(str)\
.str.findall('\d+')\
.str.join('/')
df.date = pd.to_datetime(i, errors='coerce')
The astype(str)
is required if you have a column of list of strings, rather than a column of strings. You may also need another parameter errors='coerce'
if you have malformed data in your column.
df
date
0 2017-02-17
1 2017-02-17
2 2017-02-17
3 2017-02-17
4 2017-02-17
5 2017-02-17
6 2017-02-17
7 2017-02-17
8 2017-02-17
9 2017-02-17
10 2017-02-17
11 2017-02-17
Upvotes: 1
Reputation: 3560
if you from datetime import datetime
then you should specify dates like this:
[datetime(2017, 2, 17)]
This will result in entries of type <class 'pandas._libs.tslib.Timestamp'>
which you can convert to datetime using .to_datetime()
Upvotes: 0