diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py index 446350cb5d915..b2bc9b2e4abf3 100644 --- a/pandas/plotting/_matplotlib/converter.py +++ b/pandas/plotting/_matplotlib/converter.py @@ -264,9 +264,11 @@ def convert(values, unit, axis): def _convert_1d(values, unit, axis): def try_parse(values): try: - return _dt_to_float_ordinal(tools.to_datetime(values)) - except Exception: + dtvalues = tools.to_datetime(values) + except (ValueError, TypeError): return values + else: + return _dt_to_float_ordinal(dtvalues) if isinstance(values, (datetime, pydt.date)): return _dt_to_float_ordinal(values) @@ -293,12 +295,13 @@ def try_parse(values): try: values = tools.to_datetime(values) + except (ValueError, TypeError): + values = _dt_to_float_ordinal(values) + else: if isinstance(values, Index): values = _dt_to_float_ordinal(values) else: values = [_dt_to_float_ordinal(x) for x in values] - except Exception: - values = _dt_to_float_ordinal(values) return values @@ -426,12 +429,9 @@ def __call__(self): ed = _from_ordinal(dates.date2num(dmax)) all_dates = date_range(start=st, end=ed, freq=freq, tz=tz).astype(object) - try: - if len(all_dates) > 0: - locs = self.raise_if_exceeds(dates.date2num(all_dates)) - return locs - except Exception: # pragma: no cover - pass + if len(all_dates) > 0: + locs = self.raise_if_exceeds(dates.date2num(all_dates)) + return locs lims = dates.date2num([dmin, dmax]) return lims diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 346949cb82c4d..d798db8ad290b 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -10,6 +10,7 @@ from pandas.util._decorators import cache_readonly from pandas.core.dtypes.common import ( + is_float, is_hashable, is_integer, is_iterator, @@ -1196,10 +1197,17 @@ def _post_plot_logic(self, ax, data): from matplotlib.ticker import FixedLocator def get_label(i): + if is_float(i) and i == int(i): + i = int(i) + if not is_integer(i): + # TODO: is getting here indicative of a larger problem? + return "" try: - return pprint_thing(data.index[i]) - except Exception: + val = data.index[i] + except IndexError: + # In tests we get here with both positive and negative `i` return "" + return pprint_thing(val) if self._need_to_set_index: xticks = ax.get_xticks()