convert a SAS datetime in Pandas(在 Pandas 中转换 SAS 日期时间)
问题描述
我正在使用 Pandas 通过 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 中的相应日期(不在我的 Pandas 数据集中)似乎是 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
有什么想法吗?谢谢!
推荐答案
SAS 日期值
是一个值,表示从 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
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]
这篇关于在 Pandas 中转换 SAS 日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Pandas 中转换 SAS 日期时间


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01