John Hanlon John Hanlon - 27 days ago 10
Python Question

Trying to iterate and join Pandas DFs: AttributeError: 'Series' object has no attribute 'join'

I'm looking to pull the historical data for ~200 securities in a given index. I import the list of securities from a csv file then iterate over them to pull their respective data from the quandl api. That dataframe for each security has 12 columns, so I create a new column with the name of the security and the Adjusted Close value, so I can later identify the series.

I'm receiving an error when I try to join all the new columns into an empty dataframe. I receive an attribute error:

'''
Print output data
'''
grab_constituent_data()
AttributeError: 'Series' object has no attribute 'join'


Below is the code I have used to arrive here thus far.

'''
Import the modules necessary for analysis
'''

import quandl
import pandas as pd
import numpy as np

'''
Set file pathes and API keys
'''

ticker_path = ''
auth_key = ''

'''
Pull a list of tickers in the IGM ETF
'''

def ticker_list():
df = pd.read_csv('{}IGM Tickers.csv'.format(ticker_path))
# print(df['Ticker'])
return df['Ticker']

'''
Pull the historical prices for the securities within Ticker List
'''

def grab_constituent_data():
tickers = ticker_list()
main_df = pd.DataFrame()

for abbv in tickers:
query = 'EOD/{}'.format(str(abbv))
df = quandl.get(query, authtoken=auth_key)
print('Competed the query for {}'.format(query))

df['{} Adj_Close'.format(str(abbv))] = df['Adj_Close'].copy()
df = df['{} Adj_Close'.format(str(abbv))]
print('Completed the column adjustment for {}'.format(str(abbv)))

if main_df.empty:
main_df = df
else:
main_df = main_df.join(df)

print(main_df.head())

Answer

It seems that in your line

df = df['{} Adj_Close'.format(str(abbv))]

you're getting a Serie and not a Dataframe. If you want to convert your serie to a dataframe, you can use the function to_frame() like:

df = df['{} Adj_Close'.format(str(abbv))].to_frame()

I didn't check if your code might be more simple, but this should fix your issue.