日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Pandas.DataFrame時間序列數據處理的實現_python

作者:餃子大人 ? 更新時間: 2023-06-04 編程語言

將pandas.DataFrame,pandas.Series的索引設置為datetime64 [ns]類型時,將其視為DatetimeIndex,并且可以使用各種處理時間序列數據的函數。

可以按年或月指定行,并按切片指定提取周期,這在處理包含日期和時間信息(例如日期和時間)的數據時非常方便。

在此,將對以下內容進行描述。

  • 如何將一列現有數據指定為DatetimeIndex
  • 讀取CSV時如何指定DatetimeIndex
  • 關于pandas.Series

如何將一列現有數據指定為DatetimeIndex

將pandas.DataFrame與默認的基于0的索引和一個字符串列作為日期。

import pandas as pd

df = pd.read_csv('./data/26/sample_date.csv')
print(df)
# ? ? ? ? ? date ?val_1 ?val_2
# 0 ? 2017-11-01 ? ? 65 ? ? 76
# 1 ? 2017-11-07 ? ? 26 ? ? 66
# 2 ? 2017-11-18 ? ? 47 ? ? 47
# 3 ? 2017-11-27 ? ? 20 ? ? 38
# 4 ? 2017-12-05 ? ? 65 ? ? 85
# 5 ? 2017-12-12 ? ? ?4 ? ? 29
# 6 ? 2017-12-22 ? ? 31 ? ? 54
# 7 ? 2017-12-29 ? ? 21 ? ? ?8
# 8 ? 2018-01-03 ? ? 98 ? ? 76
# 9 ? 2018-01-08 ? ? 48 ? ? 64
# 10 ?2018-01-19 ? ? 18 ? ? 48
# 11 ?2018-01-23 ? ? 86 ? ? 70

print(type(df.index))
# <class 'pandas.core.indexes.range.RangeIndex'>

print(df['date'].dtype)
# object

將to_datetime()應用于日期字符串列,并轉換為datetime64 [ns]類型。

df['date'] = pd.to_datetime(df['date'])
print(df['date'].dtype)
# datetime64[ns]

使用set_index()方法將datetime64 [ns]類型的列指定為索引。

Pandas.DataFrame,重置列的行名(set_index)

索引現在是DatetimeIndex。索引的每個元素都是時間戳類型。

df.set_index('date', inplace=True)
print(df)
# ? ? ? ? ? ? val_1 ?val_2
# date ? ? ? ? ? ? ? ? ? ?
# 2017-11-01 ? ? 65 ? ? 76
# 2017-11-07 ? ? 26 ? ? 66
# 2017-11-18 ? ? 47 ? ? 47
# 2017-11-27 ? ? 20 ? ? 38
# 2017-12-05 ? ? 65 ? ? 85
# 2017-12-12 ? ? ?4 ? ? 29
# 2017-12-22 ? ? 31 ? ? 54
# 2017-12-29 ? ? 21 ? ? ?8
# 2018-01-03 ? ? 98 ? ? 76
# 2018-01-08 ? ? 48 ? ? 64
# 2018-01-19 ? ? 18 ? ? 48
# 2018-01-23 ? ? 86 ? ? 70

print(type(df.index))
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

print(df.index[0])
print(type(df.index[0]))
# 2017-11-01 00:00:00
# <class 'pandas._libs.tslib.Timestamp'>

可以按年或月指定行,并按切片提取周期。

print(df['2018'])
# ? ? ? ? ? ? val_1 ?val_2
# date ? ? ? ? ? ? ? ? ? ?
# 2018-01-03 ? ? 98 ? ? 76
# 2018-01-08 ? ? 48 ? ? 64
# 2018-01-19 ? ? 18 ? ? 48
# 2018-01-23 ? ? 86 ? ? 70

print(df['2017-11'])
# ? ? ? ? ? ? val_1 ?val_2
# date ? ? ? ? ? ? ? ? ? ?
# 2017-11-01 ? ? 65 ? ? 76
# 2017-11-07 ? ? 26 ? ? 66
# 2017-11-18 ? ? 47 ? ? 47
# 2017-11-27 ? ? 20 ? ? 38

print(df['2017-12-15':'2018-01-15'])
# ? ? ? ? ? ? val_1 ?val_2
# date ? ? ? ? ? ? ? ? ? ?
# 2017-12-22 ? ? 31 ? ? 54
# 2017-12-29 ? ? 21 ? ? ?8
# 2018-01-03 ? ? 98 ? ? 76
# 2018-01-08 ? ? 48 ? ? 64

