Morph

The Morph class defines the restructuring actions performed on a single table in a Method. BaseMorph is the main definition for an action and is inherited by child classes in the /morph folder.

class core.BaseMorph

Custom Morphs inherit from this base class which describes the core functions and methodology for a Morph.

Morphs should redefine name, id, structure, and parameters, as well as produce a transform function. Everything else will probably remain as defined, but particularly complex Morphs should modify as required.

parameters can be an empty dict, but a morph may be defined by these parameters:

  • rows: the specific rows effected by the morph, a list of int
  • columns: the specific columns effected by the morph, a list of string

A morph defined as JSON is, e.g.:

{
    "name": "xxxxx",
    "parameters": {
        "rows": [1,2,3],
        "columns": ["column1"]
    }
}

In markup, the user codes:

morph = ["MORPH_NAME", [rows], [columns]]
method.set_morph(morph)

Where the presence and order of the lists is set by structure.

settings

Returns the dict representation of the Morph.

Raises:NameError if parameters don’t yet exist.
Returns:Dict representation of a Morph.
Return type:dict
validates(df=Empty DataFrame Columns: [] Index: [], rows=None, columns=None, column_names=None)

Traverses a list defined by *structure, ensuring that the terms conform to that morph’s default structural requirements.

The format for defining a structure is as follows:

[morph, [rows], [columns], [column_names]]

e.g.:

["RENAME", ["column_1", "column_2"]]

A calling function would specify something like:

Morph.validates(df, **dict((zip(['rows','columns','column_names'], structure[1:])))
Parameters:
  • rows (int or list of int) –
  • columns (str or list of str) –
  • column_names (str or list of str) –
  • df (dataframe) – The source DataFrame.
Returns:

True if valid

Return type:

bool

transform(df=Empty DataFrame Columns: [] Index: [], **kwargs)

Perform a transformation. This function must be overridden by child Morphs and describe a unique new method.

Warning

Assumes validates has been run.

Parameters:
  • df (DataFrame) – Working data to be transformed
  • **kwargs – Other fields which may be required in custom transforms
Returns:

Containing the implementation of the Morph

Return type:

Dataframe