ts_shape.transform.time_functions.timezone_shift
Classes
| Name | Description |
|---|---|
TimezoneShift | A class for shifting timestamps in a DataFrame to a different timezone, with methods to handle timezone localization and conversion. |
TimezoneShift
Bases: Base
A class for shifting timestamps in a DataFrame to a different timezone, with methods to handle timezone localization and conversion.
Functions
| Name | Description |
|---|---|
shift_timezone | Shifts timestamps in the specified column of a DataFrame from a given timezone to a target timezone. |
add_timezone_column | Creates a new column with timestamps converted from an input timezone to a target timezone, without altering the original column. |
list_available_timezones | Returns a sorted list of all available timezones. |
detect_timezone_awareness | Detects if a time column in a DataFrame is timezone-aware. |
revert_to_original_timezone | Reverts a timezone-shifted time column back to the original timezone. |
calculate_time_difference | Calculates the time difference between two timestamp columns. |
shift_timezone
shift_timezone(
dataframe: pd.DataFrame,
time_column: str,
input_timezone: str,
target_timezone: str,
ambiguous: str = "raise",
nonexistent: str = "raise",
) -> pd.DataFrame
Shifts timestamps in the specified column of a DataFrame from a given timezone to a target timezone.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | The DataFrame containing the data. | required |
time_column | str | The name of the time column to convert. | required |
input_timezone | str | The timezone of the input timestamps (e.g., 'UTC' or 'America/New_York'). | required |
target_timezone | str | The target timezone to shift to (e.g., 'America/New_York'). | required |
ambiguous | str | How to localize naive wall-clock times that occur twice (DST fall-back, e.g. 2024-10-27 02:30 in Europe/Berlin). Passed to tz_localize: 'raise' (default), 'infer', 'NaT'. | 'raise' |
nonexistent | str | How to localize naive times that do not exist (DST spring-forward, e.g. 2024-03-31 02:30 in Europe/Berlin): 'raise' (default), 'shift_forward', 'shift_backward', 'NaT'. | 'raise' |
Returns
| Type | Description |
|---|---|
DataFrame | pd.DataFrame: A copy of the DataFrame with timestamps converted to |
DataFrame | the target timezone (the input frame is not modified). |
add_timezone_column
add_timezone_column(
dataframe: pd.DataFrame,
time_column: str,
input_timezone: str,
target_timezone: str,
) -> pd.DataFrame
Creates a new column with timestamps converted from an input timezone to a target timezone, without altering the original column.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | The DataFrame containing the data. | required |
time_column | str | The name of the time column to convert. | required |
input_timezone | str | The timezone of the input timestamps. | required |
target_timezone | str | The target timezone. | required |
Returns
| Type | Description |
|---|---|
DataFrame | pd.DataFrame: A DataFrame with an additional column for the shifted timezone. |
list_available_timezones
list_available_timezones() -> list[str]
Returns a sorted list of all available timezones.
Returns
| Name | Type | Description |
|---|---|---|
list | list[str] | A sorted list of strings representing all available timezones. |
detect_timezone_awareness
detect_timezone_awareness(dataframe: pd.DataFrame, time_column: str) -> bool
Detects if a time column in a DataFrame is timezone-aware.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | The DataFrame containing the data. | required |
time_column | str | The name of the time column to check. | required |
Returns
| Name | Type | Description |
|---|---|---|
bool | bool | True if the column is timezone-aware, False otherwise. |
revert_to_original_timezone
revert_to_original_timezone(
dataframe: pd.DataFrame, time_column: str, original_timezone: str
) -> pd.DataFrame
Reverts a timezone-shifted time column back to the original timezone.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | The DataFrame containing the data. | required |
time_column | str | The name of the time column to revert. | required |
original_timezone | str | The original timezone to revert to. | required |
Returns
| Type | Description |
|---|---|
DataFrame | pd.DataFrame: A DataFrame with timestamps reverted to the original timezone. |
Raises
| Type | Description |
|---|---|
ValueError | If time_column is timezone-naive (and holds a timestamp): there is no zone to convert from. Localize it first with :meth:shift_timezone. An all-missing naive column is taken as UTC. |
calculate_time_difference
calculate_time_difference(
dataframe: pd.DataFrame, start_column: str, end_column: str
) -> pd.Series
Calculates the time difference between two timestamp columns.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | The DataFrame containing the data. | required |
start_column | str | The name of the start time column. | required |
end_column | str | The name of the end time column. | required |
Returns
| Type | Description |
|---|---|
Series | pd.Series: A Series with the time differences in seconds. |