From c7786760a7aad378b7ed4119f1973e296327faaa Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 26 Sep 2019 16:47:27 -0700 Subject: [PATCH 1/2] CLN: Exception in io, plotting --- pandas/plotting/_matplotlib/tools.py | 13 +++++-------- pandas/tests/io/test_sql.py | 7 +++++-- pandas/tests/plotting/common.py | 12 ++++-------- pandas/tests/plotting/test_datetimelike.py | 9 +++------ 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index 1c9bd01b16739..eddc9b4cd21bd 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -12,14 +12,11 @@ def format_date_labels(ax, rot): # mini version of autofmt_xdate - try: - for label in ax.get_xticklabels(): - label.set_ha("right") - label.set_rotation(rot) - fig = ax.get_figure() - fig.subplots_adjust(bottom=0.2) - except Exception: # pragma: no cover - pass + for label in ax.get_xticklabels(): + label.set_ha("right") + label.set_rotation(rot) + fig = ax.get_figure() + fig.subplots_adjust(bottom=0.2) def table(ax, data, rowLabels=None, colLabels=None, **kwargs): diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 89bc98b5a1006..7491cef17ebfc 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -539,13 +539,16 @@ def _transaction_test(self): with self.pandasSQL.run_transaction() as trans: trans.execute("CREATE TABLE test_trans (A INT, B TEXT)") + class DummyException(Exception): + pass + # Make sure when transaction is rolled back, no rows get inserted ins_sql = "INSERT INTO test_trans (A,B) VALUES (1, 'blah')" try: with self.pandasSQL.run_transaction() as trans: trans.execute(ins_sql) - raise Exception("error") - except Exception: + raise DummyException("error") + except DummyException: # ignore raised exception pass res = self.pandasSQL.read_query("SELECT * FROM test_trans") diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 5a591f72d7361..b7ecc6be43733 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -536,18 +536,14 @@ def _check_plot_works(f, filterwarnings="always", **kwargs): plt.clf() - ax = kwargs.get("ax", fig.add_subplot(211)) # noqa + kwargs.get("ax", fig.add_subplot(211)) ret = f(**kwargs) assert_is_valid_plot_return_object(ret) - try: - kwargs["ax"] = fig.add_subplot(212) - ret = f(**kwargs) - except Exception: - pass - else: - assert_is_valid_plot_return_object(ret) + kwargs["ax"] = fig.add_subplot(212) + ret = f(**kwargs) + assert_is_valid_plot_return_object(ret) with ensure_clean(return_filelike=True) as path: plt.savefig(path) 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 ff1805aceb937421e30842de1662177a62a4d0db Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 19 Oct 2019 09:17:53 -0700 Subject: [PATCH 2/2] TST: xfail broken test, stop suppressing Exception --- pandas/tests/plotting/common.py | 7 ++++++- pandas/tests/plotting/test_frame.py | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index b7ecc6be43733..5e2663c0e0bea 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -12,6 +12,7 @@ from pandas.core.dtypes.api import is_list_like +import pandas as pd from pandas import DataFrame, Series import pandas.util.testing as tm from pandas.util.testing import assert_is_valid_plot_return_object, ensure_clean @@ -541,7 +542,11 @@ def _check_plot_works(f, filterwarnings="always", **kwargs): assert_is_valid_plot_return_object(ret) - kwargs["ax"] = fig.add_subplot(212) + if f is pd.plotting.bootstrap_plot: + assert "ax" not in kwargs + else: + kwargs["ax"] = fig.add_subplot(212) + ret = f(**kwargs) assert_is_valid_plot_return_object(ret) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 84badba271fce..fd66888fc30e4 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -3,6 +3,7 @@ """ Test cases for DataFrame.plot """ from datetime import date, datetime +import itertools import string import warnings @@ -2604,12 +2605,6 @@ def test_errorbar_plot(self): ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4) self._check_has_errorbars(ax, xerr=0, yerr=2) - # yerr is iterator - import itertools - - ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df))) - self._check_has_errorbars(ax, xerr=0, yerr=2) - # yerr is column name for yerr in ["yerr", "誤差"]: s_df = df.copy() @@ -2626,6 +2621,17 @@ def test_errorbar_plot(self): with pytest.raises((ValueError, TypeError)): df.plot(yerr=df_err) + @pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError) + @pytest.mark.slow + def test_errorbar_plot_iterator(self): + with warnings.catch_warnings(): + d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} + df = DataFrame(d) + + # yerr is iterator + ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df))) + self._check_has_errorbars(ax, xerr=0, yerr=2) + @pytest.mark.slow def test_errorbar_with_integer_column_names(self): # test with integer column names