Python數據分析實戰【第三章】2.9-Pandas時刻數據:Timestamp【python】

【課程2.9】 Pandas時刻數據:Timestamp

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

pandas.Timestamp()

1.pd.Timestamp()


import numpy as np
import pandas as pd

date1 = datetime.datetime(2016,12,1,12,45,30)  # 創建一個datetime.datetime
date2 = '2017-12-21'  # 創建一個字符串
t1 = pd.Timestamp(date1)
t2 = pd.Timestamp(date2)
print(t1,type(t1))
print(t2)
print(pd.Timestamp('2017-12-21 15:00:22'))
# 直接生成pandas的時刻數據 → 時間戳
# 數據類型爲 pandas的Timestamp
-----------------------------------------------------------------------
2016-12-01 12:45:30 <class 'pandas.tslib.Timestamp'>
2017-12-21 00:00:00
2017-12-21 15:00:22

2.pd.to_datetime單個數據

from datetime import datetime

date1 = datetime(2016,12,1,12,45,30)
date2 = '2017-12-21'
t1 = pd.to_datetime(date1)
t2 = pd.to_datetime(date2)
print(t1,type(t1))
print(t2,type(t2))
# pd.to_datetime():如果是單個時間數據,轉換成pandas的時刻數據,數據類型爲Timestamp

lst_date = [ '2017-12-21', '2017-12-22', '2017-12-23']
t3 = pd.to_datetime(lst_date)
print(t3,type(t3))
# 多個時間數據,將會轉換爲pandas的DatetimeIndex
-----------------------------------------------------------------------
2016-12-01 12:45:30 <class 'pandas.tslib.Timestamp'>
2017-12-21 00:00:00 <class 'pandas.tslib.Timestamp'>
DatetimeIndex(['2017-12-21', '2017-12-22', '2017-12-23'], dtype='datetime64[ns]', freq=None) <class 'pandas.tseries.index.DatetimeIndex'>

3.pd.to_datetime → 多個時間數據轉換時間戳索引

date1 = [datetime(2015,6,1),datetime(2015,7,1),datetime(2015,8,1),datetime(2015,9,1),datetime(2015,10,1)]
date2 = ['2017-2-1','2017-2-2','2017-2-3','2017-2-4','2017-2-5','2017-2-6']
print(date1)
print(date2)
t1 = pd.to_datetime(date2)
t2 = pd.to_datetime(date2)
print(t1)
print(t2)
# 多個時間數據轉換爲 DatetimeIndex

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
-----------------------------------------------------------------------
[datetime.datetime(2015, 6, 1, 0, 0), datetime.datetime(2015, 7, 1, 0, 0), datetime.datetime(2015, 8, 1, 0, 0), datetime.datetime(2015, 9, 1, 0, 0), datetime.datetime(2015, 10, 1, 0, 0)]
['2017-2-1', '2017-2-2', '2017-2-3', '2017-2-4', '2017-2-5', '2017-2-6']
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04',
               '2017-02-05', '2017-02-06'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', '2017-02-04',
               '2017-02-05', '2017-02-06'],
              dtype='datetime64[ns]', freq=None)
['2017-2-1' '2017-2-2' '2017-2-3' 'hello world!' '2017-2-5' '2017-2-6'] <class 'numpy.ndarray'>
DatetimeIndex(['2017-02-01', '2017-02-02', '2017-02-03', 'NaT', '2017-02-05',
               '2017-02-06'],
              dtype='datetime64[ns]', freq=None) <class 'pandas.tseries.index.DatetimeIndex'>

【課程2.9 Pandas時刻數據:Timestamp】 課程作業
作業1:請通過迭代創建一個時間列表(如圖,時間區間爲任意一個月,這裏可以用for語句),然後轉化成DatetimeIndex,並print月中是多少號。
在這裏插入圖片描述

datelst = []
for i in range(1,32):
    datelst.append('2017-12-%i' %i)
print('創建時間列表爲:\n',datelst,'\n------')
t = pd.to_datetime(datelst)
print('轉化成DatetimeIndex爲:\n',t,'\n------')
n = (len(t) + 1)/2 - 1
print('月中日期爲:\n',t[int(n)],'\n------')

作業2:請如圖創建一個包含時間日期的txt文件,通過open語句讀取後轉化成DatetimeIndex
在這裏插入圖片描述
在這裏插入圖片描述

f = open('C:/Users/Hjx/Desktop/date.txt','r')
s = f.readline()
datelst = s.split(',')
print('讀取txt文件爲:\n',datelst,'\n------')
t = pd.to_datetime(datelst)
print('轉化成DatetimeIndex爲:\n',t,'\n------')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章