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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pytz>=2022.5
frozendict>=2.3.4
beautifulsoup4>=4.11.1
html5lib>=1.1
peewee>=3.16.2
peewee>=3.16.2
20 changes: 18 additions & 2 deletions tests/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,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
24 changes: 12 additions & 12 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import logging
import time as _time
import warnings
from typing import Optional
from typing import Optional, Union
from urllib.parse import quote as urlencode

import dateutil as _dateutil
Expand Down Expand Up @@ -1916,7 +1916,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 @@ -1946,31 +1946,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 pd.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 pd.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 pd.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 @@ -1979,9 +1979,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 @@ -2078,7 +2078,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 @@ -134,7 +134,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 @@ -146,7 +146,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 @@ -259,7 +259,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