Description
Bug summary
Basically, plotting from a dataframe failed because of a keyerror but the message I received was regarding formatting using a string. The failure happened silently, causing me to spend over an hour tracking down a type because I had no clue where to start.
Code for reproduction
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> data = [ [1,1], [2,2], [3,3] ]
>>> df = pd.DataFrame(data, columns = ['header','mispelledHeader'])
>>> figure, axes = plt.subplots()
>>> line = axes.plot('header','correctlySpelledHeader',data = df)
Actual outcome
Traceback (most recent call last):
File "", line 1, in
File "/home/b_briscoe/thirdparty/phel-1.2.0/linux_x86_64_9.4.0/miniconda3-4.9.2/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/home/b_briscoe/thirdparty/phel-1.2.0/linux_x86_64_9.4.0/miniconda3-4.9.2/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 315, in call
yield from self._plot_args(this, kwargs)
File "/home/b_briscoe/thirdparty/phel-1.2.0/linux_x86_64_9.4.0/miniconda3-4.9.2/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 452, in _plot_args
linestyle, marker, color = _process_plot_format(fmt)
File "/home/b_briscoe/thirdparty/phel-1.2.0/linux_x86_64_9.4.0/miniconda3-4.9.2/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 188, in _process_plot_format
raise ValueError(
ValueError: Illegal format string "correctlySpelledHeader"; two color symbols
Expected outcome
The actual failure is happening when the df and key are passed into this as data and value respectively.
mpl._replacer(data,value):
----try:
--------# if key isn't a string don't bother
--------if isinstance(value, str):
--------# try to use getitem
--------value = data[value] <-----------------------Key Error because of typo
----except Exception:
--------# key does not exist, silently fall back to key
--------pass
----return sanitize_sequence(value)
As you can see from the comment, this happens silently. And as you can see from the Traceback provided the error you finally receive is regarding a formatting string. So this caused quite a bit of confusion, because I was looking everywhere except my header spellings. I feel like this shouldn't happen 'silently', it at least deseves a warning, perhaps:
----except Exception:
--------warnings.warn('KeyError generated when attempting to access data using provided str')
side note: the docstring says it returns data[value] or data back. in reality it passes back data[value] or value back. Not sure what the purpose is for allowing this to slide through, but either the behavior is wrong or the docstring is.
Additional information
No response
Operating system
Ubuntu 20.04
Matplotlib Version
3.4.2
Matplotlib Backend
Qt5Agg
Python version
3.9.1
Jupyter version
No response
Installation
No response