简体   繁体   中英

Pandas, wrong datetime format

I have a df, contains a date of 2999-01-01, which doesn't match format specified.

    a date
prod1 2019/02/23
prod2 2999/01/02

when I use:

df.date=pd.to_datetime(df.date_DATE,format='%Y-%m-%d')

it returns error:time data '2999-01-01 00:00:00.0' doesn't match format specified

How to fix? Thanks

Use parameter errors='coerce' for convert datetimes outside of limits to NaT , also is changed format to %Y/%m/%d because separator is / :

df['date'] = pd.to_datetime(df['date'],format='%Y/%m/%d', errors='coerce')
print (df)
       a       date
0  prod1 2019-02-23
1  prod2        NaT

Since pd.Timestamp is represented as a long of nanoseconds , the maximum representable Timestamp is in 2262 (specifically, pd.Timestamp.max ), so what you are doing will never work.

If you don't mind representing your time as a day-long pd.Period , you could use that instead. For instance:

In [12]: df
Out[12]:
       a        date
0  prod1  2019/02/23
1  prod2  2999/01/02

In [13]: df.date.apply(pd.Period)
Out[13]:
0   2019-02-23
1   2999-01-02
Name: date, dtype: object

In [23]: df.date.apply(pd.Period)[1]
Out[23]: Period('2999-01-02', 'D')

Just something came across my mind:

import datetime    
df.Date.apply(lambda x:datetime.datetime.strftime(x,'%Y-%m-%d'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM