Engineering

Engineering-specific data cleaning functions.

janitor.engineering.convert_units(df: pandas.core.frame.DataFrame, column_name: str = None, existing_units: str = None, to_units: str = None, dest_column_name: str = None) → pandas.core.frame.DataFrame[source]

Converts a column of numeric values from one unit to another.

Functional usage example:

import pandas as pd
import janitor.engineering

df = pd.DataFrame(...)

df = janitor.engineering.convert_units(
    df=df,
    column_name='temp_F',
    existing_units='degF',
    to_units='degC',
    dest_column_name='temp_C'
)

Method chaining usage example:

import pandas as pd
import janitor.engineering

df = pd.DataFrame(...)

df = df.convert_units(
    column_name='temp_F',
    existing_units='degF',
    to_units='degC',
    dest_column_name='temp_C'
)

Unit conversion can only take place if the existing_units and to_units are of the same type (e.g., temperature or pressure). The provided unit types can be any unit name or alternate name provided in the unyt package’s Listing of Units table: https://unyt.readthedocs.io/en/stable/unit_listing.html#unit-listing.

Volume units are not provided natively in unyt. However, exponents are supported, and therefore some volume units can be converted. For example, a volume in cubic centimeters can be converted to cubic meters using existing_units=’cm**3’ and to_units=’m**3’.

This method mutates the original DataFrame.

Parameters:
  • df – A pandas dataframe.
  • column_name – Name of the column containing numeric values that are to be converted from one set of units to another.
  • existing_units – The unit type to convert from.
  • to_units – The unit type to convert to.
  • dest_column_name – The name of the new column containing the converted values that will be created.
Returns:

A pandas DataFrame with a new column of unit-converted values.