API

class omilayers.Omilayers(db: str, config: dict = {'threads': 1}, read_only: bool = False, engine: str = 'duckdb')

Bases: object

config_settings() DataFrame

Print duckdb config settings

run(query: str, fetchdf=False) DataFrame | None

Execute a SQL query.

Parameters:
  • query (str) – Query to execute.

  • fetchdf (bool) – Pass True in cases the query fetches data.

Returns:

  • pandas.DataFrame – A pandas dataframe if query fetches data like a ‘SELECT’ query.

  • None – Nothing if query does not fetch data like a ‘UPDATE’ query.

Examples

omi.run(“SELECT * from tables_info”, fetchdf=True)

class omilayers.core.Stack(db: str, config: str, read_only: bool, dbutilsClass)

Bases: object

drop(layer: str) None

Delete layer.

Parameters:

layer (str) – The name of the layer to delete.

from_csv(layer: str, filename: str, chunksize: int | None = None, *args, **kwargs) None

Create layer from a csv file. For large csv files, set chunksize to the number of rows that will be read each time from the file.

Parameters:
  • layer (str) – The name of the layer to be created.

  • filename (str) – The input csv file.

  • chunksize (int, None) – The number of rows that will be read each time from the file. If None, the whole csv file will be read.

  • *args, **kwargs (arguments and keywords as defined by pandas.read_csv)

rename(layer: str, new_name: str) None

Rename layer.

Parameters:
  • layer (str) – Current name of layer.

  • new_name (str) – The new name of the layer.

search(term: str) None

Search for layers based on a given term. Useful in situations where there are many layers and there is doubt which table holds a given information or data. The term is searched across layer names, layer columns and layer descriptions.

Parameters:

term (str) – Term to be search across layer names, layer columns and layer descriptions.

Return type:

Prints the names of the layers that matched the searched term.

class omilayers.core.Layer(name: str, data: DataFrame | None, dbutilsClass)

Bases: object

property columns: List

Get the columns of the layer.

drop(col: str | None = None, values: None | str | int | float | List = None) None

Delete column or rows from layer.

Parameters:
  • col (str, None) – Name of column in layer.

  • values (str, int, float, list, None) – if None, whole column will be deleted. Otherwise, rows that match values in column will be deleted. If column is None, values correspond to rowids.

property exists: bool

Check layer exists.

property info: str | List

Get the description of the layer.

insert(data: Dict | DataFrame, ordered: bool = False) None

Insert new rows of data to an existing layer.

Parameters:
  • data (pandas.DataFrame, dict) – Pass a pandas.DataFrame object. The rows of the pandas.DataFrame will be inserted as new layer rows. Alternatively, pass a dictionary with keys the names of the columns of the layer and values the data to be inserted as rows.

  • ordered (bool) – Pass True only in case data is string, and the order of the columns in the referred pandas.DataFrame matches the order of the layer’s columns.

query(condition: str, cols: str | List = '*') DataFrame

Select one or more columns from layer given condition.

Parameters:
  • cols (str, list) – One or more columns to be selected from layer. If col=’*’ all columns will be selected.

  • condition (str) – The condition to be matched during selection. For instance, when a given column has a given value.

Return type:

A pandas.DataFrame with the selected columns and the filtered rows.

rename(col: str, new_name: str) None

Rename a column in layer.

Parameters:
  • col (str) – Current name of column in layer.

  • new_name (str) – New name of column.

select(cols: str | List, where: str, values: str | int | float | slice | ndarray | List, exclude: str | List | None = None) DataFrame

Select columns from layer where a reference column has rows with certain values.

Parameters:
  • cols (str, list) – The columns to select from layer. If cols=’*’ all columns are selected.

  • where (str) – The name of the reference column in the layer.

  • values (str, int, float, np.ndarray, list) – The values the reference column to be used during row selection.

  • exclude (str, list) – Useful in cases where large number of columns need to selected except few ones.

Return type:

A pandas.DataFrame with the selected columns and the filtered rows.

set_data(data: DataFrame) None

Change the data the layer currently holds.

Parameters:

data (pandas.DataFrame) – A pandas.DataFrame object.

set_info(value: str) None

Change the description of the layer.

set_tag(value: str) None

Change the assigned tag of the layer.

property tag: str | List

Get the assigned tag of the layer.

to_df(index: str | None = None) DataFrame

Load layer as pandas.DataFrame.

Parameters:

index (str, None) – The column to be used as pandas.DataFrame index.

to_json(key_col: str, value_col: str) dict

Create dictionary using two columns of layer

Parameters:
  • key_col (str) – Layer’s column whose values are going to be used as dictionary keys.

  • value_col (str) – Layer’s column whose values are going to be used as dictionary values.

Return type:

Python dictionary.