網站首頁 編程語言 正文
將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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-05-15 Redis中有序集合的內部實現方式的詳細介紹_Redis
- 2022-10-27 SQL案例學習之字符串的合并與拆分方法總結_oracle
- 2022-11-04 ASP.NET?MVC實現登錄后跳轉到原界面_實用技巧
- 2022-09-22 set數據結構/map數據結構(ES6)
- 2022-08-06 詳解Python如何優雅地解析命令行_python
- 2022-07-13 CSS 不需要清除浮動的圣杯布局~面試可能會問
- 2022-10-11 RabbitMQ:生產者消息確認、消息持久化、消費者消息確認、消費失敗重試機制
- 2023-02-06 C#實現對文件進行加密保護的示例代碼_C#教程
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支