欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas.DataFrame時間序列數(shù)據(jù)處理的實現(xiàn)

 更新時間:2023年02月23日 09:57:10   作者:餃子大人  
本文主要介紹了Pandas.DataFrame時間序列數(shù)據(jù)處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

在此,將對以下內(nèi)容進(jìn)行描述。

  • 如何將一列現(xiàn)有數(shù)據(jù)指定為DatetimeIndex
  • 讀取CSV時如何指定DatetimeIndex
  • 關(guān)于pandas.Series

如何將一列現(xiàn)有數(shù)據(jù)指定為DatetimeIndex

將pandas.DataFrame與默認(rèn)的基于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()應(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。索引的每個元素都是時間戳類型。

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

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

在參數(shù)index_col中指定要用作索引的日期和時間數(shù)據(jù)的列名(或從0開始的列號),并將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)格式,請在read_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

這可能不是實際的模式,但是如果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,則可以通過將用to_datetime轉(zhuǎn)換的索引替換為屬性索引來覆蓋它。

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ù)據(jù)處理的實現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas.DataFrame時間序列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    Python正則表達(dá)式在使用中會經(jīng)常應(yīng)用到字符串替換的代碼。這篇文章主要介紹了Python正則表達(dá)式如何進(jìn)行字符串替換,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • python常用知識梳理(必看篇)

    python常用知識梳理(必看篇)

    下面小編就為大家?guī)硪黄猵ython常用知識梳理(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • 最新評論