Home > database >  An error occured to code like that "argument of type 'method' is not iterable"
An error occured to code like that "argument of type 'method' is not iterable"

Time:06-22

I would like to predict future stock price and I tried to create calculate function but when I run code below I found an error. I am not sure if I missing (), or not. Could you please advice me?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

Gold_train_data = pd.read_csv('Gold Data Last Year.csv', index_col=False)
Gold_test_data = pd.read_csv('Gold Data Last Month.csv', index_col=False)
              

current_train_data = Gold_train_data
current_test_data = Gold_test_data

NUM_train_data = 266
NUM_test_data = 22

def load_stock_data(stock_name, num_data_points):
    data = pd.read_csv(stock_name,
                       skiprows=0,
                       nrows=num_data_points,
                       usecols=['Price', 'Open', 'Vol.'])
    final_prices = data['Price'].astype(str).str.replace(',','').astype(np.float)
    opening_prices = data['Open'].astype(str).str.replace(',', '').astype(np.float)
    volumes = data['Vol.'].str.strip('MK').astype(np.float)
    return final_prices, opening_prices, volumes

def calculate_price_differences(final_prices, opening_prices):
    price_differences = []
    for d_i in range(len(final_prices) - 1):
        price_difference = opening_prices[d_i   1] - final_prices[d_i]
        price_differences.append(price_difference)
    return price_differences

print(load_stock_data(current_test_data, NUM_test_data))

The above is the code and the below is the error shown as below:

Traceback (most recent call last):

  Input In [6] in <cell line: 1>
    print(load_stock_data(current_test_data, NUM_test_data))

  Input In [4] in load_stock_data
    data = pd.read_csv(stock_name,

  File ~\Anaconda3\lib\site-packages\pandas\util\_decorators.py:311 in wrapper
    return func(*args, **kwargs)

  File ~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:680 in read_csv
    return _read(filepath_or_buffer, kwds)

  File ~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:575 in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)

  File ~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:933 in __init__
    self._engine = self._make_engine(f, self.engine)

  File ~\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:1217 in _make_engine
    self.handles = get_handle(  # type: ignore[call-overload]

  File ~\Anaconda3\lib\site-packages\pandas\io\common.py:661 in get_handle
    if _is_binary_mode(path_or_buf, mode) and "b" not in mode:

  File ~\Anaconda3\lib\site-packages\pandas\io\common.py:1128 in _is_binary_mode
    return isinstance(handle, _get_binary_io_classes()) or "b" in getattr(

TypeError: argument of type 'method' is not iterable

CodePudding user response:

The issue is with the function call

print(load_stock_data(current_test_data, NUM_test_data))

current_test_data was the pandas dataframe you loaded previously. Passing the variable to load_stock_data() results in the function trying to execute

data = pd.read_csv(current_test_data,
                       skiprows=0,
                       nrows=num_data_points,
                       usecols=['Price', 'Open', 'Vol.'])

Note that by default the first argument to pd.read_csv() by convention is the pathname to the csv file. Hence the error arises when it thinks current_test_data is the pathname but it is actually a pandas.DataFrame.

What I think you want to do could be achieved by using

print(load_stock_data('Gold Data Last Month.csv', NUM_test_data))

instead. Feeding the pathname to the function call instead of the dataframe itself.

  • Related