0

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'

2
  • 2
    Yeah, final is an instance attribute, not a class attribute. DataSource().final would work for example… Commented Jun 4, 2020 at 8:21
  • 1
    You haven't initialized a Datasource instance. final belongs to an instance, not the class. (It is inside the init) Therefore you cannot access final without initializing an instance of Datasource. That said, I have no idea what you're trying to achieve with the test class. Commented Jun 4, 2020 at 8:21

1 Answer 1

1

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())
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.