2

I am trying to put together a pandas dataframe for a school project with but to do so I am hitting an api repeatedly. I can't figure out excatly why I am returning the same dataframe over and over, sans the column title, any help much appreciated.

Code is as follows:

a.py

import json
import requests
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup


tmp = [] 
tmp_1 = []
def fetchdata(ticker):
    url = 'https://api.iextrading.com/1.0/stock/'
    time = '/chart/5y'
    get = url + ticker + time
    data = requests.get(get).json()
    length = len(data)
    # i = i + 1
    for j in range(0, length):
        date = data[j]['date']
        closing = data[j]['close']
        x = tmp.append(date)
        y = tmp_1.append(closing)
        df = pd.DataFrame(x)
        df[ticker] = tmp_1
        df_1 = df.loc[1:1000]
    return df_1

b.py

import pandas as pd
import numpy as np
from slizzy import fetchdata

df_appl_1 = fetchdata('aapl')
df_appl_2 = fetchdata('aapl')
df_appl_3 = fetchdata('aapl')
df_gold = fetchdata('gld')

print df_appl_1
print df_gold

1 Answer 1

1

Move your list declarations into your function:

def fetchdata(ticker):
    tmp = [] 
    tmp_1 = []

As it stands, after the first call to your function, these lists are not cleared out (because they're globals), so you successively query the same 1000 elements each time.

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.