Skip to content

Commit

Permalink
Merge pull request #567 from DanielGoldfarb/master
Browse files Browse the repository at this point in the history
Check for whitespace in column names when column not found.
  • Loading branch information
DanielGoldfarb authored Nov 1, 2022
2 parents 8d46383 + c7005ab commit d5fff66
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
55 changes: 36 additions & 19 deletions src/mplfinance/_arg_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ def _check_and_prepare_data(data, config):
if not isinstance(data.index,pd.core.indexes.datetimes.DatetimeIndex):
raise TypeError('Expect data.index as DatetimeIndex')

if (len(data.index) > config['warn_too_much_data'] and
(config['type']=='candle' or config['type']=='ohlc' or config['type']=='hollow_and_filled')
):
warnings.warn('\n\n ================================================================= '+
'\n\n WARNING: YOU ARE PLOTTING SO MUCH DATA THAT IT MAY NOT BE'+
'\n POSSIBLE TO SEE DETAILS (Candles, Ohlc-Bars, Etc.)'+
'\n For more information see:'+
'\n - https://github.com/matplotlib/mplfinance/wiki/Plotting-Too-Much-Data'+
'\n '+
'\n TO SILENCE THIS WARNING, set `type=\'line\'` in `mpf.plot()`'+
'\n OR set kwarg `warn_too_much_data=N` where N is an integer '+
'\n LARGER than the number of data points you want to plot.'+
'\n\n ================================================================ ',
category=UserWarning)

# We will not be fully case-insensitive (since Pandas columns as NOT case-insensitive)
# but because so many people have requested it, for the default column names we will
# try both Capitalized and lower case:
Expand All @@ -57,10 +42,22 @@ def _check_and_prepare_data(data, config):
o, h, l, c, v = columns
cols = [o, h, l, c]

if config['tz_localize']:
dates = mdates.date2num(data.index.tz_localize(None).to_pydatetime())
else: # Just in case someone was depending on this bug (Issue 236)
dates = mdates.date2num(data.index.to_pydatetime())
if config['volume'] != False:
expect_cols = columns
else:
expect_cols = cols

for col in expect_cols:
if col not in data.columns:
for dc in data.columns:
if dc.strip() != dc:
warnings.warn('\n ================================================================= '+
'\n Input DataFrame column name "'+dc+'" '+
'\n contains leading and/or trailing whitespace.',category=UserWarning)
raise ValueError('Column "'+col+'" NOT FOUND in Input DataFrame!'+
'\n CHECK that your column names are correct AND/OR'+
'\n CHECK for leading or trailing blanks in your column names.')

opens = data[o].values
highs = data[h].values
lows = data[l].values
Expand All @@ -75,6 +72,26 @@ def _check_and_prepare_data(data, config):
if not all( isinstance(v,(float,int)) for v in data[col] ):
raise ValueError('Data for column "'+str(col)+'" must be ALL float or int.')

if config['tz_localize']:
dates = mdates.date2num(data.index.tz_localize(None).to_pydatetime())
else: # Just in case someone was depending on this bug (Issue 236)
dates = mdates.date2num(data.index.to_pydatetime())

if (len(data.index) > config['warn_too_much_data'] and
(config['type']=='candle' or config['type']=='ohlc' or config['type']=='hollow_and_filled')
):
warnings.warn('\n\n ================================================================= '+
'\n\n WARNING: YOU ARE PLOTTING SO MUCH DATA THAT IT MAY NOT BE'+
'\n POSSIBLE TO SEE DETAILS (Candles, Ohlc-Bars, Etc.)'+
'\n For more information see:'+
'\n - https://github.com/matplotlib/mplfinance/wiki/Plotting-Too-Much-Data'+
'\n '+
'\n TO SILENCE THIS WARNING, set `type=\'line\'` in `mpf.plot()`'+
'\n OR set kwarg `warn_too_much_data=N` where N is an integer '+
'\n LARGER than the number of data points you want to plot.'+
'\n\n ================================================================ ',
category=UserWarning)

return dates, opens, highs, lows, closes, volumes

def _get_valid_plot_types(plottype=None):
Expand Down
2 changes: 1 addition & 1 deletion src/mplfinance/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version_info = (0, 12, 9, 'beta', 4)
version_info = (0, 12, 9, 'beta', 5)

_specifier_ = {'alpha': 'a','beta': 'b','candidate': 'rc','final': ''}

Expand Down
2 changes: 1 addition & 1 deletion src/mplfinance/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ def _auto_secondary_y( panels, panid, ylo, yhi ):

def _valid_addplot_kwargs():

valid_linestyles = ('-','solid','--','dashed','-.','dashdot','.','dotted',None,' ','')
valid_linestyles = ('-','solid','--','dashed','-.','dashdot',':','dotted',None,' ','')
valid_types = ('line','scatter','bar', 'ohlc', 'candle','step')
valid_stepwheres = ('pre','post','mid')
valid_edgecolors = ('face', 'none', None)
Expand Down

0 comments on commit d5fff66

Please sign in to comment.