DurationToFloat#
- class skrub.DurationToFloat[source]#
Convert duration columns to seconds.
A note on using single column transformations
DurationToFloatis a type of single column transformation . Unlike most scikit-learn estimators, itsfit,transformandfit_transformmethods 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 aApplyToColsor aTableVectorizer.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
durationdtype 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 withDurationdtype 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
transformmethod.fit_transform
transform
- fit(column, y=None, **kwargs)[source]#
Fit the transformer.
This default implementation simply calls
fit_transform()and returnsself.Subclasses should implement
fit_transformandtransform.- 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().
- columna pandas or polars
- 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.
- input_featuresarray_like of
- Returns:
- 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
TransformerMixinbefore SingleColumnTransformer).
- 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:
- **params
dict Estimator parameters.
- **params
- Returns:
- selfestimator instance
Estimator instance.
- set_transform_request(*, col='$UNCHANGED$')[source]#
Configure whether metadata should be requested to be passed to the
transformmethod.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(seesklearn.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 totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.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.