added feature mapping
This commit is contained in:
41
src/delta_barth/analysis/parse.py
Normal file
41
src/delta_barth/analysis/parse.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping, Set
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from delta_barth.errors import FeaturesMissingError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def check_needed_features(
|
||||
data: pd.DataFrame,
|
||||
features: Set,
|
||||
) -> None:
|
||||
data_feats = set(data.columns)
|
||||
missing_features = features - data_feats
|
||||
|
||||
if missing_features:
|
||||
raise FeaturesMissingError(
|
||||
f"The datset does not contain all needed features: "
|
||||
f"Missing features are: {missing_features}"
|
||||
)
|
||||
|
||||
|
||||
def map_features_to_targets(
|
||||
data: pd.DataFrame,
|
||||
feature_map: Mapping[str, str],
|
||||
) -> pd.DataFrame:
|
||||
data_feats = data.columns
|
||||
mapped_feats: list[str] = []
|
||||
|
||||
for feat in data_feats:
|
||||
if feat in feature_map:
|
||||
mapped_feats.append(feature_map[feat])
|
||||
else:
|
||||
mapped_feats.append(feat)
|
||||
|
||||
data.columns = mapped_feats
|
||||
|
||||
return data
|
||||
@@ -3,6 +3,7 @@ from typing import Final
|
||||
|
||||
from delta_barth.types import CurrentConnection, HttpContentHeaders
|
||||
|
||||
# ** API connection management
|
||||
HTTP_BASE_CONTENT_HEADERS: Final[HttpContentHeaders] = {
|
||||
"Content-type": "application/json",
|
||||
"Accept": "application/json",
|
||||
@@ -15,3 +16,23 @@ HTTP_CURRENT_CONNECTION: Final[CurrentConnection] = CurrentConnection(
|
||||
|
||||
class KnownApiErrorCodes(enum.Enum):
|
||||
COMMON = frozenset((400, 401, 409, 500))
|
||||
|
||||
|
||||
# ** API response parsing
|
||||
# ** column mapping [API-Response --> Target-Features]
|
||||
COL_MAP_SALES_PROGNOSIS: Final[dict[str, str]] = {
|
||||
"artikelId": "artikel_refid",
|
||||
"firmaId": "firma_refid",
|
||||
"betrag": "betrag",
|
||||
"menge": "menge",
|
||||
"buchungsDatum": "buchungs_datum",
|
||||
}
|
||||
FEATURES_SALES_PROGNOSIS: Final[frozenset[str]] = frozenset(
|
||||
(
|
||||
"firma_refid",
|
||||
"beleg_typ",
|
||||
"betrag",
|
||||
"vorgang_refid",
|
||||
"buchungs_datum",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -8,3 +8,7 @@ class UnknownApiErrorCode(Exception):
|
||||
|
||||
class ApiConnectionError(Exception):
|
||||
"""exception raised if an established connection is needed, but the current session is not connected"""
|
||||
|
||||
|
||||
class FeaturesMissingError(Exception):
|
||||
"""exception raised if needed features are missing"""
|
||||
|
||||
Reference in New Issue
Block a user