Unable to parse DataFrame values

Question:

In spite of searching for and adapting several potential solutions online and via StackOverflow, I seem to be making no headway. I was testing this air quality index (AQI) library (you can install it using – $ pip install python-aqi) that I recently discovered using two different data. First data is a value stored in a variable, while the second is a series of values in a data frame. For the former, the code ran successfully, but for the latter, I kept getting this error: InvalidOperation: [<class ‘decimal.ConversionSyntax’>]. Please help. Thanks. Here is the link to the data: https://drive.google.com/file/d/16QQnDIEFkDQGafNYTk6apY9JcRimRBxH/view?usp=sharing

Code 1 – successful

import aqi     # import air quality library
import pandas as pd

# define the PM2.5 variable and store a value
pmt_2_5 = 60.5

mush_2pt5 = aqi.to_iaqi(aqi.POLLUTANT_PM25, str(pmt_2_5))   #AQI code
mush_2pt5

Code 2 – Unsuccessful

dataframe for PM2.5 data

import aqi    # import air quality library
import pandas as pd

# Read data
df_mush = pd.read_csv("book1.csv", parse_dates=["Date"], index_col="Date", sep=",")

# parse data in the third column to the variable (see image)
pmt_2_5 = df_mushin['mush-pm_2_5']

# parse the variable and calculate AQI
df_mush_2pt5 = aqi.to_iaqi(aqi.POLLUTANT_PM25, str(pmt_2_5)) 
df_mush_2pt5
Asked By: Deeone

||

Answers:

You can’t pass a Series but only a string:

df_mush = pd.read_csv('book1.csv', parse_dates=['Date'], index_col='Date', sep=',')
df_mush_2pt5 = (df_mush['mush-pm_2_5'].astype(str)
                    .map(lambda cc: aqi.to_iaqi(aqi.POLLUTANT_PM25, cc)))

Output:

# it's not a dataframe but a series
>>> df_mush_2pt5
0     154
1     152
2     162
3     153
4     153
5     158
6     153
7     134
8     151
9     136
10    154
Name: mush-pm_2_5, dtype: object

Documentation:

Help on function to_iaqi in module aqi:

to_iaqi(elem, cc, algo='aqi.algos.epa')
    Calculate an intermediate AQI for a given pollutant. This is the
    heart of the algo.
    
    .. warning:: the concentration is passed as a string so
        :class:`decimal.Decimal` doesn't act up with binary floats.
    
    :param elem: pollutant constant
    :type elem: int
    :param cc: pollutant contentration (µg/m³ or ppm)
    :type cc: str   # <-- This is a string, not Series
    :param algo: algorithm module canonical name
    :type algo: str
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.