Python Forum
Getting a matolo - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting a matolo (/thread-15814.html)



Getting a matolo - gehrenfeld - Feb-01-2019

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.


RE: Getting a matolo - scidam - Feb-01-2019

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.


RE: Getting a matolo - gehrenfeld - Feb-01-2019

I never even thought about that.

Works fine now.

Thank you!!!!