1

The code below starts by uploading data from yahoo finance in pnls. It does this successufly. Then I have code where I want to add columns to the loaded data in pnls. This part is unsuccessful. Finally I want to save pnls to a csv file which it does successfully if the column changes are not in the code.

The question that I need answered is how do I successfully make multiple column changes before I save pnls to csv??

from pandas_datareader import data as dreader
import pandas as pd
from datetime import datetime
import numpy as np

# Symbols is a list of all the ticker symbols that I am downloading from yahoo finance
symbols = ['tvix','pall','pplt','sivr','jju','nib','jo','jjc','bal','jjg','ld','cdw','gaz','jjn','pgm',
           'sgg','jjt','grn','oil','gld','corn','soyb','weat','uhn','uga','bunl','jgbl','bndx','aunz',
           'cemb','emhy','vwob','ald','udn','fxa','fxb'] 

#This gets the data from yahoo finance 
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in symbols}

 # These are the new columns that I want to add
pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100 
pnls['U_D_F']  = np.where(pnls.PofChg > 0 , 'Up', np.where(pnls.PofChg == 0, 'Flat', 'Down'));pnls
pnls['Up_Down']   = pnls.Close.diff()
pnls['Close%pd']  = pnls.Close.pct_change()*100


# This saves the current ticker to a csv file
for df_name in pnls:
    pnls.get(df_name).to_csv("{}_data.csv".format(df_name), index=True, header=True)

This is the error that I get when I run the code

Traceback (most recent call last):
  File "YahooFinanceDataGetter.py", line 14, in <module>
    pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100
AttributeError: 'dict' object has no attribute 'Close'
Press any key to continue . . .
ZacAttack
  • 1,035
  • 2
  • 12
  • 29
  • the df you want to load is actually `pnls.get(df_name)`: why don't you apply your dataframe modifications inside the existing for loop?? – Zeugma Sep 18 '16 at 03:26

1 Answers1

1

The line

pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in symbols}

builds a python dict (using dictionary comprehension). To check this, you can run the following:

assert type(pnls) == dict

This is what the error message is telling you: AttributeError: 'dict' object has no attribute 'Close'.

The DataFrames are actually the values of the dictionary. To apply transformations to them, you can iterate over the values() of the dictionary:

for df in pnls.values():
    df = ((df.Close - df.Open) / (df.Open)) * 100 
    ...
Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185