Datetime transformer
DateColumnsTransformer
¶
Bases: BaseTransformer
Splits a date column into multiple columns.
Example:
import pandas as pd
from sk_transformers import DateColumnsTransformer
X = pd.DataFrame({"foo": ["2021-01-01", "2022-02-02", "2023-03-03"]})
transformer = DateColumnsTransformer(["foo"])
transformer.fit_transform(X)
foo foo_year ... foo_is_year_end foo_is_weekend
0 2021-01-01 2021 ... False False
1 2022-02-02 2022 ... False False
2 2023-03-03 2023 ... False False
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str]
|
List of columns to transform. |
required |
date_format |
str
|
Date format. Defaults to |
'%Y-%m-%d'
|
errors |
str
|
How to handle errors in |
'raise'
|
date_elements |
[List[str]]
|
List of date elements to extract. |
['year', 'month', 'day', 'day_of_week', 'day_of_year', 'week_of_year', 'quarter', 'is_leap_year', 'is_month_start', 'is_month_end', 'is_quarter_start', 'is_quarter_end', 'is_year_start', 'is_year_end', 'is_weekend']
|
Source code in src/sk_transformers/datetime_transformer.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
transform(X)
¶
Transforms columns from the provided dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
Dataframe with columns to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Dataframe with transformed columns. |
Source code in src/sk_transformers/datetime_transformer.py
DurationCalculatorTransformer
¶
Bases: BaseTransformer
Calculates the duration between to given dates.
Example:
import pandas as pd
from sk_transformers import DurationCalculatorTransformer
X = pd.DataFrame(
{
"foo": ["1960-01-01", "1970-01-01", "1990-01-01"],
"bar": ["1960-01-01", "1971-01-01", "1988-01-01"],
}
)
transformer = DurationCalculatorTransformer(("foo", "bar"), "days", "foo_bar_duration")
transformer.fit_transform(X)
foo bar foo_bar_duration
0 1960-01-01 1960-01-01 0
1 1970-01-01 1971-01-01 365
2 1990-01-01 1988-01-01 -731
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
Tuple[str, str]
|
The two columns that contain the dates which should be used to calculate the duration. |
required |
unit |
str
|
The unit in which the duration should be returned. Should be either |
required |
new_column_name |
str
|
The name of the output column. |
required |
Source code in src/sk_transformers/datetime_transformer.py
transform(X)
¶
Transform method that calculates the duration between two dates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
The input DataFrame. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: The transformed DataFrame. |
Source code in src/sk_transformers/datetime_transformer.py
TimestampTransformer
¶
Bases: BaseTransformer
Transforms a date column with a specified format into a timestamp column.
Example:
import pandas as pd
from sk_transformers import TimestampTransformer
X = pd.DataFrame({"foo": ["1960-01-01", "1970-01-01", "1990-01-01"]})
transformer = TimestampTransformer(["foo"])
transformer.fit_transform(X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str]
|
List of features which should be transformed. |
required |
date_format |
str
|
Format of the date column. Defaults to "%Y-%m-%d". |
'%Y-%m-%d'
|
Source code in src/sk_transformers/datetime_transformer.py
transform(X)
¶
Transforms columns from the provided dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
Dataframe with columns to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Dataframe with transformed columns. |