Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can only use .dt accessor with datetimelike values

When i trying to convert the string to datetime, I am facing some issues.

testdata['pe']
Out[69]: 
0     2019-09-26
1     2019-10-31
2     2019-08-28
3     2019-06-20
4     2019-09-30
5     2019-07-22
6     2019-07-31
7     2019-08-30
8     2019-07-29
9     2019-06-28
10    2019-11-20
Name: prd_exp_dt, dtype: object

When i did, testdata.pe=testdata.pe.dt.strftime('%Y%M'), i get this error:

Can only use .dt accessor with datetimelike values

Tried to do errors:"corerce", like this:

pandas.to_datetime(testdata['prd_exp_dt'],errors = 'coerce',format = '%Y%M')
Out[68]: 
0    NaT
1    NaT
2    NaT
3    NaT
4    NaT
5    NaT
6    NaT
7    NaT
8    NaT
9    NaT
10   NaT
Name: prd_exp_dt, dtype: datetime64[ns]

Need some guidance on this.

like image 442
lakshmen Avatar asked Oct 28 '25 01:10

lakshmen


1 Answers

Use format '%Y-%m-%d' and use .dt.strftime("%Y%m")

Ex:

import pandas as pd
testdata['prd_exp_dt'] = pd.to_datetime(testdata['prd_exp_dt'] ,errors = 'coerce',format = '%Y-%m-%d').dt.strftime("%Y%m")
like image 167
Rakesh Avatar answered Oct 30 '25 18:10

Rakesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!