bbartling
bbartling

Reputation: 3504

API time series data

I have some data that cant be replecated but I am doing an API request to an energy management system to build a simple pandas data frame... When the data request comes back, I do a write to pd_to.CSV for the data, and then do a read_CSV with index_col='Date', parse_dates=True

How do I incorporate this to my API request labeling the time stamp as 'Date' and as well as the parse_dates=True? (Skip the read/write data to CSV...)

from pyhaystack.client.niagara import NiagaraHaystackSession
import pandas as pd


session = NiagaraHaystackSession(uri='http://192.x.x.x', username='username', password='password', pint=True)

op = session.nav()
op.wait()
nav = op.result
print(nav)

#Get Data for the OSA-T & convert to Pandas series:
oat = session.find_entity(filter_expr='outsideAir').result
oat_df = session.his_read_frame(oat, rng= '2017-10-01,2018-01-01').result
oat_df = pd.Series(oat_df[oat_df.columns[0]])


#Get Data for the electrical energy kWh & convert to Pandas series:
elecMeter = session.find_entity(filter_expr='elecMeter').result
elecMeter_df = session.his_read_frame(elecMeter, rng= '2017-10-01,2018-01-01').result
elecMeter_df = pd.Series(elecMeter_df[elecMeter_df.columns[0]])

#Get Data for the natural gas energy therms & convert to Pandas series:
gasMeter = session.find_entity(filter_expr='gasMeter').result
gasMeter_df = session.his_read_frame(gasMeter, rng= '2017-10-01,2018-01-01').result
gasMeter_df = pd.Series(gasMeter_df[gasMeter_df.columns[0]])

UtilityInfo = pd.DataFrame({'oat' : oat_df, 'kWh' : elecMeter_df, 'therms' : gasMeter_df})

This is where I am read/write to CSV that I am hoping to incorporate into the API request... Unless this is just best practices doing the read/write???.. Basically in between the read/write steps below I have to cheat and open up Excel to label the timestampt column 'Date' which I am hoping to avoid!!!

UtilityInfo.to_csv('C:\\Python Scripts\\UtilityInfo.csv', sep=',', header=True, index=True, na_rep='N/A')

UtilityInfo = pd.read_csv('C:\\Python Scripts\\UtilityInfo.csv', index_col='Date', parse_dates=True)

Upvotes: 0

Views: 421

Answers (1)

Christian Tremblay
Christian Tremblay

Reputation: 839

You can use UtilityInfo.index.names=['Date'] to give a name to the index.

You could also create a column from this index using df.reset_index()

Good ref : Rename Pandas DataFrame Index

Upvotes: 1

Related Questions