在 pandas 转换SAS日期时间 [英] convert a SAS datetime in Pandas
问题描述
我使用熊猫阅读 Sas 数据集,使用 read_sas
I am using Pandas to read a Sas dataset using read_sas
SAS数据集中有一个datetime变量,它在Pandas中显示为:
There is a datetime variable in the SAS dataset, which appears in Pandas as:
1.775376e + 09
一旦将其转换为 str
,日期为:
Once I convert it to str
the date is:
1775376002.0
SAS中的相应日期(不是我的熊猫数据集)似乎成为 DATETIME21.2
The corresponding date in SAS (not in my Pandas dataset) appears to be a DATETIME21.2
04APR2016:08:00:02.00
我尝试使用
pd.to_datetime(df .mysasdate,format ='%d%m%Y%H%M%S')
没有成功
TypeError: 'float' object is unsliceable
任何想法?
谢谢!
Any ideas? Thanks!
推荐答案
我想你可以尝试更改%m
到%b
并添加%f
:
I think you can try change %m
to %b
and add %f
:
print pd.to_datetime(df.mysasdate,format='%d%b%Y:%H:%M:%S.%f')
0 2016-04-04 08:00:02
Name: mysasdate, dtype: datetime64[ns]
更多信息,请参阅此处。
编辑:
SAS日期值
SAS date value
是一个代表1960年1月1日之前的天数的值,和一个指定的日期。 链接
is a value that represents the number of days between January 1, 1960, and a specified date. link
所以你可以转换号码 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
print df
mysasdate
0 1775376002.0
print pd.to_timedelta(df.mysasdate, unit='s') + pd.datetime(1960, 1, 1)
0 2016-04-04 08:00:02
Name: mysasdate, dtype: datetime64[ns]
这篇关于在 pandas 转换SAS日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!