add_local_time¶
add_local_time
¶
add_local_time(panel: DataFrame, tz_name: str, *, time_col: str = 'fetched_at', new_col: str = 'local_time') -> DataFrame
Add a local-time column to a panel, handling the index correctly.
Converting a UTC timestamp to local time is the first step of any diurnal analysis,
and doing it on a MultiIndexed panel by hand (reset_index then tz_convert then
set_index) is a recurring papercut. This flattens the panel and appends one
tz-aware new_col in tz_name, leaving time_col (UTC) untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
panel
|
DataFrame
|
Any panel/frame with a tz-aware (or UTC-coercible) |
required |
tz_name
|
str
|
An IANA zone, e.g. |
required |
time_col
|
str
|
Source UTC column and the local-time column to add. |
'fetched_at'
|
new_col
|
str
|
Source UTC column and the local-time column to add. |
'fetched_at'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A flattened copy of |
See Also
build_availability_panel : Build a panel already in local time via target_tz.
resample_panel : Put the panel on a fixed time grid.
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({"fetched_at": pd.to_datetime(["2026-01-01T08:00Z"])})
>>> int(add_local_time(df, "Europe/Paris")["local_time"].dt.hour.iloc[0])
9