From f84a2d141c955b20770805a179d6ccb38dac153c Mon Sep 17 00:00:00 2001 From: MJ Rossetti Date: Sat, 16 Nov 2024 14:05:31 -0500 Subject: [PATCH] Financial datasets --- .../pandas-datareader.qmd | 18 +++---------- .../financial-data-sources/yahooquery.qmd | 27 ++++++++++--------- .../notes/financial-data-sources/yfinance.qmd | 6 ++--- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/docs/notes/financial-data-sources/pandas-datareader.qmd b/docs/notes/financial-data-sources/pandas-datareader.qmd index cc8c3ff..eaeb1dc 100644 --- a/docs/notes/financial-data-sources/pandas-datareader.qmd +++ b/docs/notes/financial-data-sources/pandas-datareader.qmd @@ -85,21 +85,9 @@ We can access [FRED datasets](https://fred.stlouisfed.org/) directly in Python u ```{python} from pandas_datareader import get_data_fred -dataset_id = "GDP" -df = get_data_fred(dataset_id) -df.head() -``` - - -Specifying start and end dates, as desired: - -```{python} -from datetime import datetime -from pandas_datareader import get_data_fred - -start = datetime(1900, 1, 1) -end = datetime(2023, 1, 1) +dataset_id = "GDP" # quarterly -df = get_data_fred(dataset_id, start, end) +#df = get_data_fred(dataset_id) +df = get_data_fred(dataset_id, start="2001-01-01", end="2023-01-01") df.head() ``` diff --git a/docs/notes/financial-data-sources/yahooquery.qmd b/docs/notes/financial-data-sources/yahooquery.qmd index e7201b3..106e049 100644 --- a/docs/notes/financial-data-sources/yahooquery.qmd +++ b/docs/notes/financial-data-sources/yahooquery.qmd @@ -15,6 +15,7 @@ execute: #| echo: false import warnings warnings.simplefilter(action='ignore', category=FutureWarning) +#warnings.filterwarnings("ignore") ``` ## Single Company @@ -22,39 +23,42 @@ warnings.simplefilter(action='ignore', category=FutureWarning) ```{python} from yahooquery import Ticker -symbol = "NFLX" - -ticker = Ticker(symbol) -ticker +ticker = Ticker("NFLX") ``` +Company information: + ```{python} ticker.summary_profile ``` +Historical prices: ```{python} df = ticker.history(start="2024-01-01", end="2024-01-31", adj_ohlc=True) -#print(df.index) - df.head() ``` + ## Multiple Companies ```{python} from yahooquery import Ticker -symbols = ["MSFT", "AAPL", "GOOGL"] +tickers = Ticker( ["MSFT", "AAPL", "GOOGL"]) +``` + +Company information: -tickers = Ticker(symbols) -tickers +```{python} +tickers.summary_profile ``` +Historical prices: + ```{python} df = tickers.history() -#print(df.index) df.head() ``` @@ -63,13 +67,10 @@ Simplifying the multi-index: ```{python} from pandas import to_datetime -df = tickers.history() -#print(df.index) df["symbol"] = df.index.get_level_values(0) df["date"] = to_datetime(df.index.get_level_values(1)).date df.reset_index(drop=True, inplace=True) - df[["date", "symbol", "adjclose"]].head() ``` diff --git a/docs/notes/financial-data-sources/yfinance.qmd b/docs/notes/financial-data-sources/yfinance.qmd index 1cb0152..1756c20 100644 --- a/docs/notes/financial-data-sources/yfinance.qmd +++ b/docs/notes/financial-data-sources/yfinance.qmd @@ -5,7 +5,7 @@ Fetching data from Yahoo Finance: ```{python} import yfinance as yf -ticker = "NVDA" -df = yf.download(ticker, start="2014-01-01", end="2024-01-01") -df +symbol = "NVDA" +df = yf.download(symbol, start="2014-01-01", end="2024-01-31") +df.head() ```