From ee84b49af039a005aa7a11ca411e71c4944e0602 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 24 Sep 2019 17:38:00 -0700 Subject: [PATCH 1/4] CLN: Exception x5 --- pandas/_libs/tslibs/conversion.pyx | 2 +- pandas/_libs/tslibs/parsing.pyx | 4 ++-- pandas/plotting/_matplotlib/tools.py | 20 +++++++++----------- pandas/tests/plotting/common.py | 7 ++++++- pandas/tests/plotting/test_datetimelike.py | 9 +++------ 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 0a3f4ed3cc91d..bd74180403ad9 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -519,7 +519,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit, try: ts = parse_datetime_string(ts, dayfirst=dayfirst, yearfirst=yearfirst) - except Exception: + except (ValueError, OverflowError): raise ValueError("could not convert string to Timestamp") return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index d099a77a77044..ca70c8af45f2f 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -309,9 +309,9 @@ cdef parse_datetime_string_with_reso(date_string, freq=None, dayfirst=False, parsed, reso = dateutil_parse(date_string, _DEFAULT_DATETIME, dayfirst=dayfirst, yearfirst=yearfirst, ignoretz=False, tzinfos=None) - except Exception as e: + except (ValueError, OverflowError) as err: # TODO: allow raise of errors within instead - raise DateParseError(e) + raise DateParseError(err) if parsed is None: raise DateParseError("Could not parse {dstr}".format(dstr=date_string)) return parsed, parsed, reso diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index 67fa79ad5da8c..1c9bd01b16739 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -281,17 +281,15 @@ def _remove_labels_from_axis(axis): for t in axis.get_majorticklabels(): t.set_visible(False) - try: - # set_visible will not be effective if - # minor axis has NullLocator and NullFormattor (default) - if isinstance(axis.get_minor_locator(), ticker.NullLocator): - axis.set_minor_locator(ticker.AutoLocator()) - if isinstance(axis.get_minor_formatter(), ticker.NullFormatter): - axis.set_minor_formatter(ticker.FormatStrFormatter("")) - for t in axis.get_minorticklabels(): - t.set_visible(False) - except Exception: # pragma no cover - raise + # set_visible will not be effective if + # minor axis has NullLocator and NullFormattor (default) + if isinstance(axis.get_minor_locator(), ticker.NullLocator): + axis.set_minor_locator(ticker.AutoLocator()) + if isinstance(axis.get_minor_formatter(), ticker.NullFormatter): + axis.set_minor_formatter(ticker.FormatStrFormatter("")) + for t in axis.get_minorticklabels(): + t.set_visible(False) + axis.get_label().set_visible(False) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 5a591f72d7361..22f5eaa857dc9 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -544,7 +544,12 @@ def _check_plot_works(f, filterwarnings="always", **kwargs): try: kwargs["ax"] = fig.add_subplot(212) ret = f(**kwargs) - except Exception: + except (ValueError, AttributeError): + # e.g. + # AttributeError in test_bootstrap_plot + # "'Line2D' object has no property 'ax'" + # ValueError in test_errorbar_plot + # "lengths of the data (12) and the error 0 do not match" pass else: assert_is_valid_plot_return_object(ret) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index e2b7f2819f957..be24f102851b7 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -1553,12 +1553,9 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs): assert ax.freq == freq ax = fig.add_subplot(212) - try: - kwargs["ax"] = ax - ret = f(*args, **kwargs) - assert ret is not None # do something more intelligent - except Exception: - pass + kwargs["ax"] = ax + ret = f(*args, **kwargs) + assert ret is not None # TODO: do something more intelligent with ensure_clean(return_filelike=True) as path: plt.savefig(path) From da6474d94ffd8893b2c9f1956864435bc7b28038 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 24 Sep 2019 19:53:50 -0700 Subject: [PATCH 2/4] lint fixup --- pandas/tests/groupby/test_categorical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index e09af3fd48ee6..fcc0aa3b1c015 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -782,7 +782,7 @@ def test_categorical_no_compress(): def test_sort(): - # http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby # noqa: flake8 + # http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby # noqa: E501 # This should result in a properly sorted Series so that the plot # has a sorted x axis # self.cat.groupby(['value_group'])['value_group'].count().plot(kind='bar') From 8a9d12d663232ee20a816c437d6a46e32fca571c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 25 Sep 2019 09:14:09 -0700 Subject: [PATCH 3/4] Revert change that breaks with TypeError --- pandas/tests/plotting/test_datetimelike.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index be24f102851b7..e2b7f2819f957 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -1553,9 +1553,12 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs): assert ax.freq == freq ax = fig.add_subplot(212) - kwargs["ax"] = ax - ret = f(*args, **kwargs) - assert ret is not None # TODO: do something more intelligent + try: + kwargs["ax"] = ax + ret = f(*args, **kwargs) + assert ret is not None # do something more intelligent + except Exception: + pass with ensure_clean(return_filelike=True) as path: plt.savefig(path) From 27076407c5a5577ccf8fc8fb6477825523ce873d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 25 Sep 2019 11:34:28 -0700 Subject: [PATCH 4/4] revertt change that breaks on old numpy --- pandas/tests/plotting/common.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 22f5eaa857dc9..5a591f72d7361 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -544,12 +544,7 @@ def _check_plot_works(f, filterwarnings="always", **kwargs): try: kwargs["ax"] = fig.add_subplot(212) ret = f(**kwargs) - except (ValueError, AttributeError): - # e.g. - # AttributeError in test_bootstrap_plot - # "'Line2D' object has no property 'ax'" - # ValueError in test_errorbar_plot - # "lengths of the data (12) and the error 0 do not match" + except Exception: pass else: assert_is_valid_plot_return_object(ret)