datamop

Submodules

Attributes

__version__

Functions

column_encoder(data, columns[, method, order])

Encodes categorical columns using one-hot or ordinal encoding based on user input.

column_scaler(data, column[, method, new_min, ...])

Scales the values of a specified column in a DataFrame.

sweep_nulls(data[, strategy, columns, fill_value])

Handles missing values in a dataset using the specified strategy.

Package Contents

datamop.__version__
datamop.column_encoder(data, columns, method='one-hot', order=None)[source]

Encodes categorical columns using one-hot or ordinal encoding based on user input.

Parameters:

datapandas.DataFrame

The input DataFrame containing the dataset.

columnslist

The name of the columns to be encoded.

methodstr, optional, default=’one-hot’

The encoding method to use. Accepts either ‘one-hot’ for one-hot encoding or ‘ordinal’ for ordinal encoding.

orderdict, optional, default=None

A dictionary specifying the custom order for ordinal encoding. The keys should be column names, and the values should be lists defining the order of categories for each column.

Returns:

pd.DataFrame

A new DataFrame with the specified column encoded. The original column will be dropped.

Raises:

TypeError:

If input types are incorrect (e.g., non-DataFrame input, columns not a list of strings, method not a string, or order not a dictionary).

ValueError:

If required parameters are missing or invalid values are provided.

KeyError:

If specified columns are not found in the input DataFrame.

UserWarning:

If a column contains only one unique value or if there are missing values.

Examples:

>>> import pandas as pd
>>> data = pd.DataFrame({
...     'Sport': ['Tennis', 'Basketball', 'Football', 'Badminton'],
...     'Level': ['A', 'B', 'C', 'D']
... })
>>> encoded_df_onehot = column_encoder(data, columns=['Sport'], method='one-hot')
>>> print(encoded_df_onehot)
  Level  Sport_Badminton  Sport_Basketball  Sport_Football  Sport_Tennis
     A                0                 0               0             1
     B                0                 1               0             0
     C                0                 0               1             0
     D                1                 0               0             0
>>> encoded_df_ordinal = column_encoder(data, columns=['Level'], method='ordinal', order={'Level': ['A', 'B', 'C', 'D']})
>>> print(encoded_df_ordinal)
        Sport  Level
       Tennis      0
    Basketball     1
     Football      2
    Badminton      3
datamop.column_scaler(data, column, method='minmax', new_min=0, new_max=1, inplace=True)[source]

Scales the values of a specified column in a DataFrame.

Parameters:
  • data (pandas.DataFrame) – The DataFrame containing the column of interest for scaling.

  • column (str) – The name of the numeric column to scale.

  • method (str) –

    The method used for scaling. Options include:
    • minmax: Scales values between new_min and new_max, used as default method.

    • standard: Scales values with mean of 0 and standard deviation of 1.

  • new_min (float) – The lower boundary value for min-max scaling. Default value is 0.

  • new_max (float) – The upper boundary value for min-max scaling. Default value is 1.

  • inplace (bool) – If True the original column is replaced with new scaled values. If False the original column is retained and the new scaled column is added to the dataframe with title <column-name>-scaled. Default is True.

Returns:

A copy of the DataFrame with the scaled column replacing the original column if inplace is set to True. If inplace is set to False, the copy of DataFrame is returned with the new scaled column added, keeping the original column.

Return type:

pandas.DataFrame

Raises:
  • TypeError – If the input data is not a pandas DataFrame.

  • KeyError: – If the column passed for scaling does not exist in the DataFrame.

  • ValueError: – If the column passed for scaling is not numeric. If the method is not minmax or standard. If the new_min value is greater or equal to the new_max when using minmax method.

Examples

>>> import pandas as pd
>>> df = pd.DataFrame({"price": [25, 50, 75]})
>>> df_scaled = column_scaler(df, column = 'price', method='minmax', new_min=0, new_max=1)
>>> print(df_scaled)
        price
        0.0
        0.5
        1.0
datamop.sweep_nulls(data, strategy='mean', columns=None, fill_value=None)[source]

Handles missing values in a dataset using the specified strategy.

Parameters:
  • data (pandas.DataFrame) – The input dataset where missing values need to be handled.

  • strategy ({'mean', 'median', 'mode', 'constant', 'drop'}, optional, default='mean') – The strategy to use for handling missing values. Supported options are: - ‘mean’: For numeric columns only. Replace missing values with the mean of the respective column. - ‘median’: For numeric columns only. Replace missing values with the median of the respective column. - ‘mode’: Replace missing values with the mode (most frequent value) of the respective column. - ‘constant’: Replace missing values with a specified constant value (requires fill_value). - ‘drop’: Drop rows or columns containing missing values (depending on the columns parameter).

  • columns (list of str or None, optional, default=None) – The specific columns to apply the missing value handling. If None or an empty list, the strategy is applied to all columns.

  • fill_value (int, float, str, or None, optional, default=None) – The constant value to use when strategy=’constant’. Ignored for other strategies.

Returns:

A new DataFrame with missing values handled based on the specified strategy.

Return type:

pandas.DataFrame

Raises:
  • ValueError

    • If the input data is not a pandas.DataFrame.

    • If the input strategy is not in ‘mean’, ‘median’, ‘mode’, ‘constant’, or ‘drop’.

    • If fill_value is missing for the ‘constant’ strategy.

  • KeyError – If any specified column in columns does not exist in the pandas.DataFrame.

  • TypeError – If the input of fill_value is not a number or a string.

Examples

a b c

0 10.0 1.5 x 1 NaN 2.5 None 2 30.0 NaN z

>>> cleaned = sweep_nulls(data, strategy='mean')
>>> print(cleaned)
        a    b     c
    0  10.0  1.5     x
    1  20.0  2.5  None
    2  30.0  2.0     z