DurationToFloat#

class skrub.DurationToFloat[source]#

Convert duration columns to seconds.

A note on using single column transformations

DurationToFloat is a type of single column transformation . Unlike most scikit-learn estimators, its fit, transform and fit_transform methods expect a single column (e.g. Series) not a full dataframe. To apply this transformer to one or more columns in a dataframe, use it in a ApplyToCols or a TableVectorizer.

To apply to all columns:

ApplyToCols(DurationToFloat())

To apply to selected columns:

ApplyToCols(DurationToFloat(), cols=['col_name_1', 'col_name_2'])

This transformer converts duration columns to seconds. It only works on columns with duration dtype. Other dtypes are rejected.

Examples

>>> import pandas as pd
>>> from datetime import timedelta
>>> from skrub import DurationToFloat
>>> s = pd.Series([
...     timedelta(seconds=3600), timedelta(minutes=2), timedelta(days=1)
... ])
>>> s
0   0 days 01:00:00
1   0 days 00:02:00
2   1 days 00:00:00
dtype: timedelta64[...]
>>> converter = DurationToFloat()
>>> converter.fit_transform(s)
0    3600.0
1    120.0
2    86400.0
dtype: float32

Columns that do not have duration dtype are rejected:

>>> s = pd.Series(['1 day', '2 days', '3 days'], name='s')
>>> converter.fit_transform(s)
Traceback (most recent call last):
    ...
skrub.core.RejectColumn: Expected a duration column, got...

For polars, only columns with Duration dtype are modified:

>>> import pytest
>>> pl = pytest.importorskip('polars')
>>> s = pl.Series([
...     timedelta(seconds=3600), timedelta(minutes=2), timedelta(days=1)
... ])
>>> s
shape: (3,)
Series: '' [duration[μs]]
[
    1h
    2m
    1d
]
>>> converter.fit_transform(s)
shape: (3,)
Series: '' [f32]
[
    3600.0
    120.0
    86400.0
]
>>> s = pl.Series('s', ['1 day', '2 days', '3 days'], dtype=pl.String)
>>> converter.fit_transform(s)
Traceback (most recent call last):
    ...
skrub.core.RejectColumn: Expected a duration column, got String

Methods

fit(column[, y])

Fit the transformer.

get_feature_names_out([input_features])

Get the output feature names.

get_params([deep])

Get parameters for this estimator.

set_output(*[, transform])

Default no-op implementation for set_output.

set_params(**params)

Set the parameters of this estimator.

set_transform_request(*[, col])

Configure whether metadata should be requested to be passed to the transform method.

fit_transform

transform

fit(column, y=None, **kwargs)[source]#

Fit the transformer.

This default implementation simply calls fit_transform() and returns self.

Subclasses should implement fit_transform and transform.

Parameters:
columna pandas or polars Series

Unlike most scikit-learn transformers, single-column transformers transform a single column, not a whole dataframe.

ycolumn or dataframe

Prediction targets.

**kwargs

Extra named arguments are passed to self.fit_transform().

Returns:
self

The fitted transformer.

get_feature_names_out(input_features=None)[source]#

Get the output feature names.

Parameters:
input_featuresarray_like of str, default=None

Input feature names. Ignored.

Returns:
list of str

The names of the output features.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_output(*, transform=None)[source]#

Default no-op implementation for set_output.

Skrub transformers already output dataframes of the correct type by default so there is usually no need for set_output to do anything.

Subclasses are of course free to redefine set_output (e.g. by inheriting from TransformerMixin before SingleColumnTransformer).

Parameters:
transformstr or None, default=None

Ignored.

Returns:
SingleColumnTransformer

Returns self.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_transform_request(*, col='$UNCHANGED$')[source]#

Configure whether metadata should be requested to be passed to the transform method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
colstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for col parameter in transform.

Returns:
selfobject

The updated object.