python Pandas時(shí)序數(shù)據(jù)處理
Python中時(shí)間的一些常用操作
import time
# 從格林威治時(shí)間到現(xiàn)在,單位秒
print('系統(tǒng)時(shí)間戳:', time.time())
print('本地時(shí)間按格式轉(zhuǎn)成str:', time.strftime('%Y-%m-%d %X', time.localtime()))
# 無(wú)參的localtime返回time.struct_time格式的時(shí)間,是本地時(shí)區(qū)的時(shí)間
print('無(wú)參localtime:', time.localtime())
print('本時(shí)區(qū)時(shí)間轉(zhuǎn)成時(shí)間戳:', time.mktime(time.localtime()))
# 將時(shí)間戳轉(zhuǎn)換為能讀懂的時(shí)間
print('時(shí)間戳轉(zhuǎn)時(shí)間:', time.strftime('%Y-%m-%d %X', time.localtime(time.time())))
運(yùn)行結(jié)果:
系統(tǒng)時(shí)間戳: 1542188096.1592166
本地時(shí)間按格式轉(zhuǎn)成str: 2018-11-14 17:34:56
無(wú)參localtime: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=17, tm_min=34, tm_sec=56, tm_wday=2, tm_yday=318, tm_isdst=0)
本時(shí)區(qū)時(shí)間轉(zhuǎn)成時(shí)間戳: 1542188096.0
時(shí)間戳轉(zhuǎn)時(shí)間: 2018-11-14 17:34:56
Pandas時(shí)間序列(DatetimeIndex)與時(shí)序數(shù)據(jù)
時(shí)間序列在Series對(duì)象中且作為索引存在時(shí),就構(gòu)成了時(shí)序數(shù)據(jù)。
import datetime
import numpy as np
import pandas as pd
# pd.date_range()函數(shù)用于創(chuàng)建一個(gè)Pandas時(shí)間序列DatetimeIndex
# start參數(shù)(也是第一個(gè)參數(shù))傳入一個(gè)str格式的開(kāi)始時(shí)間,也可以傳入一個(gè)datetime對(duì)象
# 這里用datetime.datetime()創(chuàng)建了一個(gè)datetime對(duì)象,只用了前三個(gè)參數(shù)也就是年月日
# pd.date_range()函數(shù)可以指明end表示時(shí)間序列的結(jié)尾時(shí)間
# 這里用periods參數(shù)指明序列中要生成的時(shí)間的個(gè)數(shù),freq='D'指定為每天(Day)生成一個(gè)時(shí)間
dti = pd.date_range(start=datetime.datetime(2018, 11, 14), periods=18, freq='D')
print(dti, '\n', '*' * 40, sep='')
# 將時(shí)間序列放在Series對(duì)象中作為索引,這里freq='W'表示隔一周生成一個(gè)
s_dti = pd.Series(np.arange(6), index=pd.date_range('2018/11/4', periods=6, freq='W'))
print(s_dti.head(), '\n', '*' * 40, sep='')
# 取時(shí)序數(shù)據(jù)中指定時(shí)間的內(nèi)容
print(s_dti['2018-11-25'], '\n', '*' * 40, sep='')
# 取第二個(gè)索引對(duì)應(yīng)的時(shí)間的年月日
print(s_dti.index[2].year, s_dti.index[2].month, s_dti.index[2].day, '\n', '*' * 40, sep='')運(yùn)行結(jié)果:
DatetimeIndex(['2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17',
'2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21',
'2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25',
'2018-11-26', '2018-11-27', '2018-11-28', '2018-11-29',
'2018-11-30', '2018-12-01'],
dtype='datetime64[ns]', freq='D')
****************************************
2018-11-04 0
2018-11-11 1
2018-11-18 2
2018-11-25 3
2018-12-02 4
Freq: W-SUN, dtype: int32
****************************************
3
****************************************
20181118
****************************************
杭州天氣的時(shí)序處理
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
df = pd.read_csv('E:/Data/practice/hz_weather.csv')
df = df[['日期', '最高氣溫', '最低氣溫']]
# print(df.head())
print(type(df.日期)) # <class 'pandas.core.series.Series'> print(type(df.日期.values)) # <class 'numpy.ndarray'> # 修改日期格式 # 注意,df.日期得到的是Series對(duì)象,df.日期.values得到的是ndarray多維數(shù)組 # pd.to_datetime()函數(shù)將輸入解析成時(shí)間對(duì)象的格式并返回 # format參數(shù)指定解析的方式 # 當(dāng)輸入列表形式的值時(shí),返回DatetimeIndex;當(dāng)輸入Series時(shí),返回Series;當(dāng)輸入常量時(shí),返回Timestamp print(type(pd.to_datetime(df.日期.values, format="%Y-%m-%d"))) # <class 'pandas.core.indexes.datetimes.DatetimeIndex'> print(type(pd.to_datetime(df.日期, format="%Y-%m-%d"))) # <class 'pandas.core.series.Series'> df.日期 = pd.to_datetime(df.日期.values, format="%Y-%m-%d") # print(df.head())

