本文介紹了在 Pandas 中將數字 sas 日期轉換為日期時間的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用 Pandas 0.18 和 read_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 日期轉換為日期時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!