Python Forum

Full Version: Getting a matolo
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I run this script everything works great.
When I change line glucose.append(row.scan) to glucose.append(row.hist)
I get the error: TypeError: float() argument must be a string or a number, not 'method'

I am pretty sure it has to be something with df.loc[df['hist'] == 0, 'hist'] = df['scan'], but not sure what.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime

# --------------------------------- read from csv ---------------------------
df = pd.read_csv('summary.csv', delimiter='\t', skiprows=3, header=None, names=[
    'id', 'rdate', 'rtype', 'hist', 'scan'], usecols=[0, 1, 2, 3, 4])

# ---------- Fill in 0 ---------------------------
df['hist'] = df['hist'].fillna(0)
df['scan'] = df['scan'].fillna(0)

# -------------- Change dtypes -------------------
df['rdate'] = df['rdate'].astype('datetime64')
df['hist'] = df['hist'].astype('int64')
df['scan'] = df['scan'].astype('int64')

df.loc[df['hist'] == 0, 'hist'] = df['scan']

sd = (df[df.rtype == 1])
sr = (sd[sd.rdate >= datetime.datetime.now() - pd.to_timedelta("2day")])

def displayplot(time, glucose):
    fig, ax = plt.subplots()

    plt.plot(time, glucose, color='k', marker='o', label='Glucose', linewidth=1,
             markevery=1, markerfacecolor='blue')

    plt.plot(time, glucose)

    ax.grid()
    ax.tick_params(axis='y')

    fig.autofmt_xdate()

    plt.axhspan(80, 120, facecolor='lightgreen', alpha=0.5)
    # plt.axhline(y=80, color='green')
    plt.axhline(y=180, color='red')
    plt.axhline(y=70, color='red')
    plt.title('Daily Glucose Chart')
    plt.ylabel('Glucose')
    plt.xlabel('Time')
    plt.legend(loc=2)
    plt.yticks(np.arange(0, 360, 10.0))
    plt.tight_layout()

    plt.show()


# ---------------------------------------------------------------------------------------------------------

time = []
glucose = []
for i, row in sr.iterrows():
    time.append(row.rdate)
    [b]glucose.append(row.scan)[/b]

displayplot(time, glucose)
Thanks for the help.
When you're iterating through the data frame each row in your loop (sr.iterrows()) becomes an instance of pandas Series class.
Series instances have methods...
Unfortunately, pandas Series has hist method, and luckily it hasn't scan method.
Just change glucose.append(row.hist) to glucose.append(row['hist']) and the error will likely gone.
I never even thought about that.

Works fine now.

Thank you!!!!