Home > Mobile >  TypeError: float() argument must be a string or a number, not 'datetime.timedelta'
TypeError: float() argument must be a string or a number, not 'datetime.timedelta'

Time:10-31

I want to make a plot with the data am fetching from the database, voltage(float), and time(time), I was getting the error now am stack. please help or is there a best way I can go about it.?

import matplotlib.pyplot as plt
import numpy as np
import serial as ser
import time
import datetime
import mysql.connector


my_connect = mysql.connector.connect(host="localhost", user="Kennedy", passwd="Kennerdol05071994", database="ecg_db", auth_plugin="mysql_native_password")
mycursor = my_connect.cursor()

voltage_container = []
time_container = []


def fetch_voltage():
    pat_id = 1
    query = "SELECT voltage, time FROM ecg_data_tbl where patient_id = 1 "
    mycursor.execute(query)
    result = mycursor .fetchall()
    voltage, time = list(zip(*result))
    for volts in voltage:
        voltage_container.append(volts)
        voltage_data = np.array(voltage_container)
    for tim in time:
        time_container.append(tim)
        time_data = np.array(time_container)
    plt.plot(time_data, voltage_data)
    

fetch_voltage()

I was told to convert them to arrays which I did but nothing is changing. The error am getting is:

Traceback (most recent call last):
  File "C:\Users\Kennedy Mulenga\Desktop\Level 5\18136709_BIT_280_Arduino_ECG_Project\fetch_all.py", line 31, in <module>
    fetch_voltage()
  File "C:\Users\Kennedy Mulenga\Desktop\Level 5\18136709_BIT_280_Arduino_ECG_Project\fetch_all.py", line 28, in fetch_voltage
    plt.plot(time_data, voltage_data)
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot
    return gca().plot(
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_axes.py", line 1607, in plot
    self.add_line(line)
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 2101, in add_line
    self._update_line_limits(line)
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\axes\_base.py", line 2123, in _update_line_limits
    path = line.get_path()
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\lines.py", line 1022, in get_path
    self.recache()
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\lines.py", line 663, in recache
    x = _to_unmasked_float_array(xconv).ravel()
  File "C:\Users\Kennedy Mulenga\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\cbook\__init__.py", line 1333, in _to_unmasked_float_array
    return np.asarray(x, float)
TypeError: float() argument must be a string or a number, not 'datetime.timedelta'

CodePudding user response:

Your time data is not a type matplotlib is familiar with so it's trying to convert it itself and it can't. Instead make it a type it can handle yourself.

A common choice, for example, would be

for tim in time:
        time_container.append(tim.total_seconds())
        time_data = np.array(time_container)

Now you are using the total number of seconds in your datetime.timedelta object (that is the result of your query). This is a float so matplotlib can plot it. If you don't want total seconds you can look at the docs for other options.

https://docs.python.org/3/library/datetime.html

  • Related