The purpose of this script is:
• Read a group of csv files.
• Scrape the date and extract some features out of it.
• Merge these csv files into a single data frame.
• Import the final data frame into another class and print it.
Here is the code:
import pandas as pd import os class DataSource: def __init__(self): self.dfs = [] self.final = pd.DataFrame() self.names = ['Date', 'Time', 'open', 'high', 'low', 'close', 'Volume'] self.directory = os.chdir(r"C:\Users\Sayed\Desktop\forex") def merge(self): for file in os.listdir(self.directory): df = pd.read_csv(file, names=self.names, parse_dates={'Release Date': ['Date', 'Time']}) self.dfs.append(df) self.final = pd.concat(self.dfs, axis=0) self.final = self.final[['Release Date', 'open', 'high', 'low', 'close']] print(self.final.head()) return self.final class test(): def __init__(self): self.df = DataSource.final def print(self): return print(self.df.head()) x = test() x.print() Here is the error:
Traceback (most recent call last):
File "C:/Users/Sayed/PycharmProjects/project/hello.py", line 31, in x = test()
File "C:/Users/Sayed/PycharmProjects/project/hello.py", line 26, in init self.df = DataSource.final
AttributeError: type object 'DataSource' has no attribute 'final'
finalis an instance attribute, not a class attribute.DataSource().finalwould work for example…finalbelongs to an instance, not the class. (It is inside theinit) Therefore you cannot accessfinalwithout initializing an instance ofDatasource. That said, I have no idea what you're trying to achieve with thetestclass.