Search code examples
python-3.xpandaspandas-datareader

How do I rename a blank column name in pandas df?


I am using pandas_datareader to return stock prices. The document states that a pandas data frame is returned by the pandas_datareader. The issue is that the data frame is returned with a blank column name. That means until I rename the column I (believe this to be true) cannot add another column. Anyway here is my code:

from datetime import datetime, timedelta
from pandas import pandas as pd
from pandas_datareader import data, wb
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter
import matplotlib.pyplot as plt
from matplotlib import style
import datetime as dt 
import numpy as np

style.use('ggplot')
today = dt.date.today()
dtStart = (dt.datetime.strptime("2017-01-01", '%Y-%m-%d'))
dtEnd = (dt.datetime(today.year,today.month,today.day)).isoformat(' ') 

def genGraph(graph_filename):

    dtStart = (dt.datetime.strptime("2015-01-01", '%Y-%m-%d'))
    my_stocks = {'VOD.L'} # Single sumbol.
    # my_stocks = {'VOD.L', {'BP.L'}} # Test multiple symbols.

    main_df = pd.DataFrame()
    df = pd.DataFrame()

    for sym in my_stocks:

        if main_df.empty:
            main_df = (data.get_data_google(sym, dtStart)['Close'])
            main_df.rename(columns = {'' : 'X'}, inplace=True)

            print(main_df.head())

        else:
            main_df = main_df.join((data.get_data_google(sym, dtStart)['Close']), how='outer')
            print(main_df)
    else:
        print('')    
    print(main_df.tail())
    return True
genGraph('OK')

This currently returns: One Symbol

Two Symbols

I have tried various things however it seems that the column with the prices cannot be renamed. I have also tried renaming the date column and making that as an index. No luck! I wonder if it's better to stick the data into a table and then get it back. Pandas data frame seems to be overly complicated for simple operations.

I have python 3.5.3 and the latest pandas.


Solution

  • That AttributeError posted in the comments is important – you have a Series, equivalent to a single column, not a DataFrame.

    main_df.to_frame()
    

    will return a dataframe, which you can then rename the columns of and add new columns to.