Skip to content
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

Replace empty list with empty pd.Series #1724

Merged
merged 10 commits into from
Jan 9, 2024
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pandas>=1.3.0
numpy>=1.16.5
requests>=2.31
requests_cache==1.1.0
requests_ratelimiter==0.4.2
multitasking>=0.0.7
lxml>=4.9.1
appdirs>=1.4.4
pytz>=2022.5
frozendict>=2.3.4
beautifulsoup4>=4.11.1
html5lib>=1.1
peewee>=3.16.2
peewee>=3.16.2
22 changes: 19 additions & 3 deletions tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,24 @@ def test_badTicker(self):
dat.fast_info[k]

for attribute_name, attribute_type in ticker_attributes:
assert_attribute_type(self, dat, attribute_name, attribute_type)

assert_attribute_type(self, dat, attribute_name, attribute_type)

with self.assertRaises(YFNotImplementedError):
assert isinstance(dat.earnings, pd.Series)
assert dat.earnings.empty
assert isinstance(dat.dividends, pd.Series)
assert dat.dividends.empty
assert isinstance(dat.splits, pd.Series)
assert dat.splits.empty
assert isinstance(dat.capital_gains, pd.Series)
assert dat.capital_gains.empty
with self.assertRaises(YFNotImplementedError):
assert isinstance(dat.shares, pd.DataFrame)
assert dat.shares.empty
assert isinstance(dat.actions, pd.DataFrame)
assert dat.actions.empty


def test_goodTicker(self):
# that yfinance works when full api is called on same instance of ticker

Expand Down Expand Up @@ -798,7 +814,7 @@ def test_info(self):
def suite():
suite = unittest.TestSuite()
suite.addTest(TestTicker('Test ticker'))
suite.addTest(TestTickerEarnings('Test earnings'))
#suite.addTest(TestTickerEarnings('Test earnings'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I branched from the wrong base I guess. Reverted.

suite.addTest(TestTickerHolders('Test holders'))
suite.addTest(TestTickerHistory('Test Ticker history'))
suite.addTest(TestTickerMiscFinancials('Test misc financials'))
Expand Down
27 changes: 15 additions & 12 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import logging
import time as _time
import warnings
from typing import Optional
from typing import Optional, Union, List
from urllib.parse import quote as urlencode

import dateutil as _dateutil
Expand All @@ -45,6 +45,9 @@
from .const import _BASE_URL_, _ROOT_URL_


_empty_series = pd.Series()
mreiche marked this conversation as resolved.
Show resolved Hide resolved


class TickerBase:
def __init__(self, ticker, session=None, proxy=None):
self.ticker = ticker.upper()
Expand Down Expand Up @@ -1883,7 +1886,7 @@ def get_balance_sheet(self, proxy=None, as_dict=False, pretty=False, freq="yearl
def get_balancesheet(self, proxy=None, as_dict=False, pretty=False, freq="yearly"):
return self.get_balance_sheet(proxy, as_dict, pretty, freq)

def get_cash_flow(self, proxy=None, as_dict=False, pretty=False, freq="yearly"):
def get_cash_flow(self, proxy=None, as_dict=False, pretty=False, freq="yearly") -> Union[pd.DataFrame, dict]:
"""
:Parameters:
as_dict: bool
Expand Down Expand Up @@ -1913,31 +1916,31 @@ def get_cash_flow(self, proxy=None, as_dict=False, pretty=False, freq="yearly"):
def get_cashflow(self, proxy=None, as_dict=False, pretty=False, freq="yearly"):
return self.get_cash_flow(proxy, as_dict, pretty, freq)

def get_dividends(self, proxy=None):
def get_dividends(self, proxy=None) -> pd.Series:
if self._history is None:
self.history(period="max", proxy=proxy)
if self._history is not None and "Dividends" in self._history:
dividends = self._history["Dividends"]
return dividends[dividends != 0]
return []
return _empty_series

def get_capital_gains(self, proxy=None):
def get_capital_gains(self, proxy=None) -> pd.Series:
if self._history is None:
self.history(period="max", proxy=proxy)
if self._history is not None and "Capital Gains" in self._history:
capital_gains = self._history["Capital Gains"]
return capital_gains[capital_gains != 0]
return []
return _empty_series

def get_splits(self, proxy=None):
def get_splits(self, proxy=None) -> pd.Series:
if self._history is None:
self.history(period="max", proxy=proxy)
if self._history is not None and "Stock Splits" in self._history:
splits = self._history["Stock Splits"]
return splits[splits != 0]
return []
return _empty_series

def get_actions(self, proxy=None):
def get_actions(self, proxy=None) -> pd.DataFrame:
if self._history is None:
self.history(period="max", proxy=proxy)
if self._history is not None and "Dividends" in self._history and "Stock Splits" in self._history:
Expand All @@ -1946,9 +1949,9 @@ def get_actions(self, proxy=None):
action_columns.append("Capital Gains")
actions = self._history[action_columns]
return actions[actions != 0].dropna(how='all').fillna(0)
return []
return pd.DataFrame()

def get_shares(self, proxy=None, as_dict=False):
def get_shares(self, proxy=None, as_dict=False) -> Union[pd.DataFrame, dict]:
self._fundamentals.proxy = proxy or self.proxy
data = self._fundamentals.shares
if as_dict:
Expand Down Expand Up @@ -2047,7 +2050,7 @@ def get_isin(self, proxy=None) -> Optional[str]:
self._isin = data.split(search_str)[1].split('"')[0].split('|')[0]
return self._isin

def get_news(self, proxy=None):
def get_news(self, proxy=None) -> list:
if self._news:
return self._news

Expand Down
6 changes: 3 additions & 3 deletions yfinance/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def dividends(self) -> _pd.Series:
return self.get_dividends()

@property
def capital_gains(self):
def capital_gains(self) -> _pd.Series:
return self.get_capital_gains()

@property
Expand All @@ -134,7 +134,7 @@ def actions(self) -> _pd.DataFrame:
return self.get_actions()

@property
def shares(self) -> _pd.DataFrame :
def shares(self) -> _pd.DataFrame:
return self.get_shares()

@property
Expand Down Expand Up @@ -240,7 +240,7 @@ def options(self) -> tuple:
return tuple(self._expirations.keys())

@property
def news(self):
def news(self) -> list:
return self.get_news()

@property
Expand Down