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:


Once I convert it to str the date is:


The corresponding date in SAS (not in my Pandas dataset) appears to be a DATETIME21.2


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,890 points

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)
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,250 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,520 points
+2 votes
2 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,400 points
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
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
• 57,550 points
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,183 views
0 votes
0 answers
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,250 points
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,250 points