#!/usr/bin/env python
""" pandas_finance.

Usage:
    pandas_finance.py STOCKS START_DATE END_DATE

Arguments:
    STOCKS     comma separated stock ticker symbols, e.g. FB,TWTR
"""
import datetime
import sqlite3
import sys

import pandas.io.data as web
import pandas.io.sql as sql


def get_stock(stock, start, end):
    """
    Return data frame of Yahoo! Finance data for stock.

    Takes stock name and start and end dates as datetimes.
    """
    return web.DataReader(stock, 'yahoo', start, end)


def scrape_stock(stock, start, end):
    """
    Write SQLite table of stock data from Yahoo! Finance

    Take a stock name and start and end dates as datetimes.
    """
    sqlite_db.execute("drop table if exists {};".format(stock))
    frame = (get_stock(stock, start, end))
    # make Date not an index so it appears in table
    frame = frame.reset_index()
    # force Date datetime to string
    frame[['Date']] = frame[['Date']].applymap(lambda x: x.isoformat())
    sql.write_frame(frame, stock, sqlite_db)


def parse_wanted_stocks(stocks_string):
    """
    Take comma-separated string of stocks; return list of strings.
    >>> parse_wanted_stocks('TWTR,FB')
    ['TWTR', 'FB']
    >>> parse_wanted_stocks('TWTR')
    ['TWTR']
    """
    return stocks_string.split(',')


def main():
    """
    Save stock ticker data from Yahoo! Finance to sqlite.
    """
    # Yahoo: "Historical prices typically do not go back further than 1970"
    start = datetime.datetime(1900, 1, 1, 0, 0)
    end = datetime.datetime.today()
    global sqlite_db
    sqlite_db = sqlite3.connect("scraperwiki.sqlite")
    for ticker in parse_wanted_stocks(sys.argv[1]):
        scrape_stock(ticker, start, end)

if __name__ == '__main__':
    main()

@@ -673,72 +673,8 @@
 %22%22%22%0A
-    sqlite_db.execute(%22drop table if exists %7B%7D;%22.format(stock))%0A
     
@@ -904,16 +904,43 @@
 rmat())%0A
+    frame%5B'Stock'%5D = stock%0A
     sql.
@@ -958,21 +958,24 @@
 (frame, 
+'
 stock
+s'
 , sqlite
@@ -977,16 +977,36 @@
 qlite_db
+, if_exists='append'
 )%0A%0A%0Adef 
@@ -1590,16 +1590,83 @@
 qlite%22)%0A
+    sqlite_db.execute(%22drop table if exists %7B%7D;%22.format('stocks'))%0A
     for 

Store all stocks in single table.