# 將日期設(shè)置為索引
df = df.set_index('日期')
# 取出第0個(gè)索引值對(duì)應(yīng)的日期
print(df.index[0]) # 2017-01-01 00:00:00
# DatetimeIndex里存的是一個(gè)個(gè)的Timestamp,查看一下類型
print(type(df.index[0])) # <class 'pandas._libs.tslibs.timestamps.Timestamp'>
# print(df.info())
# 提取1月份的溫度數(shù)據(jù) df_jan = df[(df.index >= "2017-1-1") & (df.index < "2017-2-1")] # 或用這種方式也可以 df_jan = df["2017-1-1":"2017-1-31"] # print(df_jan.info())

# 只取到月份
df_m = df.to_period('M')
# print(df_m.head())
# 利用上面的只取到月份,對(duì)level=0(即索引層級(jí))做聚合就可以求月內(nèi)的平均值等 s_m_mean = df_m.groupby(level=0).mean() # print(s_m_mean.head())

# 繪制[最高溫度]和[最低溫度]兩個(gè)指標(biāo)隨著索引[時(shí)間]變化的圖 fig, ax = plt.subplots(1, 1, figsize=(12, 4)) df.plot(ax=ax) plt.show()

附:matplotlib中文支持

到此這篇關(guān)于python Pandas時(shí)序數(shù)據(jù)處理 的文章就介紹到這了,更多相關(guān)Pandas 時(shí)序數(shù)據(jù) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PIL.Image.open和cv2.imread的比較與相互轉(zhuǎn)換的方法
這篇文章主要介紹了PIL.Image.open和cv2.imread的比較與相互轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
python使用xmlrpclib模塊實(shí)現(xiàn)對(duì)百度google的ping功能
這篇文章主要介紹了python使用xmlrpclib模塊實(shí)現(xiàn)對(duì)百度google的ping功能,實(shí)例分析了xmlrpclib模塊的相關(guān)技巧,需要的朋友可以參考下2015-06-06
pandas dataframe中雙中括號(hào)和單中括號(hào)的區(qū)別及說(shuō)明
這篇文章主要介紹了pandas dataframe中雙中括號(hào)和單中括號(hào)的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python通過(guò)wordcloud庫(kù)實(shí)現(xiàn)將單詞生成詞云
Python的wordcloud庫(kù)是一個(gè)用于生成詞云的Python包,它可以將一段文本中出現(xiàn)頻率高的單詞按其出現(xiàn)頻率大小以及顏色深淺排列成一個(gè)詞云圖形,從而更好地展示文本中的信息,你可以使用wordcloud庫(kù)來(lái)生成各種類型的詞云,本文就介紹了如何生成心型詞云2023-06-06
Python Traceback異常代碼排錯(cuò)利器使用指南
這篇文章主要為大家介紹了Python Traceback異常代碼排錯(cuò)利器使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python使用matplotlib繪制余弦的散點(diǎn)圖示例
這篇文章主要介紹了Python使用matplotlib繪制余弦的散點(diǎn)圖,涉及Python操作matplotlib的基本技巧與散點(diǎn)的設(shè)置方法,需要的朋友可以參考下2018-03-03
解決nohup重定向python輸出到文件不成功的問(wèn)題
今天小編就為大家分享一篇解決nohup重定向python輸出到文件不成功的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

