Loading src/common/mixins/pandas.py +19 −6 Original line number Diff line number Diff line Loading @@ -26,12 +26,25 @@ class PandasMixin(): self.logger.error('Cursor is not open, please create one.') # TODO wrap in a try except block self.logger.debug(f'Returning {query} as pandas dataframe.') data_fetch_query = self.cursor.execute(query) if data_fetch_query is None: # FIXME better way to handle this? self.logger.error(f"Unable to fetch {query}.") # FIXME raise an Exception here? dataframe = pd.DataFrame(data_fetch_query.fetchall()) dataframe.columns = data_fetch_query.keys() self.cursor.execute(query) # https://www.psycopg.org/docs/cursor.html # Comments on the execute method: # The method returns None. If a query was executed, # the returned values can be retrieved using fetch*() methods. result = self.cursor.fetchall() if result is None: self.logger.error("Query returned: 'None' for query " f"'{query}'") else: self.logger.info(f"Query returned: '{result}' for query " f"'{query}'") columns = [i[0] for i in self.cursor.description] dataframe = pd.DataFrame.from_records(result, columns=columns) # dataframe.columns = result.keys() self.logger.info(f"Fetched Dataframe: {dataframe}") return dataframe if query is not None and not isinstance(self.connection, Loading Loading
src/common/mixins/pandas.py +19 −6 Original line number Diff line number Diff line Loading @@ -26,12 +26,25 @@ class PandasMixin(): self.logger.error('Cursor is not open, please create one.') # TODO wrap in a try except block self.logger.debug(f'Returning {query} as pandas dataframe.') data_fetch_query = self.cursor.execute(query) if data_fetch_query is None: # FIXME better way to handle this? self.logger.error(f"Unable to fetch {query}.") # FIXME raise an Exception here? dataframe = pd.DataFrame(data_fetch_query.fetchall()) dataframe.columns = data_fetch_query.keys() self.cursor.execute(query) # https://www.psycopg.org/docs/cursor.html # Comments on the execute method: # The method returns None. If a query was executed, # the returned values can be retrieved using fetch*() methods. result = self.cursor.fetchall() if result is None: self.logger.error("Query returned: 'None' for query " f"'{query}'") else: self.logger.info(f"Query returned: '{result}' for query " f"'{query}'") columns = [i[0] for i in self.cursor.description] dataframe = pd.DataFrame.from_records(result, columns=columns) # dataframe.columns = result.keys() self.logger.info(f"Fetched Dataframe: {dataframe}") return dataframe if query is not None and not isinstance(self.connection, Loading