Reputation: 615
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'
Upvotes: 0
Views: 141
Reputation: 61
Your cannot access self.final
property directly in your DataSource class. You need to instantiate it first. So your test
class will more be like :
class test():
def __init__(self):
self.d = DataSource()
self.df = self.d.merge()
def print(self):
return print(self.df.head())
Upvotes: 1