【Python數據分析】pandas時刻數據:Timestamp


所謂的時刻數據代表時間點,是pandas的數據類型,是將值與時間點相關聯的最基本類型的時間序列數據。

1.Timestamp

Timestamp是將數據類型轉化為pandas的Timestamp類型

import pandas as pd
import datetime

date1 = datetime.datetime(2019, 12, 31, 12, 1, 2) # 創建一個datetime.datetime
date2 = '2017-12-31' # 創建一個字符串

t1 = pd.Timestamp(date1)
t2 = pd.Timestamp(date2)

print(t1,type(t1))
print(t2,type(t2))

2.to_datetime

to_datetime也是將數據類轉化為pandas的Timestamp類型,但是如果是多個時間,則會轉化為pandas的DatetimeIndex

2.1 單個時間轉化

import pandas as pd
from datetime import datetime

date1 = datetime(2019, 12, 31, 12, 1, 2) # 創建一個datetime.datetime
date2 = '2017-12-31' # 創建一個字符串

# pd.to_datetime():如果是單個時間數據,轉換成pandas的時刻數據,數據類型為Timestamp
t1 = pd.to_datetime(date1)
t2 = pd.to_datetime(date2)

print(t1,type(t1))
print(t2,type(t2))

2.2 多個時間轉化

import pandas as pd
from datetime import datetime

# 多個時間數據,將會轉換為pandas的DatetimeIndex
list_date = ['2019-12-31','2020-01-01','2020-01-02']
t3 = pd.to_datetime(list_date)
print(t3,type(t3))

print('-' * 50) # 分割線
date1 = [datetime(2018,6,1),datetime(2018,7,1),datetime(2018,8,1),datetime(2018,9,1),datetime(2018,10,1)]
date2 = ['2019-2-1','2019-2-2','2019-2-3','2019-2-4','2019-2-5','2019-2-6']
print(date1)
print(date2)

print('-' * 50) # 分割線
t1 = pd.to_datetime(date1)
t2 = pd.to_datetime(date2)
print(t1)
print(t2)

print('-' * 50) # 分割線
date3 = ['2017-2-1','2017-2-2','2017-2-3','hello world!','2017-2-5','2017-2-6']
t3 = pd.to_datetime(date3, errors = 'ignore')
print(t3,type(t3))
# 當一組時間序列中夾雜其他格式數據,可用errors參數返回
# errors = 'ignore':不可解析時返回原始輸入,這里就是直接生成一般數組

t4 = pd.to_datetime(date3, errors = 'coerce')
print(t4,type(t4))
# errors = 'coerce':不可擴展,缺失值返回NaT(Not a Time),結果認為DatetimeIndex


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM