将SAS数字转换为python datetime [英] Convert SAS numeric to python datetime
本文介绍了将SAS数字转换为python datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个简单的解决方案,但是如何转换SAS日期时间数字(自1/1960以来的秒数)。pandas列中的一个值的示例是1716470000。 p>
我试过:
df ['PyDatetime'] = pd.to_datetime (df,infer_datetime_format = True)
我得到的数字如'1970-01-01 00:00 :01.725480'
解决方案
你需要内置的 datetime
模块:
import datetime
sastime = 1716470000
epoch = datetime.datetime(1960,1,1 )
print(epoch + datetime.timedelta(seconds = sastime))
哪个显示:
datetime.datetime(2014,5,23,13,13,20)#这是对吗?
所以如果你有一个名为 sastime
,你可以这样做:
epoch = datetime.datetime(1960,1,1)
#cast -s-到python int,以防它是一个字符串或numpy.int
df ['datetime'] = df ['sastime']。apply(
lambda s:epoch + datetime.timedelta(seconds = int(s))
)
This is probably a simple solution, but how would I go about converting a SAS datetime number (number of seconds since 1/1/1960.) An example of one of the values inside of the pandas column is 1716470000.
I tried:
df['PyDatetime'] = pd.to_datetime(df,infer_datetime_format=True)
and I get numbers like '1970-01-01 00:00:01.725480'
解决方案
You'll need the built-in datetime
module:
import datetime
sastime = 1716470000
epoch = datetime.datetime(1960, 1, 1)
print(epoch + datetime.timedelta(seconds=sastime))
Which shows:
datetime.datetime(2014, 5, 23, 13, 13, 20) # is this right?
So if you have a dataframe with a column called sastime
, you could do:
epoch = datetime.datetime(1960, 1, 1)
# cast -s- to python int in case it's a string or a numpy.int
df['datetime'] = df['sastime'].apply(
lambda s: epoch + datetime.timedelta(seconds=int(s))
)
这篇关于将SAS数字转换为python datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文