
在Pandas中,時間序列(Time Series)是一種特殊的數(shù)據(jù)類型,用于處理時間相關(guān)的數(shù)據(jù)。Pandas提供了豐富的功能和方法,方便對時間序列數(shù)據(jù)進行處理和分析。下面是一些針對時間序列的常用操作:
方式① 使用to_datetime
創(chuàng)建時間序列:直接傳入列表即可
import pandas as pd
# 將列表轉(zhuǎn)換為時間戳
date_range = pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-03'])
date_range
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03'], dtype='datetime64[ns]', freq=None)
方式② 使用pd.date_range()
創(chuàng)建一段連續(xù)的時間范圍:使用指定參數(shù)即可
import pandas as pd
date_range = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
date_range
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
'2024-01-05', '2024-01-06', '2024-01-07', '2024-01-08',
'2024-01-09', '2024-01-10',
...
'2024-12-22', '2024-12-23', '2024-12-24', '2024-12-25',
'2024-12-26', '2024-12-27', '2024-12-28', '2024-12-29',
'2024-12-30', '2024-12-31'],
dtype='datetime64[ns]', length=366, freq='D')
其中,start是起始日期,end是結(jié)束日期,freq是頻率,這里設(shè)置為'D'表示每天。
方式③ 使用Timestamp()
函數(shù)創(chuàng)建一個特定的時間戳:使用指定參數(shù)即可
import pandas as pd
timestamp = pd.Timestamp(year=2023, month=1, day=1, hour=12, minute=30, second=45)
timestamp
Timestamp('2023-01-01 12:30:45')
方式④ 使用 datetime 模塊創(chuàng)建時間戳:使用指定參數(shù)即可
import pandas as pd
from datetime import datetime
timestamp = datetime(2023, 1, 1, 12, 30, 45)
print(timestamp)
2023-01-01 12:30:45
計算一下兩個時間數(shù)據(jù)之差
import pandas as pd
# 創(chuàng)建兩個固定時間
start_time = pd.Timestamp('2024-01-01 12:00:00')
end_time = pd.Timestamp('2024-01-02 14:30:00')
# 計算時間差
time_diff = end_time - start_time
time_diff
Timedelta('1 days 02:30:00')
一個固定時間加上pd.Timedelta
類型的時間差
pd.Timestamp('2024-01-02 14:30:00')+pd.Timedelta('1 days 02:30:00')
Timestamp('2024-01-03 17:00:00')
接下來,我們看看日期做索引的情況
將日期作為索引創(chuàng)建時間序列:
import pandas as pd
data = [1, 2, 3, 4, 5]
dates = pd.date_range(start='2024-01-01', periods=5, freq='D')
ts = pd.Series(data, index=dates)
ts
2024-01-01 1
2024-01-02 2
2024-01-03 3
2024-01-04 4
2024-01-05 5
Freq: D, dtype: int64
其中,periods是時間序列的長度,freq是頻率,這里設(shè)置為'D'表示每天。
import pandas as pd
ts['2024-01-01']
1
使用日期范圍進行切片:
import pandas as pd
ts['2024-01-01':'2024-01-05']
2024-01-01 1
2024-01-02 2
2024-01-03 3
2024-01-04 4
2024-01-05 5
Freq: D, dtype: int64
也可以使用切片操作對數(shù)據(jù)進行訪問
import pandas as pd
ts[1:4]
2024-01-02 2
2024-01-03 3
2024-01-04 4
Freq: D, dtype: int64
時間序列的重采樣: 將時間序列從高頻率轉(zhuǎn)換為低頻率:
import pandas as pd
ts.resample('W').mean()
2024-01-07 3.0
Freq: W-SUN, dtype: float64
其中,'W'表示按周進行重采樣,mean()表示計算每周的平均值。
時間序列的滾動計算: 計算滾動平均值:
import pandas as pd
ts.rolling(window=3).mean()
2024-01-01 NaN
2024-01-02 NaN
2024-01-03 2.0
2024-01-04 3.0
2024-01-05 4.0
Freq: D, dtype: float64
其中,window=3表示窗口大小為3,即計算每3個數(shù)據(jù)的平均值。
時間序列的時間偏移: 將時間序列向前或向后移動:
import pandas as pd
ts.shift(1)
2024-01-01 NaN
2024-01-02 1.0
2024-01-03 2.0
2024-01-04 3.0
2024-01-05 4.0
Freq: D, dtype: float64
其中,1表示向后移動1個時間單位。
在 Pandas 中,可以使用 dt 訪問器來訪問時間戳或時間序列中的各個時間部分,例如年、月、日、小時、分鐘、秒等。通過使用 dt 訪問器,你可以方便地提取和操作時間信息。
下面是一些常用的 dt 訪問器的示例:
import pandas as pd
# 創(chuàng)建一個時間序列
timestamps = pd.Series(pd.date_range('2023-01-01', periods=5, freq='D'))
timestamps
0 2023-01-01
1 2023-01-02
2 2023-01-03
3 2023-01-04
4 2023-01-05
dtype: datetime64[ns]
# 提取年份
year = timestamps.dt.year
year
0 2023
1 2023
2 2023
3 2023
4 2023
dtype: int64
# 提取月份
month = timestamps.dt.month
month
0 1
1 1
2 1
3 1
4 1
dtype: int64
# 提取日期
day = timestamps.dt.day
day
0 1
1 2
2 3
3 4
4 5
dtype: int64
# 提取小時
hour = timestamps.dt.hour
hour
0 0
1 0
2 0
3 0
4 0
dtype: int64
# 提取分鐘
minute = timestamps.dt.minute
minute
0 0
1 0
2 0
3 0
4 0
dtype: int64
# 提取秒數(shù)
second = timestamps.dt.second
second
0 0
1 0
2 0
3 0
4 0
dtype: int64
# 獲取季度
quarter = timestamps.dt.quarter
quarter
0 1
1 1
2 1
3 1
4 1
dtype: int64
# 獲取周數(shù)
week = timestamps.dt.isocalendar().week
week
0 52
1 1
2 1
3 1
4 1
Name: week, dtype: UInt32
# 獲取星期幾的名稱
day_name = timestamps.dt.day_name()
day_name
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
dtype: object
# 獲取該日期是一年中的第幾天
day_of_year = timestamps.dt.dayofyear
day_of_year
0 1
1 2
2 3
3 4
4 5
dtype: int64
# 獲取該日期是一周中的第幾天(星期一為1,星期日為7)
day_of_week = timestamps.dt.dayofweek + 1
day_of_week
0 7
1 1
2 2
3 3
4 4
dtype: int64
# 獲取該日期是一個月中的第幾天
day_of_month = timestamps.dt.day
day_of_month
0 1
1 2
2 3
3 4
4 5
dtype: int64
# 獲取該日期所在月份的最后一天
end_of_month = timestamps.dt.daysinmonth
end_of_month
0 31
1 31
2 31
3 31
4 31
dtype: int64
import pandas as pd
# 創(chuàng)建時間戳序列
ts = pd.Series(pd.to_timedelta(np.arange(10),unit='m'))
ts
0 0 days 00:00:00
1 0 days 00:01:00
2 0 days 00:02:00
3 0 days 00:03:00
4 0 days 00:04:00
5 0 days 00:05:00
6 0 days 00:06:00
7 0 days 00:07:00
8 0 days 00:08:00
9 0 days 00:09:00
dtype: timedelta64[ns]
# 提取時間戳中的秒數(shù)
seconds = ts.dt.seconds
seconds
0 0
1 60
2 120
3 180
4 240
5 300
6 360
7 420
8 480
9 540
dtype: int64
seconds = ts.dt.to_pytimedelta()
seconds
array([datetime.timedelta(0), datetime.timedelta(seconds=60),
datetime.timedelta(seconds=120), datetime.timedelta(seconds=180),
datetime.timedelta(seconds=240), datetime.timedelta(seconds=300),
datetime.timedelta(seconds=360), datetime.timedelta(seconds=420),
datetime.timedelta(seconds=480), datetime.timedelta(seconds=540)],
dtype=object)
以上是Pandas針對時間序列的一些常用操作和示例代碼
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認 ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準 ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機制,在 ...
2025-07-07統(tǒng)計學方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03