Home > Enterprise >  AttributeError: 'NoneType' object has no attribute 'asfreq'
AttributeError: 'NoneType' object has no attribute 'asfreq'

Time:09-28

I'm working on a small project trying to make a stock price prediction neural network with python on jupyter notebook.I have all the imports I need and I imported them correctly without an error. I also made sure that I'm importing the csv file correctly.

This is the cell that is giving me the error:

a_df = get_dataframe_from_csv("DAX")
a_df = a_df.asfreq('d')
a_df.index

Here is the code for the function:

def get_dataframe_from_csv(ticker):
    try:
        df = pd.read_csv(PATH   ticker   '.csv', index_col='Date', parse_dates=True)
    except FileNotFoundError:
        pass
    else:
        return df

Here is a sample of the csv file. It has no headings/titles:

2013.09.05,00:00,8210.80,8257.80,8162.80,8223.50,30335
2013.09.06,00:00,8224.00,8297.00,8171.50,8251.50,33836
2013.09.09,00:00,8246.50,8315.80,8245.30,8301.00,26223
2013.09.10,00:00,8301.50,8477.80,8298.00,8467.00,30111

When I run the cell, I get the error that I mentioned in the title. I'm trying to tell the network that the data it's getting is daily data, but when I try running the cell, I get an error.

I tried writing it as a_df = a_df.asfreq(freq='d') but that didn't work.

What are some possible solutions?

Edit: I think the error is that in my csv file, I don't have an index column with the text "Date"..if this is the case, how do I add it

CodePudding user response:

Try to modify your function like below:

PATH = "C:\\Users\\terry\\Documents\\jupyter projects\\stock\\"
HEADERS = ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']

def get_dataframe_from_csv(ticker):
    return pd.read_csv(PATH   ticker   '.csv', index_col='DateTime',
                       parse_dates={'DateTime': ['Date', 'Time']},
                       header=None, names=HEADERS)
                       

a_df = get_dataframe_from_csv("DAX")
>>> a_df
              Open    High     Low   Close  Volume
DateTime                                          
2013-09-05  8210.8  8257.8  8162.8  8223.5   30335
2013-09-06  8224.0  8297.0  8171.5  8251.5   33836
2013-09-09  8246.5  8315.8  8245.3  8301.0   26223
2013-09-10  8301.5  8477.8  8298.0  8467.0   30111
  • Related