5

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

Any ideas? Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    Check out this question. The module datetime has the converter you need, I think: http://stackoverflow.com/a/26923644/4633893 – Robert Rodkey Apr 08 '16 at 13:22

2 Answers2

11

SAS date value

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

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]
Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

You will get the right date in Python by using format='sas7bdat' option in your read_sas() method. For example, I used: pd.read_sas(dataset, format='sas7bdat'), and the dates got translated correctly to python dataframe.

Kunal
  • 21
  • 1