0

I've looked at the following links to figure out where I should freeze my code

TypeError: unhashable type: 'dict'

TypeError: unhashable type: 'dict', when dict used as a key for another dict

TypeError: unhashable type: 'dict' in python

def getPx (self, date, instrument, item = 'Adj Close', prices = None):
        ''' get a price for an instrument on a date from prices table'''

    prices = prices or self.prices
    import datetime
    import numpy as np

    if type ( instrument ) == np.ndarray: instrument = str ( instrument [ 0 ] )

    pricelist = [item.copy() for item in prices[instrument]]
    for item in pricelist:
        item['Date'] = datetime.datetime.strptime(item['Date'],"%Y-%m-%d")


    dates = map(lambda x: x['Date'],pricelist)
    eff_date = max([d for d in dates if d <= date])

    price = [price[item] for price in pricelist if price["Date"] == eff_date]

    return price

where pricelist is a dict but I can't seem to figure out what to freeze. Wasted a whole morning trying to figure out why I can't just get the price at a particular date from my dictionary..

Any help deeply appreciated

EDIT:

A typical example pricelist would be

{'ABCD':{'Adj Close': '82.72',
   'Close': '86.06',
   'Date': '2013-09-19',
   'High': '86.90',
   'Low': '85.79',
   'Open': '86.86',
   'Volume': '18700'},
  {'Adj Close': '83.67',
   'Close': '87.05',
   'Date': '2013-09-18',
   'High': '87.75',
   'Low': '85.18',
   'Open': '85.49',
   'Volume': '30900'},
  {'Adj Close': '82.48',
   'Close': '85.81',
   'Date': '2013-09-17',
   'High': '85.85',
   'Low': '85.02',
   'Open': '85.12',
   'Volume': '14700'},
  {'Adj Close': '81.43',
   'Close': '84.72',
   'Date': '2013-09-16',
   'High': '86.36',
   'Low': '84.42',
   'Open': '86.18',
   'Volume': '4700'},
  {'Adj Close': '82.16',
   'Close': '85.48',
   'Date': '2013-09-13',
   'High': '86.31',
   'Low': '85.12',
   'Open': '85.98',
   'Volume': '205000'},
  {'Adj Close': '82.10',
   'Close': '85.42',
   'Date': '2013-09-12',
   'High': '86.60',
   'Low': '85.08',
   'Open': '86.33',
   'Volume': '16000'},
  {'Adj Close': '82.15',
   'Close': '85.47',
   'Date': '2013-09-11',
   'High': '85.47',
   'Low': '84.47',
   'Open': '85.14',
   'Volume': '9700'}}
Community
  • 1
  • 1
Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75

2 Answers2

2

In your price = [price[item] for price in pricelist if price["Date"] == eff_date] line, you are using item which is previously used in a for loop:

for item in pricelist: item['Date'] = datetime.datetime.strptime(item['Date'],"%Y-%m-%d")

item is a dictionary, you cannot use it as a key in the price dictionary.

Rikka
  • 999
  • 8
  • 19
1

You have the nested dictionary for item. You need to access data as item['ABCD']['Date'] instead of item['Date'] inside pricelist forloop.

So changing the code as below would work:

for item in pricelist:
    for k in item:
        item[k]['Date'] = datetime.datetime.strptime(item[k]['Date'],"%Y-%m-%d")
venpa
  • 4,268
  • 21
  • 23