From 5e6f81b50b07ed2ad03b217af0c381b842800e10 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 13 Jul 2026 21:50:32 +0200 Subject: [PATCH] Add hex converter utility - To move to separate utility file in the future --- stat/utils/extractor.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/stat/utils/extractor.py b/stat/utils/extractor.py index 197af1c..807835b 100644 --- a/stat/utils/extractor.py +++ b/stat/utils/extractor.py @@ -1,11 +1,20 @@ -# File: extractor.py -# Copyright (C) 2026 Erick Ahmed -# SPDX-License-Identifier: AGPL-3.0-or-later - import json from pathlib import Path + +import numpy as np import pandas as pd +def to_int(x): + """Convert a hex string or integer to int, returning NaN on failure.""" + if isinstance(x, (int, np.integer)): + return int(x) + if isinstance(x, str): + try: + return int(x, 16) + except ValueError: + return np.nan + return np.nan + def extract_id(row: pd.Series) -> str: """Extracts PGN from metadata or falls back to CAN ID.""" meta = row.get('j1939_metadata') @@ -25,14 +34,3 @@ def load_data(file_path: Path) -> pd.DataFrame: df = pd.read_parquet(file_path) df['Identifier'] = df.apply(extract_id, axis=1) return df - -def to_int(x): - """Convert a hex string or integer to int, returning NaN on failure.""" - if isinstance(x, (int, np.integer)): - return int(x) - if isinstance(x, str): - try: - return int(x, 16) - except ValueError: - return np.nan - return np.nan