還可以指定各種格式的行。

print(df.loc['01/19/2018', 'val_1'])
# 18

print(df.loc['20180103', 'val_2'])
# 76

讀取CSV時如何指定DatetimeIndex

如果原始數據是CSV文件,則在使用read_csv()進行讀取時可以指定DatetimeIndex。

在參數index_col中指定要用作索引的日期和時間數據的列名(或從0開始的列號),并將parse_dates設置為True。

df = pd.read_csv('./data/26/sample_date.csv', index_col='date', parse_dates=True)
print(df)
# ? ? ? ? ? ? val_1 ?val_2
# date
# 2017-11-01 ? ? 65 ? ? 76
# 2017-11-07 ? ? 26 ? ? 66
# 2017-11-18 ? ? 47 ? ? 47
# 2017-11-27 ? ? 20 ? ? 38
# 2017-12-05 ? ? 65 ? ? 85
# 2017-12-12 ? ? ?4 ? ? 29
# 2017-12-22 ? ? 31 ? ? 54
# 2017-12-29 ? ? 21 ? ? ?8
# 2018-01-03 ? ? 98 ? ? 76
# 2018-01-08 ? ? 48 ? ? 64
# 2018-01-19 ? ? 18 ? ? 48
# 2018-01-23 ? ? 86 ? ? 70

print(type(df.index))
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

如果CSV文件的日期字符串為非標準格式,請在read_csv()的參數date_parser中指定由lambda表達式定義的解析器。

parser = lambda date: pd.to_datetime(date, format='%Y年%m月%d日')

df_jp = pd.read_csv('./data/26/sample_date_cn.csv', index_col='date', parse_dates=True, date_parser=parser)
print(df_jp)
# ? ? ? ? ? ? val_1 ?val_2
# date
# 2017-11-01 ? ? 65 ? ? 76
# 2017-11-07 ? ? 26 ? ? 66
# 2017-11-18 ? ? 47 ? ? 47
# 2017-11-27 ? ? 20 ? ? 38
# 2017-12-05 ? ? 65 ? ? 85
# 2017-12-12 ? ? ?4 ? ? 29
# 2017-12-22 ? ? 31 ? ? 54
# 2017-12-29 ? ? 21 ? ? ?8
# 2018-01-03 ? ? 98 ? ? 76
# 2018-01-08 ? ? 48 ? ? 64
# 2018-01-19 ? ? 18 ? ? 48
# 2018-01-23 ? ? 86 ? ? 70

print(type(df_jp.index))
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

關于pandas.Series

這可能不是實際的模式,但是如果pandas.Series索引是日期字符串。

s = pd.read_csv('./data/26/sample_date.csv', index_col=0, usecols=[0, 1], squeeze=True)
print(s)
# date
# 2017-11-01 ? ?65
# 2017-11-07 ? ?26
# 2017-11-18 ? ?47
# 2017-11-27 ? ?20
# 2017-12-05 ? ?65
# 2017-12-12 ? ? 4
# 2017-12-22 ? ?31
# 2017-12-29 ? ?21
# 2018-01-03 ? ?98
# 2018-01-08 ? ?48
# 2018-01-19 ? ?18
# 2018-01-23 ? ?86
# Name: val_1, dtype: int64

print(type(s))
print(type(s.index))
# <class 'pandas.core.series.Series'>
# <class 'pandas.core.indexes.base.Index'>

如果要將此索引轉換為DatetimeIndex,則可以通過將用to_datetime轉換的索引替換為屬性索引來覆蓋它。

s.index = pd.to_datetime(s.index)
print(s)
# date
# 2017-11-01 ? ?65
# 2017-11-07 ? ?26
# 2017-11-18 ? ?47
# 2017-11-27 ? ?20
# 2017-12-05 ? ?65
# 2017-12-12 ? ? 4
# 2017-12-22 ? ?31
# 2017-12-29 ? ?21
# 2018-01-03 ? ?98
# 2018-01-08 ? ?48
# 2018-01-19 ? ?18
# 2018-01-23 ? ?86
# Name: val_1, dtype: int64

print(type(s))
print(type(s.index))
# <class 'pandas.core.series.Series'>
# <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

print(s['2017-12-15':'2018-01-15'])
# date
# 2017-12-22 ? ?31
# 2017-12-29 ? ?21
# 2018-01-03 ? ?98
# 2018-01-08 ? ?48
# Name: val_1, dtype: int64

原文鏈接:https://blog.csdn.net/qq_18351157/article/details/108185814

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新