Python Forum
Passing Values of Dictionaries to Function & Unable to Access Them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Values of Dictionaries to Function & Unable to Access Them
#3
Hi Yoriz, thank you for the reply and help. I tried some reworking the script in the hope of making changes based on your suggestion (i hope i did). I am getting the following error message
Error:
Traceback (most recent call last): File "C:\Users\bgeor\Desktop\multiEMAs.py", line 24, in <module> ema_val = ExpMovingAverage(val, 3) File "C:\Users\bgeor\Desktop\multiEMAs.py", line 16, in ExpMovingAverage a = np.convolve(values, weights, mode='full')[:len(values)] File "C:\Users\bgeor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\numeric.py", line 1114, in convolve return multiarray.correlate(a, v[::-1], mode) TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'
in the reworked version i removed the lines
get_curprices = lambda:stockdata()[0]
#getting dataset of current prices into a dictionary
dict_curprices = {}
dict_curprices.update(get_curprices())
 
get_openprices  = lambda:stockdata()[1]
#getting dataset of open prices into a dictionary
dict_openprices = {}
dict_openprices.update(get_openprices())
as it seems they won't affect the result i'm after
This is the new version:
import numpy as np
import pandas as pd
from yahoofinancials import YahooFinancials

stocks = ['AAPL', 'MSFT', 'TSLA']

def stockdata():
    yahoo_financials = YahooFinancials(stocks)
    price = yahoo_financials.get_current_price()
    Open = yahoo_financials.get_open_price()
    return price, Open

def ExpMovingAverage(values, window):
    weights = np.exp(np.linspace(-1., 0., window))
    weights /= weights.sum()
    a =  np.convolve(values, weights, mode='full')[:len(values)]
    a[:window] = a[window]
    return a

if __name__ == "__main__":
    dF = pd.DataFrame(stockdata())
    data = dict(dF)
    for val in data.keys():
        ema_val = ExpMovingAverage(val, 3)
        print(ema_val, key) 
Reply


Messages In This Thread
RE: Passing Values of Dictionaries to Function & Unable to Access Them - by firebird - Aug-03-2019, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to access values returned from inquirer cspower 6 905 Dec-26-2023, 09:34 PM
Last Post: cspower
  Unable to get function to do anything... ejwjohn 8 971 Nov-07-2023, 08:52 AM
Last Post: ejwjohn
  Access list of dictionaries britesc 4 1,131 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Adding values with reduce() function from the list of tuples kinimod 10 2,784 Jan-24-2023, 08:22 AM
Last Post: perfringo
  passing dictionary to the function mark588 2 1,027 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  function accepts infinite parameters and returns a graph with those values edencthompson 0 887 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Help with passing values in npyscreen Extra 8 2,606 May-21-2022, 12:41 AM
Last Post: Extra
  dict class override: how access parent values? Andrey 1 1,682 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Unable to access jarfile Rakshan 2 2,695 Jul-28-2021, 11:10 AM
Last Post: Rakshan
  Function - Return multiple values tester_V 10 4,584 Jun-02-2021, 05:34 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020