chemaxon.pandasutil

This package is a connector between the widely used pandas library and chemaxon functionalities.

GitHub examples: https://github.com/ChemAxon/python-examples/blob/main/jupyter/07_pandas_integration_examples.ipynb

@typecheck(Molecule)
def mol_to_svg_formatter(mol: chemaxon.Molecule) -> str:

Converts a Molecule object to its SVG representation.

Parameters
Returns

str - SVG representation of the molecule.

@typecheck(str, str, str, (str, None), bool)
def load_molecules_for_pandas( file_path: str, mol_format: str = '', mol_obj_column: str = 'Molecule', mol_str_column: str = None, read_properties_to_columns: bool = False) -> dict[str, chemaxon.Molecule | str]:

Loads molecules from a file and prepares them for use in a pandas.DataFrame.

Parameters
  • file_path: The file containing the molecules
  • mol_format: The format of the input file. If not set, then it is auto recognized
  • mol_obj_column: The string label of the column containing the chemaxon.Molecule object inside the DataFrame
  • mol_str_column: The label of the column containing the short string representation (smiles or cxsmiles) of the chemaxon.Molecule object
  • read_properties_to_columns: If True, the properties of the chemaxon.Molecule objects will be added as separate columns to the pandas.DataFrame
Returns

A python dict object filled with chemaxon.Molecule data, that can be passed to a pandas.DataFrame constructor. The keys are the labels of the columns.

@typecheck(str, str, int, str, (str, None), bool)
def load_molecules_for_pandas_batches( file_path: str, mol_format: str = '', batch_size: int = 1000, mol_obj_column: str = 'Molecule', mol_str_column: str = None, read_properties_to_columns: bool = False) -> Generator[dict, Any, NoneType]:

Generator that yields dictionary inputs for pandas.DataFrame in batches. Avoids loading entire file into memory. Useful for files with millions of molecules.

Parameters
  • file_path: The file containing the molecules
  • mol_format: The format of the input file. If not set, then it is auto recognized
  • batch_size: Size of each chemaxon.Molecule batches
  • mol_obj_column: The string label of the column containing the chemaxon.Molecule object inside the DataFrame
  • mol_str_column: The label of the column containing the CXSMILES representation of the chemaxon.Molecule object
  • read_properties_to_columns: If True, the properties of the chemaxon.Molecule objects will be added as separate columns to the DataFrame
Returns

A python Generator[dict] object filled with chemaxon.Molecule data, that can be passed to a pandas.DataFrame constructor. The keys are the labels of the columns.

Examples

Build one DataFrame from all batches (convenient, but fully materializes in memory):

import pandas as pd
import chemaxon as cxn

batch_iter = cxn.pandasutil.load_molecules_for_pandas_batches(
    "./resources/nci_random_992.smiles",
    batch_size=200,
    molecule_column="Mol",
    molecule_str_column="cxsmiles"
)

df = pd.concat((pd.DataFrame(batch) for batch in batch_iter), ignore_index=True)
print(df.shape)

Process batches incrementally (memory-friendly):

import pandas as pd
import chemaxon as cxn

for i, batch in enumerate(
    cxn.pandasutil.load_molecules_for_pandas_batches("./resources/nci_random_992.smiles", batch_size=200)
):
    df_batch = pd.DataFrame(batch)
    print(f"batch={i}, rows={len(df_batch)}")
    # process df_batch, then release it
@typecheck(list, str, (str, None), bool)
def prepare_molecules_for_pandas( mol_list: list[chemaxon.Molecule], mol_obj_column: str = 'Molecule', mol_str_column: str = None, read_properties_to_columns: bool = False) -> dict[str, chemaxon.Molecule | str]:

Prepares a dict object to be loaded into a pandas.DataFrame from a list of chemaxon.Molecule objects.

Note: In case, properties are being added as separate columns to the returned dictionary, all the property keys are collected from all the molecules. If a molecule does not have a specific property, then None is added to the corresponding column.

Parameters
  • mol_list: list of chemaxon.Molecule objects
  • mol_obj_column: The string label of the column containing the chemaxon.Molecule object inside the DataFrame
  • mol_str_column: The label of the column containing the str (as CXSMILES) representation of the chemaxon.Molecule object
  • read_properties_to_columns: If True, the properties of the chemaxon.Molecule objects will be added as separate columns to the DataFrame
Returns

A python dict object filled with chemaxon.Molecule data, that can be passed to a pandas.DataFrame constructor. The keys are the labels of the columns.