溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Python獲取和存儲時間序列數(shù)據(jù)

發(fā)布時間:2023-04-12 09:42:29 來源:億速云 閱讀:107 作者:iii 欄目:編程語言

今天小編給大家分享一下怎么用Python獲取和存儲時間序列數(shù)據(jù)的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

要求

本教程在通過Homebrew已安裝Python 3的macOS系統(tǒng)上完成。建議安裝額外的工具,比如virtualenv、pyenv或conda-env,以簡化Python和Client的安裝。完整的要求在這里:

txt
influxdb-client=1.30.0
pandas=1.4.3
requests>=2.27.1

本教程還假設(shè)您已經(jīng)創(chuàng)建Free Tier InfluxDB云帳戶或正在使用InfluxDB OSS,您也已經(jīng):

  • 創(chuàng)建了存儲桶。您可以將存儲桶視為數(shù)據(jù)庫或InfluxDB中最高層次的數(shù)據(jù)組織。

  • 創(chuàng)建了令牌。

最后,該教程要求您已經(jīng)使用OpenWeatherMap創(chuàng)建了一個帳戶,并已創(chuàng)建了令牌。

請求天氣數(shù)據(jù)

首先,我們需要請求數(shù)據(jù)。我們將使用請求庫,通過OpenWeatherMap API從指定的經(jīng)度和緯度返回每小時的天氣數(shù)據(jù)。

# Get time series data from OpenWeatherMap API
params = {'lat':openWeatherMap_lat, 'lon':openWeatherMap_lon, 'exclude': 
"minutely,daily", 'appid':openWeatherMap_token}
r = requests.get(openWeather_url, params = params).json()
hourly = r['hourly']

將數(shù)據(jù)轉(zhuǎn)換成Pandas DataFrame

接下來,將JSON數(shù)據(jù)轉(zhuǎn)換成Pandas DataFrame。我們還將時間戳從秒精度的Unix時間戳轉(zhuǎn)換成日期時間對象。之所以進(jìn)行這種轉(zhuǎn)換,是由于InfluxDB寫入方法要求時間戳為日期時間對象格式。接下來,我們將使用這種方法,將數(shù)據(jù)寫入到InfluxDB。我們還刪除了不想寫入到InfluxDB的列。

python
# Convert data to Pandas DataFrame and convert timestamp to datetime 
object
df = pd.json_normalize(hourly)
df = df.drop(columns=['weather', 'pop'])
df['dt'] = pd.to_datetime(df['dt'], unit='s')
print(df.head)

將Pandas DataFrame寫入到InfluxDB

現(xiàn)在為InfluxDB Python客戶端庫創(chuàng)建實(shí)例,并將DataFrame寫入到InfluxDB。我們指定了測量名稱。測量含有存儲桶中的數(shù)據(jù)。您可以將其視為InfluxDB的數(shù)據(jù)組織中僅次于存儲桶的第二高層次結(jié)構(gòu)。

您還可以使用data_frame__tag_columns參數(shù)指定將哪些列轉(zhuǎn)換成標(biāo)簽。

由于我們沒有將任何列指定為標(biāo)簽,我們的所有列都將轉(zhuǎn)換成InfluxDB中的字段。標(biāo)簽用于寫入有關(guān)您的時間序列數(shù)據(jù)的元數(shù)據(jù),可用于更有效地查詢數(shù)據(jù)子集。字段是您在 InfluxDB中存儲實(shí)際時間序列數(shù)據(jù)的位置。

on
# Write data to InfluxDB
with InfluxDBClient(url=url, token=token, org=org) as client:
df = df
client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,record=df,
data_frame_measurement_name="weather",
data_frame_timestamp_column="dt")

完整腳本

回顧一下,不妨看看完整的腳本。 我們采取以下步驟:

1. 導(dǎo)入庫。

2. 收集以下內(nèi)容:

  • InfluxDB存儲桶

  • InfluxDB組織

  • InfluxDB令牌

  • InfluxDB URL

  • OpenWeatherMap URL

  • OpenWeatherMap 令牌

3. 創(chuàng)建請求。

4. 將JSON響應(yīng)轉(zhuǎn)換成Pandas DataFrame。

5. 刪除您不想寫入到InfluxDB的任何列。

6. 將時間戳列從Unix時間轉(zhuǎn)換成Pandas日期時間對象。

7. 為InfluxDB Python Client庫創(chuàng)建實(shí)例。

8. 編寫DataFrame,并指定測量名稱和時間戳列。

python
import requests
import influxdb_client
import pandas as pd
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
bucket = "OpenWeather"
org = "" # or email you used to create your Free Tier 
InfluxDB Cloud account
token = " 
url = "" # for example, 
https://us-west-2-1.aws.cloud2.influxdata.com/
openWeatherMap_token = ""
openWeatherMap_lat = "33.44"
openWeatherMap_lon = "-94.04"
openWeather_url = "https://api.openweathermap.org/data/2.5/onecall"
# Get time series data from OpenWeatherMap API
params = {'lat':openWeatherMap_lat, 'lon':openWeatherMap_lon, 'exclude': 
"minutely,daily", 'appid':openWeatherMap_token}
r = requests.get(openWeather_url, params = params).json()
hourly = r['hourly']
# Convert data to Pandas DataFrame and convert timestamp to datetime 
object
df = pd.json_normalize(hourly)
df = df.drop(columns=['weather', 'pop'])
df['dt'] = pd.to_datetime(df['dt'], unit='s')
print(df.head)
# Write data to InfluxDB
with InfluxDBClient(url=url, token=token, org=org) as client:
df = df
client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,record=df,
data_frame_measurement_name="weather",
data_frame_timestamp_column="dt")

查詢數(shù)據(jù)

現(xiàn)在,我們已經(jīng)將數(shù)據(jù)寫入到InfluxDB,可以使用InfluxDB UI來查詢數(shù)據(jù)了。導(dǎo)航到數(shù)據(jù)資源管理器(從左側(cè)導(dǎo)航欄中)。使用Query Builder(查詢構(gòu)建器),選擇想要可視化的數(shù)據(jù)和想要為之可視化的范圍,然后點(diǎn)擊“提交”。

怎么用Python獲取和存儲時間序列數(shù)據(jù)

圖1. 天氣數(shù)據(jù)的默認(rèn)物化視圖。InfluxDB自動聚合時間序列數(shù)據(jù),這樣新用戶就不會意外查詢太多數(shù)據(jù)而導(dǎo)致超時

專業(yè)提示:當(dāng)您使用查詢構(gòu)建器查詢數(shù)據(jù)時,InfluxDB自動對數(shù)據(jù)進(jìn)行下采樣。要查詢原始數(shù)據(jù),導(dǎo)航到Script Editor(腳本編輯器)以查看底層Flux查詢。Flux是面向InfluxDB的原生查詢和腳本語言,可用于使用您的時間序列數(shù)據(jù)來分析和創(chuàng)建預(yù)測。使用aggregateWindow()函數(shù)取消行注釋或刪除行,以查看原始數(shù)據(jù)。

怎么用Python獲取和存儲時間序列數(shù)據(jù)

圖2. 導(dǎo)航到腳本編輯器,并取消注釋或刪除aggregateWindow()函數(shù),以查看原始天氣數(shù)據(jù)

以上就是“怎么用Python獲取和存儲時間序列數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI