2727import logging
2828import time as _time
2929import warnings
30- from typing import Optional
30+ from typing import Optional , Union
3131from urllib .parse import quote as urlencode
3232
3333import dateutil as _dateutil
4242from .scrapers .holders import Holders
4343from .scrapers .quote import Quote , FastInfo
4444
45- from .const import _BASE_URL_ , _ROOT_URL_
45+ from .const import _BASE_URL_ , _ROOT_URL_ , price_colnames
46+
47+
48+ _empty_series = pd .Series ()
4649
4750
4851class TickerBase :
@@ -426,7 +429,9 @@ def history(self, period="1mo", interval="1d",
426429 if not actions :
427430 df = df .drop (columns = ["Dividends" , "Stock Splits" , "Capital Gains" ], errors = 'ignore' )
428431 if not keepna :
429- mask_nan_or_zero = (df .isna () | (df == 0 )).all (axis = 1 )
432+ data_colnames = price_colnames + ['Volume' ] + ['Dividends' , 'Stock Splits' , 'Capital Gains' ]
433+ data_colnames = [c for c in data_colnames if c in df .columns ]
434+ mask_nan_or_zero = (df [data_colnames ].isna () | (df [data_colnames ] == 0 )).all (axis = 1 )
430435 df = df .drop (mask_nan_or_zero .index [mask_nan_or_zero ])
431436
432437 logger .debug (f'{ self .ticker } : yfinance returning OHLC: { df .index [0 ]} -> { df .index [- 1 ]} ' )
@@ -455,7 +460,7 @@ def _reconstruct_intervals_batch(self, df, interval, prepost, tag=-1):
455460 else :
456461 intraday = True
457462
458- price_cols = [c for c in [ "Open" , "High" , "Low" , "Close" , "Adj Close" ] if c in df ]
463+ price_cols = [c for c in price_colnames if c in df ]
459464 data_cols = price_cols + ["Volume" ]
460465
461466 # If interval is weekly then can construct with daily. But if smaller intervals then
@@ -1011,7 +1016,7 @@ def _fix_zeroes(self, df, interval, tz_exchange, prepost):
10111016 elif df2 .index .tz != tz_exchange :
10121017 df2 .index = df2 .index .tz_convert (tz_exchange )
10131018
1014- price_cols = [c for c in [ "Open" , "High" , "Low" , "Close" , "Adj Close" ] if c in df2 .columns ]
1019+ price_cols = [c for c in price_colnames if c in df2 .columns ]
10151020 f_prices_bad = (df2 [price_cols ] == 0.0 ) | df2 [price_cols ].isna ()
10161021 df2_reserve = None
10171022 if intraday :
@@ -1916,7 +1921,7 @@ def get_balance_sheet(self, proxy=None, as_dict=False, pretty=False, freq="yearl
19161921 def get_balancesheet (self , proxy = None , as_dict = False , pretty = False , freq = "yearly" ):
19171922 return self .get_balance_sheet (proxy , as_dict , pretty , freq )
19181923
1919- def get_cash_flow (self , proxy = None , as_dict = False , pretty = False , freq = "yearly" ):
1924+ def get_cash_flow (self , proxy = None , as_dict = False , pretty = False , freq = "yearly" ) -> Union [ pd . DataFrame , dict ] :
19201925 """
19211926 :Parameters:
19221927 as_dict: bool
@@ -1946,31 +1951,31 @@ def get_cash_flow(self, proxy=None, as_dict=False, pretty=False, freq="yearly"):
19461951 def get_cashflow (self , proxy = None , as_dict = False , pretty = False , freq = "yearly" ):
19471952 return self .get_cash_flow (proxy , as_dict , pretty , freq )
19481953
1949- def get_dividends (self , proxy = None ):
1954+ def get_dividends (self , proxy = None ) -> pd . Series :
19501955 if self ._history is None :
19511956 self .history (period = "max" , proxy = proxy )
19521957 if self ._history is not None and "Dividends" in self ._history :
19531958 dividends = self ._history ["Dividends" ]
19541959 return dividends [dividends != 0 ]
1955- return []
1960+ return pd . Series ()
19561961
1957- def get_capital_gains (self , proxy = None ):
1962+ def get_capital_gains (self , proxy = None ) -> pd . Series :
19581963 if self ._history is None :
19591964 self .history (period = "max" , proxy = proxy )
19601965 if self ._history is not None and "Capital Gains" in self ._history :
19611966 capital_gains = self ._history ["Capital Gains" ]
19621967 return capital_gains [capital_gains != 0 ]
1963- return []
1968+ return _empty_series
19641969
1965- def get_splits (self , proxy = None ):
1970+ def get_splits (self , proxy = None ) -> pd . Series :
19661971 if self ._history is None :
19671972 self .history (period = "max" , proxy = proxy )
19681973 if self ._history is not None and "Stock Splits" in self ._history :
19691974 splits = self ._history ["Stock Splits" ]
19701975 return splits [splits != 0 ]
1971- return []
1976+ return pd . Series ()
19721977
1973- def get_actions (self , proxy = None ):
1978+ def get_actions (self , proxy = None ) -> pd . Series :
19741979 if self ._history is None :
19751980 self .history (period = "max" , proxy = proxy )
19761981 if self ._history is not None and "Dividends" in self ._history and "Stock Splits" in self ._history :
@@ -1979,9 +1984,9 @@ def get_actions(self, proxy=None):
19791984 action_columns .append ("Capital Gains" )
19801985 actions = self ._history [action_columns ]
19811986 return actions [actions != 0 ].dropna (how = 'all' ).fillna (0 )
1982- return []
1987+ return _empty_series
19831988
1984- def get_shares (self , proxy = None , as_dict = False ):
1989+ def get_shares (self , proxy = None , as_dict = False ) -> Union [ pd . DataFrame , dict ] :
19851990 self ._fundamentals .proxy = proxy or self .proxy
19861991 data = self ._fundamentals .shares
19871992 if as_dict :
@@ -2078,7 +2083,7 @@ def get_isin(self, proxy=None) -> Optional[str]:
20782083 self ._isin = data .split (search_str )[1 ].split ('"' )[0 ].split ('|' )[0 ]
20792084 return self ._isin
20802085
2081- def get_news (self , proxy = None ):
2086+ def get_news (self , proxy = None ) -> list :
20822087 if self ._news :
20832088 return self ._news
20842089
0 commit comments