Add local time parsing function to handle ISO strings and epoch values in report_service. Update adapter to utilize this function for improved timestamp handling in DataFrame processing.
This commit is contained in:
parent
633df4a5b5
commit
7db2a3daf3
2497
Lightning_Report_Automatic.json
Normal file
2497
Lightning_Report_Automatic.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -24,11 +24,11 @@ _LIGHTNING_COLUMN_ALIASES: dict[str, list[str]] = {
|
||||
"local_time": [
|
||||
"local_time",
|
||||
"localtime",
|
||||
"time",
|
||||
"timestamp",
|
||||
"captured",
|
||||
"datetime",
|
||||
"date_time",
|
||||
"time",
|
||||
"timestamp",
|
||||
],
|
||||
}
|
||||
|
||||
@ -64,6 +64,32 @@ def _apply_aliases(df: pd.DataFrame, aliases: dict[str, list[str]]) -> pd.DataFr
|
||||
return df.rename(columns=rename_map) if rename_map else df
|
||||
|
||||
|
||||
def _parse_local_time_series(series: pd.Series) -> pd.Series:
|
||||
"""
|
||||
Parse strike timestamps from ISO strings or epoch values.
|
||||
|
||||
n8n may add a numeric ``timestamp`` field (epoch ms). Pandas treats bare
|
||||
integers as nanoseconds by default, which collapses ms epochs to ~1970.
|
||||
"""
|
||||
if series.empty:
|
||||
return series
|
||||
|
||||
if not pd.api.types.is_numeric_dtype(series):
|
||||
parsed = pd.to_datetime(series, errors="coerce", utc=True)
|
||||
if parsed.notna().any():
|
||||
return parsed
|
||||
|
||||
numeric = pd.to_numeric(series, errors="coerce")
|
||||
if numeric.notna().any():
|
||||
sample = float(numeric.dropna().iloc[0])
|
||||
if sample >= 1e12:
|
||||
return pd.to_datetime(numeric, unit="ms", errors="coerce", utc=True)
|
||||
if sample >= 1e9:
|
||||
return pd.to_datetime(numeric, unit="s", errors="coerce", utc=True)
|
||||
|
||||
return pd.to_datetime(series, errors="coerce", utc=True)
|
||||
|
||||
|
||||
def _build_turbine_df(turbines: list[dict[str, Any]]) -> pd.DataFrame:
|
||||
if not turbines:
|
||||
return pd.DataFrame(columns=["name", "lat", "lng"])
|
||||
@ -118,7 +144,7 @@ def _build_lightning_df(
|
||||
df["current"] = pd.to_numeric(df["current"], errors="coerce")
|
||||
df["p_type"] = df["p_type"].astype(str)
|
||||
|
||||
local_time = pd.to_datetime(df["local_time"], errors="coerce", utc=True)
|
||||
local_time = _parse_local_time_series(df["local_time"])
|
||||
if timezone_name:
|
||||
try:
|
||||
local_time = local_time.dt.tz_convert(timezone_name)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user