Python: loop to "clip" (winsorizing) numerical data on a dataframe, assigning the result to a dictionary

Asked

Viewed 29 times

-1

What’s wrong with this code?

#calculando retornos diários máximos e mínimos:

max_std = 3
max = {}
min = {}
retornos_stds = retornos_dia.std()
retornos_med = retornos_dia.mean()

for acao in acoes:
    max[acao] = retornos_med[acao] + max_std * retornos_stds[acao]
    min[acao] = retornos_med[acao] - max_std * retornos_stds[acao]     

so far no problem, the maximum and minimum of each action are calculated normally and stored in the dictionaries "max" and "min"

#clipando:
retorno_clipado = {}

for acao in acoes:
    for contar,r in enumerate(retornos_dia[acao]):         
        if r > max[acao]:
            retorno_clipado[acao][contar] = max[acao]
        elif r < min[acao]:
            retorno_clipado[acao][contar] = min[acao]
        else:
            retorno_clipado[acao][contar] = retornos_dia[acao][contar]

retorno_clipado

here occurs the problem, the following error is returned:


KeyError                                  Traceback (most recent call last)
<ipython-input-69-fd5fc22b112d> in <module>
      8             retorno_clipado[acao][contar] = min[acao]
      9         else:
---> 10             retorno_clipado[acao][contar] = retornos_dia[acao][contar]
     11 
     12 retorno_clipado

KeyError: 'PETR3'

'PETR3' is the first element of shares

1 answer

0

I solved the problem:

#clipando:
retorno_clipado = retornos_dia
for acao in acoes:
    for contar,r in enumerate(retornos_dia[acao]):         
        if r > max[acao]:
            retorno_clipado[acao][contar] = max[acao]
        elif r < min[acao]:
            retorno_clipado[acao][contar] = min[acao]

retorno_clipado

Instead of creating clip_return as an empty dictionary, I created it as a copy of the original dataframe, which I want to "clip".

Apparently the problem appeared when trying to assign a value to a list position within a dictionary (such as a key value) that does not yet have the list or position or key defined:

teste = {}
teste['PETR3'][3] = 0.1

With this code we have the same error reported above. By trial and error I discovered that we were able to index this way both to read from a list within a dictionary and to write to a list within a dictionary PROVIDED that the list and position already exist. What is not possible is to write in a position of a list that does not yet exist in the dictionary using the same indexing. Example:

dicionario = {'chave1': 'gato','chave2': 'cachorro','chave3':['coelho1','coelho2','coelho3']}
dicionario['chave3'][1]

returns 'bunny 2'

dicionario['chave3'][1] = 'raposa'
print (dicionario)

return {'Chave1': 'cat', 'Chave2': 'dog', 'Chave3': ['rabbit 1', 'fox', 'rabbit 3']}

Already this:

dicionario['chave3'][4] = 'coelho4'
print (dicionario)

return Error as position 4 of the list which is the value of 'Chave3' does not exist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.