From 3719b4ff4f9fd5885ffe3acb80eb77f8ab50b587 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 22 Nov 2019 17:56:38 -0800 Subject: [PATCH] CLN: avoid catching Exception in io.pytables --- pandas/io/pytables.py | 52 ++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8afbd293a095b..ee3a01c54eace 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1005,6 +1005,8 @@ def remove(self, key: str, where=None, start=None, stop=None): # the key is not a valid store, re-raising KeyError raise except Exception: + # In tests we get here with ClosedFileError, TypeError, and + # _table_mod.NoSuchNodeError. TODO: Catch only these? if where is not None: raise ValueError( @@ -1825,8 +1827,7 @@ def convert( # making an Index instance could throw a number of different errors try: self.values = Index(values, **kwargs) - except Exception: - + except ValueError: # if the output freq is different that what we recorded, # it should be None (see also 'doc example part 2') if "freq" in kwargs: @@ -4242,38 +4243,29 @@ def write_data_chunk(self, rows, indexes, mask, values): if not np.prod(v.shape): return - try: - nrows = indexes[0].shape[0] - if nrows != len(rows): - rows = np.empty(nrows, dtype=self.dtype) - names = self.dtype.names - nindexes = len(indexes) - - # indexes - for i, idx in enumerate(indexes): - rows[names[i]] = idx + nrows = indexes[0].shape[0] + if nrows != len(rows): + rows = np.empty(nrows, dtype=self.dtype) + names = self.dtype.names + nindexes = len(indexes) - # values - for i, v in enumerate(values): - rows[names[i + nindexes]] = v + # indexes + for i, idx in enumerate(indexes): + rows[names[i]] = idx - # mask - if mask is not None: - m = ~mask.ravel().astype(bool, copy=False) - if not m.all(): - rows = rows[m] + # values + for i, v in enumerate(values): + rows[names[i + nindexes]] = v - except Exception as detail: - raise Exception("cannot create row-data -> {detail}".format(detail=detail)) + # mask + if mask is not None: + m = ~mask.ravel().astype(bool, copy=False) + if not m.all(): + rows = rows[m] - try: - if len(rows): - self.table.append(rows) - self.table.flush() - except Exception as detail: - raise TypeError( - "tables cannot write this data -> {detail}".format(detail=detail) - ) + if len(rows): + self.table.append(rows) + self.table.flush() def delete( self,