diff --git a/rdkit/Chem/PandasTools.py b/rdkit/Chem/PandasTools.py index a3065c573..cebf3d428 100644 --- a/rdkit/Chem/PandasTools.py +++ b/rdkit/Chem/PandasTools.py @@ -197,19 +197,19 @@ def patchPandasrepr(self, **kwargs): global defPandasGetAdjustment import pandas.io.formats.html - if not hasattr(pandas.io.formats.html.HTMLFormatter, "_rdkitpatched"): - defHTMLFormatter_write_cell = pd.io.formats.html.HTMLFormatter._write_cell - pd.io.formats.html.HTMLFormatter._write_cell = _patched_HTMLFormatter_write_cell - pandas.io.formats.html.HTMLFormatter._rdkitpatched = True - get_adjustment_attr = getAdjustmentAttr() - if get_adjustment_attr: - defPandasGetAdjustment = getattr(pd.io.formats.format, get_adjustment_attr) - setattr(pd.io.formats.format, get_adjustment_attr, _patched_get_adjustment) - res = defPandasRepr(self, **kwargs) - if get_adjustment_attr: - setattr(pd.io.formats.format, get_adjustment_attr, defPandasGetAdjustment) - pd.io.formats.html.HTMLFormatter._write_cell = defHTMLFormatter_write_cell - return res + defHTMLFormatter_write_cell = pd.io.formats.html.HTMLFormatter._write_cell + pd.io.formats.html.HTMLFormatter._write_cell = _patched_HTMLFormatter_write_cell + try: + get_adjustment_attr = getAdjustmentAttr() + if get_adjustment_attr: + defPandasGetAdjustment = getattr(pd.io.formats.format, get_adjustment_attr) + setattr(pd.io.formats.format, get_adjustment_attr, _patched_get_adjustment) + res = defPandasRepr(self, **kwargs) + if get_adjustment_attr: + setattr(pd.io.formats.format, get_adjustment_attr, defPandasGetAdjustment) + return res + finally: + pd.io.formats.html.HTMLFormatter._write_cell = defHTMLFormatter_write_cell def patchPandasHTMLrepr(self, **kwargs):