在 Pandas 中将数字 sas 日期转换为日期时间

convert numeric sas date to datetime in Pandas(在 Pandas 中将数字 sas 日期转换为日期时间)
本文介绍了在 Pandas 中将数字 sas 日期转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 Pandas 0.18read_sas 加载 sas7bdat 数据集.

I am using Pandas 0.18 and read_sas to load a sas7bdat dataset.

Pandas 数据框中的日期显示为:

The dates in the Pandas dataframe appear as:

Out[56]: 
0    19411.0
1    19325.0
2    19325.0
3    19443.0
4    19778.0
Name: sas_date, dtype: float64

pd.to_datetime 无法识别此格式.我应该怎么做才能正确解析日期?

pd.to_datetime does not recognize this format. What should I do parse the date correctly?

谢谢!

推荐答案

根据这个链接,

[A] SAS 日期值是一个值,表示之间的天数1960 年 1 月 1 日和指定日期

[A] SAS date value is a value that represents the number of days between January 1, 1960, and a specified date

因此,如果我们将数字转换为 Pandas Timedeltas 并将它们添加到1960-1-1我们可以恢复日期:

Therefore, if we convert the numbers to Pandas Timedeltas and add them to 1960-1-1 we can recover the date:

import numpy as np
import pandas as pd

ser = pd.Series([19411.0, 19325.0, 19325.0, 19443.0, 19778.0])
ser = pd.to_timedelta(ser, unit='D') + pd.Timestamp('1960-1-1')

产量

0   2013-02-22
1   2012-11-28
2   2012-11-28
3   2013-03-26
4   2014-02-24
dtype: datetime64[ns]

这篇关于在 Pandas 中将数字 sas 日期转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)