Skip to main content
Version: Next

ts_shape.transform.time_functions.timezone_shift

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

TimezoneShift​

classsource

Bases: Base

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

Functions
NameDescription
shift_timezoneShifts timestamps in the specified column of a DataFrame from a given timezone to a target timezone.
add_timezone_columnCreates a new column with timestamps converted from an input timezone to a target timezone, without altering the original column.
list_available_timezonesReturns a sorted list of all available timezones.
detect_timezone_awarenessDetects if a time column in a DataFrame is timezone-aware.
revert_to_original_timezoneReverts a timezone-shifted time column back to the original timezone.
calculate_time_differenceCalculates the time difference between two timestamp columns.

shift_timezone​

classmethodsource
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
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame containing the data.required
time_columnstrThe name of the time column to convert.required
input_timezonestrThe timezone of the input timestamps (e.g., 'UTC' or 'America/New_York').required
target_timezonestrThe target timezone to shift to (e.g., 'America/New_York').required
ambiguousstrHow 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'
nonexistentstrHow 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
TypeDescription
DataFramepd.DataFrame: A copy of the DataFrame with timestamps converted to
DataFramethe target timezone (the input frame is not modified).

add_timezone_column​

classmethodsource
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
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame containing the data.required
time_columnstrThe name of the time column to convert.required
input_timezonestrThe timezone of the input timestamps.required
target_timezonestrThe target timezone.required
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame with an additional column for the shifted timezone.

list_available_timezones​

classmethodsource
list_available_timezones() -> list[str]

Returns a sorted list of all available timezones.

Returns
NameTypeDescription
listlist[str]A sorted list of strings representing all available timezones.

detect_timezone_awareness​

classmethodsource
detect_timezone_awareness(dataframe: pd.DataFrame, time_column: str) -> bool

Detects if a time column in a DataFrame is timezone-aware.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame containing the data.required
time_columnstrThe name of the time column to check.required
Returns
NameTypeDescription
boolboolTrue if the column is timezone-aware, False otherwise.

revert_to_original_timezone​

classmethodsource
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
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame containing the data.required
time_columnstrThe name of the time column to revert.required
original_timezonestrThe original timezone to revert to.required
Returns
TypeDescription
DataFramepd.DataFrame: A DataFrame with timestamps reverted to the original timezone.
Raises
TypeDescription
ValueErrorIf 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​

classmethodsource
calculate_time_difference(
dataframe: pd.DataFrame, start_column: str, end_column: str
) -> pd.Series

Calculates the time difference between two timestamp columns.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame containing the data.required
start_columnstrThe name of the start time column.required
end_columnstrThe name of the end time column.required
Returns
TypeDescription
Seriespd.Series: A Series with the time differences in seconds.