Loading Data from Adafruit IO into Pandas
If you are logging sensor data to Adafruit IO, you may have a need to do a deeper examination of your data. One popular option is the pandas data analysis library for Python.
Here is a description from the pandas website:
pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
Let’s take a quick look at how to load data into pandas from a public Adafruit IO feed.
Setup
You will need Python 3.5 installed, and you will also need to install pip if it wasn’t included with your Python install.
Once Python and pip are installed, you can install pandas by running:
$ pip install pandas
For this example, we will be pointing pandas at a public Adafruit IO feed. If your feed is currently private, you will need to make it public.
Importing Feed Data
To get started, you will need to open up a new Python file in your favorite editor, and start by importing pandas:
import pandas as pd
Next, we need to pull in feed data via the read_json
method. Replace USERNAME
and FEED_KEY
with your
Adafruit IO username, and the key of the feed you wish to load.
feed_data = pd.read_json('https://io.adafruit.com/api/v2/USERNAME/feeds/FEED_KEY/data')
If you are not sure where to find your feed’s key, you can find it below the feed name.
Date Conversion
Next, we will convert the created_at
column from a string to datetime
, and set it as the index for the data frame:
feed_data['created_at'] = pd.to_datetime(feed_data['created_at'], infer_datetime_format=True)
feed_data = feed_data.set_index('created_at')
Plot Values on a Chart
Now that the data has a valid date column, we can load the feed values into a time series, and plot the values:
values = pd.Series(feed_data['value'])
values.plot()
Another simple example is to resample the data into daily averages, and plot them on a bar chart:
daily = values.resample('1d').mean()
daily.plot(kind='barh')
You can do a wide range of useful operations with Time Series, and you should refer to the documentation for more details.