0

I'm developing a software that reads a csv file and create a list for each column. In another class, I want to plot the data contained in the arrays of the first class.

I get this error:

AttributeError: 'Read_csv' object has no attribute 'datetime_array'

How can I solve it? These are my classes.

Csv_Reader:

import pandas as pd
from datetime import datetime
#from csv import reader

class Read_csv:

def csv_reader(self, file_name):
    
    df = pd.read_csv(file_name, delimiter = ';')
    self.datetime_array = list(map(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f'), df["DateTime"]))
    self.vet_values_s1 = df["S1"].tolist()
    self.vet_values_s2 = df["S2"].tolist()
    self.vet_values_s3 = df["S3"].tolist()
    print(self.datetime_array, self.vet_values_s1, self.vet_values_s2, self.vet_values_s3)

Data_Plotter:

import numpy
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from Csv_Reader import Read_csv

class Data_plotter:
    
def __init__(self, datetime_array, vet_values_s1, vet_values_s2, vet_values_s3):
    self.datetime_array = datetime_array
    self.vet_values_s1 = vet_values_s1
    self.vet_values_s2 = vet_values_s2
    self.vet_values_s3 = vet_values_s3
    

def plot_data(self):
    
    plt.plot(self.vet_values_s1, color = 'green')
    plt.plot(self.vet_values_s2, color = 'red')
    plt.plot(self.vet_values_s3, color = 'blue')
    
    plt.title('Test')
    
    plt.xlabel('DateTime')
    plt.ylabel('Signals Values')
    plt.xticks(numpy.arange(len(self.datetime_array)), self.datetime_array, rotation=90)
    
    green = mpatches.Patch(color='green',label='S1')
    red = mpatches.Patch(color='red',label='S2')
    blue = mpatches.Patch(color='blue',label='S3')
    plt.legend(handles=[green,red,blue])
    
    plt.grid(True)
    
    plt.savefig("Test")

main:

from Csv_Reader import Read_csv
from Data_Plotter import Data_plotter

reader_obj = Read_csv()

Read_csv().csv_reader('Signals_informations.csv')
obj = Data_plotter(reader_obj.datetime_array, reader_obj.vet_values_s1, reader_obj.vet_values_s2, reader_obj.vet_values_s3)
obj.plot_data()

My csv file:

DateTime;S1;S2;S3
2020-07-16 15:11:34.358231;677.0552427707063;787.6245155900142;543.0755073183745
2020-07-16 15:11:34.360247;535.4790551706492;317.65859520197984;218.64223032216418
2020-07-16 15:11:34.362263;451.9436928722545;449.5560971162404;215.33038976545765
2020-07-16 15:11:34.364279;72.31352267938303;251.55939892326035;896.9233907560412
2020-07-16 15:11:34.366295;758.7365312885398;686.7909954314093;303.9852170969752
2020-07-16 15:11:34.368311;593.8244329562257;698.5981983561348;369.11408762777785
2020-07-16 15:11:34.370327;338.56552989499176;469.327619765774;331.0295457896333
2020-07-16 15:11:34.372343;729.3276090259968;690.776181594403;97.6830657885398
2020-07-16 15:11:34.374359;284.58252864976197;569.0028638781417;196.02767689983673
2020-07-16 15:11:34.376375;909.5920826056772;178.28447193362686;240.4015082916274

1 Answer 1

1

Change this line:

Read_csv().csv_reader('Signals_informations.csv')

to

reader_obj.csv_reader('Signals_informations.csv')

With Read_csv() you're creating a new object instead of using the one you've already created (reader_obj) and using in the next line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.