From 5705129aa13a430a449dd9ebded2524c2b7d1901 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 8 Sep 2019 16:21:50 -0700 Subject: [PATCH 1/4] CLN: Exception in pd.plotting --- pandas/plotting/_matplotlib/converter.py | 20 ++++++++++---------- pandas/plotting/_matplotlib/core.py | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) 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..e8bd3077b88a2 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1196,10 +1196,10 @@ def _post_plot_logic(self, ax, data): from matplotlib.ticker import FixedLocator def get_label(i): - try: - return pprint_thing(data.index[i]) - except Exception: + if i >= len(data.index): + # TODO: is getting here indicative of a larger problem? return "" + return pprint_thing(data.index[i]) if self._need_to_set_index: xticks = ax.get_xticks() From ca03606190ac71023eeda878e29ada2350b4f5e9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Sep 2019 11:24:19 -0700 Subject: [PATCH 2/4] Fix error that occurs in slow plotting test --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index e8bd3077b88a2..9b5ce6fd77d4e 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1196,7 +1196,7 @@ def _post_plot_logic(self, ax, data): from matplotlib.ticker import FixedLocator def get_label(i): - if i >= len(data.index): + if i >= len(data.index) or not is_integer(i): # TODO: is getting here indicative of a larger problem? return "" return pprint_thing(data.index[i]) From a17a08c76a7abfd3cfc5e08a08631da695b4091a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Sep 2019 16:47:25 -0700 Subject: [PATCH 3/4] catch negative ints --- pandas/plotting/_matplotlib/core.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 9b5ce6fd77d4e..0cde964166c59 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -12,6 +12,7 @@ from pandas.core.dtypes.common import ( is_hashable, is_integer, + is_float, is_iterator, is_list_like, is_number, @@ -1196,10 +1197,17 @@ def _post_plot_logic(self, ax, data): from matplotlib.ticker import FixedLocator def get_label(i): - if i >= len(data.index) or not is_integer(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 "" - return pprint_thing(data.index[i]) + try: + 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() From 53b53f6147ac292e395d5185f762fac71b9b3ba7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Sep 2019 18:33:04 -0700 Subject: [PATCH 4/4] isort fixup --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 0cde964166c59..d798db8ad290b 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -10,9 +10,9 @@ from pandas.util._decorators import cache_readonly from pandas.core.dtypes.common import ( + is_float, is_hashable, is_integer, - is_float, is_iterator, is_list_like, is_number,