問(wèn)題描述
我正在使用 Pandas 通過(guò) read_sas
SAS 數(shù)據(jù)集中有一個(gè) datetime 變量,在 Pandas 中顯示為:
There is a datetime variable in the SAS dataset, which appears in Pandas as:
1.775376e+09
將其轉(zhuǎn)換為 str
后,日期為:
Once I convert it to str
the date is:
1775376002.0
SAS 中的相應(yīng)日期(不在我的 Pandas 數(shù)據(jù)集中)似乎是 DATETIME21.2
The corresponding date in SAS (not in my Pandas dataset) appears to be a DATETIME21.2
04APR2016:08:00:02.00
我嘗試使用轉(zhuǎn)換它
pd.to_datetime(df.mysasdate,format='%d%m%Y%H%M%S')
沒(méi)有成功
TypeError: 'float' object is unsliceable
有什么想法嗎?謝謝!
推薦答案
SAS 日期值
是一個(gè)值,表示從 1960 年 1 月 1 日到指定日期之間的天數(shù).鏈接
is a value that represents the number of days between January 1, 1960, and a specified date. link
所以你可以轉(zhuǎn)換數(shù)字 to_timedelta代碼> 并添加
date
1960-01-01 00:00:00
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]
這篇關(guān)于在 Pandas 中轉(zhuǎn)換 SAS 日期時(shí)間的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!