Skip to content

timezone_shift

timezone_shift ¤

TimezoneShift ¤

TimezoneShift(
    dataframe: DataFrame, time_column: str = "systime"
)

Bases: Base

A class for shifting timestamps in a DataFrame to a different timezone, with methods to handle timezone localization and conversion.

shift_timezone classmethod ¤

shift_timezone(
    dataframe: 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 classmethod ¤

add_timezone_column(
    dataframe: 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 classmethod ¤

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 classmethod ¤

detect_timezone_awareness(
    dataframe: 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 classmethod ¤

revert_to_original_timezone(
    dataframe: 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.

calculate_time_difference classmethod ¤

calculate_time_difference(
    dataframe: 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.