Pandas.DataFrame時(shí)間序列數(shù)據(jù)處理的實(shí)現(xiàn)
將pandas.DataFrame,pandas.Series的索引設(shè)置為datetime64 [ns]類型時(shí),將其視為DatetimeIndex,并且可以使用各種處理時(shí)間序列數(shù)據(jù)的函數(shù)。
可以按年或月指定行,并按切片指定提取周期,這在處理包含日期和時(shí)間信息(例如日期和時(shí)間)的數(shù)據(jù)時(shí)非常方便。
在此,將對(duì)以下內(nèi)容進(jìn)行描述。
- 如何將一列現(xiàn)有數(shù)據(jù)指定為DatetimeIndex
- 讀取CSV時(shí)如何指定DatetimeIndex
- 關(guān)于pandas.Series
如何將一列現(xiàn)有數(shù)據(jù)指定為DatetimeIndex
將pandas.DataFrame與默認(rèn)的基于0的索引和一個(gè)字符串列作為日期。
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()應(yīng)用于日期字符串列,并轉(zhuǎn)換為datetime64 [ns]類型。
df['date'] = pd.to_datetime(df['date']) print(df['date'].dtype) # datetime64[ns]
使用set_index()方法將datetime64 [ns]類型的列指定為索引。
Pandas.DataFrame,重置列的行名(set_index)
索引現(xiàn)在是DatetimeIndex。索引的每個(gè)元素都是時(shí)間戳類型。
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時(shí)如何指定DatetimeIndex
如果原始數(shù)據(jù)是CSV文件,則在使用read_csv()進(jìn)行讀取時(shí)可以指定DatetimeIndex。
在參數(shù)index_col中指定要用作索引的日期和時(shí)間數(shù)據(jù)的列名(或從0開始的列號(hào)),并將parse_dates設(shè)置為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文件的日期字符串為非標(biāo)準(zhǔn)格式,請(qǐng)?jiān)趓ead_csv()的參數(shù)date_parser中指定由lambda表達(dá)式定義的解析器。
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'>
關(guān)于pandas.Series
這可能不是實(shí)際的模式,但是如果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'>
如果要將此索引轉(zhuǎn)換為DatetimeIndex,則可以通過(guò)將用to_datetime轉(zhuǎn)換的索引替換為屬性索引來(lái)覆蓋它。
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
到此這篇關(guān)于Pandas.DataFrame時(shí)間序列數(shù)據(jù)處理的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas.DataFrame時(shí)間序列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pandas.DataFrame中提取特定類型dtype的列
- Pandas通過(guò)index選擇并獲取行和列
- Pandas中MultiIndex選擇并提取任何行和列
- pandas中按行或列的值對(duì)數(shù)據(jù)排序的實(shí)現(xiàn)
- Pandas數(shù)據(jù)查詢的集中實(shí)現(xiàn)方法
- pandas讀取Excel批量轉(zhuǎn)換時(shí)間戳的實(shí)踐
- Python?中?Pandas?文件操作和讀取?CSV?參數(shù)詳解
- Pandas merge合并兩個(gè)DataFram的實(shí)現(xiàn)
- 針對(duì)Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
相關(guān)文章
python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)(三)
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)第三篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12python實(shí)現(xiàn)基于信息增益的決策樹歸納
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)基于信息增益的決策樹歸納,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中
這篇文章主要介紹了python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python獲取指定時(shí)間差的時(shí)間實(shí)例詳解
這篇文章主要介紹了python獲取指定時(shí)間差的時(shí)間實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

Python正則表達(dá)式如何進(jìn)行字符串替換實(shí)例