-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[ENH] Remove yfinance as a dependency and implement data_loader #721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .data_loader import available_tickers, load_marketcaps, load_stockdata | ||
|
|
||
| __all__ = ["load_stockdata", "available_tickers", "load_marketcaps"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from importlib import resources | ||
|
|
||
| import pandas as pd | ||
|
|
||
|
|
||
| def _load_raw_data(filename: str, **read_csv_kwargs): | ||
| with resources.files(__package__).joinpath(filename).open("r") as f: | ||
| return pd.read_csv(f, **read_csv_kwargs) | ||
|
|
||
|
|
||
| def load_stockdata(tickers: list = None, start: str = None, end: str = None): | ||
| df = _load_raw_data("stock_prices.csv", parse_dates=["date"]) | ||
|
|
||
| if start is not None: | ||
| df = df[df["date"] >= pd.to_datetime(start)] | ||
| if end is not None: | ||
| df = df[df["date"] <= pd.to_datetime(end)] | ||
|
|
||
| if tickers is not None: | ||
| cols = ["date"] + tickers | ||
| df = df[cols] | ||
|
|
||
| return df.set_index("date") | ||
|
|
||
|
|
||
| def load_marketcaps(tickers: list = None): | ||
| df = _load_raw_data("market_caps.csv") | ||
|
|
||
| if tickers is not None: | ||
| available = set(df["ticker"]) | ||
| invalid = set(tickers) - available | ||
| if invalid: | ||
| raise ValueError(f"Invalid tickers: {invalid}") | ||
|
|
||
| df = df[df["ticker"].isin(tickers)] | ||
|
|
||
| return dict(zip(df["ticker"], df["market_cap"])) | ||
|
|
||
|
|
||
| def available_tickers(): | ||
| df = _load_raw_data("stock_prices.csv", parse_dates=["date"]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this not known in advance? Instad of loading the csv, you could simply load the header, or return the known list |
||
| cols = [c for c in df.columns if c != "date"] | ||
| cols.sort() | ||
|
|
||
| return cols | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| ticker,market_cap | ||
| AAPL,752207537100 | ||
| ACN,1901675041287 | ||
| AMD,1465327913913 | ||
| AMZN,1199323675973 | ||
| BAC,316257187682 | ||
| BLK,316209068070 | ||
| COST,120876806275 | ||
| CVS,1733021410820 | ||
| DIS,1204224448427 | ||
| DPZ,1417604792703 | ||
| F,46066066120 | ||
| GILD,1939970155063 | ||
| INTU,1665723068396 | ||
| JD,428616525803 | ||
| JPM,367740809578 | ||
| KO,370891997157 | ||
| LUV,611963274704 | ||
| MA,1051889081106 | ||
| MCD,866730312191 | ||
| MSFT,586002134695 | ||
| NAT,1225646524971 | ||
| NVDA,283290252000 | ||
| PBI,587828573827 | ||
| PFE,735891877370 | ||
| SBUX,914859618512 | ||
| SPY,1571426042979 | ||
| TGT,403349195405 | ||
| TM,1030897704635 | ||
| TSLA,1186867064879 | ||
| UL,97668573376 | ||
| UNH,1217051979543 | ||
| WMT,345195626756 | ||
| XOM,134777928005 |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add docstrings (numpydoc format)