-
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
Open
Shuvam586
wants to merge
5
commits into
PyPortfolio:main
Choose a base branch
from
Shuvam586:remove-yfinance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,547
−4,789
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbc653a
[ENH] Remove yfinance as a dependancy and implement data_loader
Shuvam586 6486710
replaced yfinance data with synthetic data
Shuvam586 67a54db
pre-commit fixes
Shuvam586 aeec39d
Merge branch 'main' into remove-yfinance
Shuvam586 6f254b6
added docstrings to data_loader
Shuvam586 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| 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): | ||
| """ | ||
| Load example stock price data. | ||
|
|
||
| This function loads a synthetic stock price dataset included with the | ||
| package. The data can optionally be filtered by ticker symbols and | ||
| date range. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tickers : list of str, optional | ||
| List of ticker symbols to include. If ``None``, all available | ||
| tickers are returned. | ||
| start : str, optional | ||
| Start date for filtering the dataset (inclusive). Should be | ||
| interpretable by ``pandas.to_datetime``. | ||
| end : str, optional | ||
| End date for filtering the dataset (inclusive). Should be | ||
| interpretable by ``pandas.to_datetime``. | ||
|
|
||
| Returns | ||
| ------- | ||
| pandas.DataFrame | ||
| DataFrame of stock prices indexed by date. Columns correspond to | ||
| ticker symbols and values represent price levels. | ||
|
|
||
| Notes | ||
| ----- | ||
| The dataset is bundled with the package and does not rely on external | ||
| data sources. It is intended for examples and tutorials. | ||
| """ | ||
| 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): | ||
| """ | ||
| Load bundled example market capitalisation data. | ||
|
|
||
| This function loads synthetic market capitalisation values for the | ||
| example assets included in the package. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tickers : list of str, optional | ||
| List of ticker symbols to return. If ``None``, market caps for all | ||
| available tickers are returned. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| Dictionary mapping ticker symbols to market capitalisation values. | ||
|
|
||
| Notes | ||
| ----- | ||
| The values are synthetic and provided solely for use in examples | ||
| demonstrating portfolio optimisation methods. | ||
| """ | ||
| 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(): | ||
| """ | ||
| Return the list of available ticker symbols. | ||
|
|
||
| Returns | ||
| ------- | ||
| list of str | ||
| Sorted list of ticker symbols present in the bundled example | ||
| dataset. | ||
|
|
||
| Notes | ||
| ----- | ||
| These tickers correspond to the columns available in the example | ||
| stock price dataset returned by :func:`load_stockdata`. | ||
| """ | ||
| cols = [ | ||
| "AAPL", | ||
| "ACN", | ||
| "AMD", | ||
| "AMZN", | ||
| "BAC", | ||
| "BLK", | ||
| "COST", | ||
| "CVS", | ||
| "DIS", | ||
| "DPZ", | ||
| "F", | ||
| "GILD", | ||
| "INTU", | ||
| "JD", | ||
| "JPM", | ||
| "KO", | ||
| "LUV", | ||
| "MA", | ||
| "MCD", | ||
| "MSFT", | ||
| "NAT", | ||
| "NVDA", | ||
| "PBI", | ||
| "PFE", | ||
| "SBUX", | ||
| "SPY", | ||
| "TGT", | ||
| "TM", | ||
| "TSLA", | ||
| "UL", | ||
| "UNH", | ||
| "WMT", | ||
| "XOM", | ||
| ] | ||
|
|
||
| return cols | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)
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.
added docstrings