Convert SAS Date and Time to Pandas

I use Pandas to read a Sas dataset using read_sas

There is a datetime variable in the SAS dataset, which appears in Pandas as:

1.775376e+09

As soon as I convert it to str , the date is:

1775376002.0

The corresponding date in SAS (not in my Pandas dataset) looks like DATETIME21.2

04APR2016:08:00:02.00

I tried to convert it using

pd.to_datetime(df.mysasdate,format='%d%m%Y%H%M%S') without success

 TypeError: 'float' object is unsliceable 

Any ideas? Thanks!

+5
source share
1 answer

SAS Date Value

is a value that represents the number of days between January 1, 1960 and the specified date. link

So you can convert to_timedelta number and add date 1960-01-01 00:00:00

 df = pd.DataFrame({'mysasdate':[1775376002.0, 1775377002.0]}) print (df) mysasdate 0 1.775376e+09 1 1.775377e+09 print (pd.to_timedelta(df['mysasdate'], unit='s') + pd.datetime(1960, 1, 1)) 0 2016-04-04 08:00:02 1 2016-04-04 08:16:42 Name: mysasdate, dtype: datetime64[ns] 
+9
source

Source: https://habr.com/ru/post/1246702/


All Articles