Converting SAS to numeric time in python

This is probably a simple solution, but how can I go about converting the datetime SAS number (number of seconds since 1/1/1960.) An example of one of the values ​​inside the pandas column is 1716470000.

I tried:

df['PyDatetime'] = pd.to_datetime(df,infer_datetime_format=True) 

and I get digits like '1970-01-01 00: 00: 01.725480'

+5
source share
1 answer

You will need the built-in datetime module:

 import datetime sastime = 1716470000 epoch = datetime.datetime(1960, 1, 1) print(epoch + datetime.timedelta(seconds=sastime)) 

Which shows:

 datetime.datetime(2014, 5, 23, 13, 13, 20) # is this right? 

So, if you have a dataframe with a sastime column, you can do:

 epoch = datetime.datetime(1960, 1, 1) # cast -s- to python int in case it a string or a numpy.int df['datetime'] = df['sastime'].apply( lambda s: epoch + datetime.timedelta(seconds=int(s)) ) 
+6
source

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


All Articles