convert a SAS datetime in Pandas

0 votes

I am using 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

Once I convert it to str the date is:

1775376002.0

The corresponding date in SAS (not in my Pandas dataset) appears to be a 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') with no success

TypeError: 'float' object is unsliceable

Can anyone help me fix this issue?

Jul 8, 2019 in Python by ana1504.k
• 7,910 points
3,335 views

1 answer to this question.

0 votes

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

So you can convert number to_timedelta 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]
answered Jul 8, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,560 points
34,471 views
+2 votes
4 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,510 points
122,497 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,483 views
0 votes
1 answer

Pandas DataFrames in a loop, df.to_csv()

can you try something like this? not ...READ MORE

answered Sep 25, 2018 in Python by Priyaj
• 58,020 points
10,430 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 13,300 points
715 views
0 votes
1 answer

How to use a global variable in function?

The global variable can be used in ...READ MORE

answered Sep 27, 2018 in Python by SDeb
• 13,300 points
941 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP