Datetime rules
Datetime rules¶
Rules operating on datetime columns.
Importing the rules
The rules are implemented by each backend. Import them from the package of the backend you use, e.g.
from etlrules.backends.pandas import DateTimeRoundRule (or etlrules.backends.polars, etlrules.backends.dask, etlrules.backends.pyarrow).
The arguments and the behaviour are the same in all the backends, unless stated otherwise.
DateTimeRoundRule ¶
Rounds a set of datetime columns to the specified granularity (day, hour, minute, etc.).
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The column name to round according to the unit specified. |
required |
unit
|
str
|
Specifies the unit of rounding. That is: rounding to the nearest day, hour, minute, etc. The supported units are: day: anything up to 12:00:00 rounds down to the current day, after that up to the next day hour: anything up to 30th minute rounds down to the current hour, after that up to the next hour minute: anything up to 30th second rounds down to the current minute, after that up to the next minute second: rounds to the nearest second (if the column has milliseconds) millisecond: rounds to the nearest millisecond (if the column has microseconds) microsecond: rounds to the nearest microsecond nanosecond: rounds to the nearest nanosecond |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, overwriting existing columns is ignored.
DateTimeRoundDownRule ¶
Rounds down (truncates) a set of datetime columns to the specified granularity (day, hour, minute, etc.).
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The column name to round according to the unit specified. |
required |
unit
|
str
|
Specifies the unit of rounding. That is: rounding to the nearest day, hour, minute, etc. The supported units are: day: removes the hours/minutes/etc. hour: removes the minutes/seconds etc. minute: removes the seconds/etc. second: removes the milliseconds/etc. millisecond: removes the microseconds microsecond: removes nanoseconds (if any) |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, overwriting existing columns is ignored.
DateTimeRoundUpRule ¶
Rounds up a set of datetime columns to the specified granularity (day, hour, minute, etc.).
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The column name to round according to the unit specified. |
required |
unit
|
str
|
Specifies the unit of rounding. That is: rounding to the nearest day, hour, minute, etc. The supported units are: day: Rounds up to the next day if there are any hours/minutes/etc. hour: Rounds up to the next hour if there are any minutes/etc. minute: Rounds up to the next minute if there are any seconds/etc. second: Rounds up to the next second if there are any milliseconds/etc. millisecond: Rounds up to the next millisecond if there are any microseconds microsecond: Rounds up to the next microsecond if there are any nanoseconds |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, overwriting existing columns is ignored.
DateTimeExtractComponentRule ¶
Extract an individual component of a date/time (e.g. year, month, day, hour, etc.).
Basic usage:
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
A datetime column to extract the given component from. |
required |
component
|
str
|
The component of the datatime to extract from the datetime. When the component is one of (year, month, day, hour, minute, second, microsecond) then the extracted component will be an integer with the respective component of the datetime. When component is weekday, the component will be an integer with the values 0-6, with Monday being 0 and Sunday 6. When the component is weekday_name or month_name, the result column will be a string column with the names of the weekdays (e.g. Monday, Tuesday, etc.) or month names respectively (e.g. January, February, etc.). The names will be printed in the language specified in the locale parameter (or English as the default). |
required |
locale
|
Optional[str]
|
An optional locale string applicable to weekday_name and month_name. When specified,
the names will use the given locale to print the names in the given language.
Default: en_US.utf8 will print the names in English.
Use the command |
required |
output_column
|
Optional[str]
|
An optional column name to contain the result. If provided, if must have the same length as the columns sequence. The existing columns are unchanged, and new columns are created with the component extracted. If not provided, the result is updated in place. |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
ValueError
|
raised if a locale is specified which is not supported or available on the machine running the scripts. |
Note
In non-strict mode, overwriting existing columns is ignored.
DateTimeAddRule ¶
Adds a number of units (days, hours, minutes, etc.) to a datetime column.
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The name of a datetime column to add to. |
required |
unit_value
|
Union[int, float, str]
|
The number of units to add to the datetime column. The unit_value can be negative, in which case this rule performs a substract. A name of an existing column can be passed into unit_value, in which case, that column will be added to the input_column. If the column is a timedelta, it will be added as is, if it's a numeric column, then it will be interpreted based on the unit parameter (e.g. years/days/hours/etc.). In this case, if the column specified in the unit_value doesn't exist, MissingColumnError is raised. |
required |
unit
|
str
|
Specifies what unit the unit_value is in. Supported values are: years, months, weeks, weekdays, days, hours, minutes, seconds, microseconds, nanoseconds. weekdays skips weekends (ie Saturdays and Sundays). |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column doesn't exist in the input dataframe. |
MissingColumnError
|
raised if unit_value is a name of a column but it doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
ValueError
|
raised if unit_value is a column which is not a timedelta column and the unit parameter is not specified. |
Note
In non-strict mode, missing columns or overwriting existing columns are ignored.
DateTimeSubstractRule ¶
Substracts a number of units (days, hours, minutes, etc.) from a datetime column.
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The name of a datetime column to add to. |
required |
unit_value
|
Union[int, float, str]
|
The number of units to add to the datetime column. The unit_value can be negative, in which case this rule performs an addition. A name of an existing column can be passed into unit_value, in which case, that column will be substracted from the input_column. If the column is a timedelta, it will be substracted as is, if it's a numeric column, then it will be interpreted based on the unit parameter (e.g. days/hours/etc.). In this case, if the column specified in the unit_value doesn't exist, MissingColumnError is raised. |
required |
unit
|
str
|
Specifies what unit the unit_value is in. Supported values are: days, hours, minutes, seconds, microseconds, nanoseconds. |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input_column doesn't exist in the input dataframe. |
MissingColumnError
|
raised if unit_value is a name of a column but it doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, missing columns or overwriting existing columns are ignored.
DateTimeDiffRule ¶
Calculates the difference between two datetime columns, optionally extracting it in the specified unit.
Basic usage:
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The name of a datetime column. |
required |
input_column2
|
str
|
The name of the second datetime column. The result will be input_column - input_column2 |
required |
unit
|
Optional[str]
|
If specified, it will extract the given component of the difference: years, months, days, hours, minutes, seconds, microseconds, nanoseconds. |
required |
output_column
|
Optional[str]
|
The name of a new column with the result. Optional. If not provided, the result is updated in place. In strict mode, if provided, the output_column must not exist in the input dataframe. In non-strict mode, if provided, the output_column with overwrite a column with the same name in the input dataframe (if any). |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if either input_column or input_column2 don't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
For best results, round the datetime columns using one of the rounding rules before calculating the difference. Otherwise, this rule will tend to truncate/round down. For example: 2023-05-05 10:00:00 - 2023-05-04 10:00:01 will result in 0 days even though the difference is 23:59:59. In cases like this one, it might be preferable to round, in this case perhaps round to "day" using DateTimeRoundRule or DateTimeRoundDownRule. This will result in a 2023-05-05 00:00:00 - 2023-05-04 00:00:00 which results in 1 day.
DateTimeUTCNowRule ¶
Adds a new column with the UTC date/time.
Basic usage:
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_column
|
str
|
The name of the column to be added to the dataframe. This column will be populated with the UTC date/time at the time of the call. The same value will be populated for all rows. The date/time populated is a "naive" datetime ie: doesn't have a timezone information. |
required |
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the input dataframe. |
Note
In non-strict mode, if the output_column exists in the input dataframe, it will be overwritten.
DateTimeLocalNowRule ¶
Adds a new column with the local date/time.
Basic usage:
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_column
|
str
|
The name of the column to be added to the dataframe. This column will be populated with the local date/time at the time of the call. The same value will be populated for all rows. The date/time populated is a "naive" datetime ie: doesn't have a timezone information. |
required |
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the input dataframe. |
Note
In non-strict mode, if the output_column exists in the input dataframe, it will be overwritten.
DateTimeToStrFormatRule ¶
Formats a datetime column to a string representation according to a specified format.
Basic usage:
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The datetime column with the values to format to string. |
required |
format
|
str
|
The format used to display the date/time. E.g. %Y-%m-%d For the directives accepted in the format, have a look at: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior |
required |
output_column
|
Optional[str]
|
An optional column to hold the formatted results. If provided, the existing column is unchanged, and a new column with this new is created. If not provided, the result is updated in place. |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter valiation. Default: True |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
Note
In non-strict mode, overwriting existing columns is ignored.
StrToDateTimeRule ¶
Parses a string column to datetime values according to a specified format.
This is the reverse of DateTimeToStrFormatRule.
Basic usage:
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_column
|
str
|
The string column with the values to parse. |
required |
format
|
str
|
The format of the date/time values in the input column. E.g. %Y-%m-%d %H:%M:%S For the directives accepted in the format, have a look at: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior |
required |
output_column
|
Optional[str]
|
An optional column to hold the parsed datetime values. If provided, the existing column is unchanged, and a new column with this name is created. If not provided, the result is updated in place. |
None
|
named_input
|
Optional[str]
|
Which dataframe to use as the input. Optional. When not set, the input is taken from the main output. Set it to a string value, the name of an output dataframe of a previous rule. |
None
|
named_output
|
Optional[str]
|
Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. |
None
|
name
|
Optional[str]
|
Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. |
None
|
description
|
Optional[str]
|
Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. |
None
|
strict
|
bool
|
When set to True, the rule does a stricter validation. Default: True In strict mode, a value which doesn't match the format raises a ValueError. In non-strict mode, the values which don't match the format become NA. |
True
|
Raises:
| Type | Description |
|---|---|
MissingColumnError
|
raised if the input column doesn't exist in the input dataframe. |
ColumnAlreadyExistsError
|
raised in strict mode only if the output_column already exists in the dataframe. |
ValueError
|
raised in strict mode if a value doesn't match the format. With the dask backend, which evaluates lazily, the error is raised when the result is computed. |
Note
In non-strict mode, overwriting existing columns is ignored.