Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

python - convert a SAS datetime in Pandas

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...