pythonbook/实例学习pandas/股票分析.py

36 lines
973 B
Python

# import numpy and pandas
import numpy as np
import pandas as pd
# used for dates
import datetime
from datetime import datetime, date
# Set formattign options
pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', 7)
pd.set_option('display.max_rows', 10)
pd.set_option('display.width', 60)
# bring in matplotlib for graphics
import matplotlib.pyplot as plt
# 从雅虎获得股票数据
# pip install pandas_datareader
# import data reader 1.14 package
import pandas_datareader as pdr
# read data from Yahoo! Finance for a specific
# stock specified by ticker and between the start and end dates
def get_stock_data(ticker, start, end):
# read the data
data = pdr.data.DataReader(ticker, 'yahoo', start, end)
# rename this column
data.insert(0, "Ticker", ticker)
return data
# request the three years of data for MSFT
start = datetime(2012, 1, 1)
end = datetime(2014, 12, 31)
get_stock_data("MSFT", start, end)[:5]