def query(self, query: peewee.ModelSelect) -> dict: """Custom query, returns a generator that will return a dictionary (corresponding to a row). Parameters: query (peewee.ModelSelect) : Custom query. """ for res in query.dicts(): yield res
def query_to_pandas(self, query: peewee.ModelSelect, array_cols: list = None) -> pd.DataFrame: """Convert a model query to a pandas dataframe.""" df = pd.DataFrame(query.dicts()) if not array_cols: array_cols = self._array_types # Try to use the new data to infer what the format should be # Convert array columns to numpy arrays. Leave as string if not specified if array_cols: for key in array_cols: df[key] = df.apply(lambda x: np.array(ast.literal_eval(x[key]), dtype=x[f'{key}_dtype']), axis=1) df.drop(f'{key}_dtype', axis=1, inplace=True) return df
def _query_to_dataframe(cls, query: peewee.ModelSelect) -> pd.DataFrame: dict_query = {indice: row for indice, row in enumerate(query.dicts())} return pd.DataFrame.from_dict(dict_query, orient='index')