-
Notifications
You must be signed in to change notification settings - Fork 0
CDSPT-15588 Implemented module for producing type-strict dataframes #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TiffanyCheng27
merged 2 commits into
main
from
CDSPT-15588-create-datatype-mapping-function-in-mario
Jun 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import pandas as pd | ||
| import logging | ||
|
|
||
| from mario.metadata import Item, Metadata | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| INT_TYPES = {"Int16", "Int32", "Int64"} | ||
| SAFE_EARLY_TYPES = {"string", "float64"} | ||
|
|
||
|
|
||
| TYPE_MAPPINGS = { | ||
| "tinyint": "Int16", | ||
| "smallint": "Int16", | ||
| "integer": "Int32", | ||
| "int": "Int32", | ||
| "bigint": "Int64", | ||
| "float": "float64", | ||
| "double": "float64", | ||
| "real": "float64", | ||
| "numeric": "float64", | ||
| "decimal": "float64", | ||
| "varchar": "string", | ||
| "nvarchar": "string", | ||
| "char": "string", | ||
| "text": "string", | ||
| "varbinary": "string", | ||
| "boolean": "boolean", | ||
| "timestamp": "datetime64[ns]", | ||
| "date": "datetime64[ns]" | ||
| } | ||
|
|
||
|
|
||
| def get_column_type(item: Item) -> str: | ||
| """ | ||
| :param item: a metadata item | ||
| :return: the Pandas column type for the item, based on its metadata | ||
| """ | ||
| source_type = 'string' | ||
| # legacy attribute | ||
| if 'DATA_TYPE' in item.properties: | ||
| logger.warning(f'Item {item.name} uses the legacy "DATA_TYPE" attribute; use "format" instead in future') | ||
| source_type = item.get_property('DATA_TYPE').lower() | ||
| # standard attribute | ||
| if 'format' in item.properties: | ||
| source_type = item.get_property('format').lower() | ||
| if source_type in TYPE_MAPPINGS: | ||
| return TYPE_MAPPINGS[source_type] | ||
| return 'string' | ||
|
|
||
|
|
||
| def get_column_types(metadata: Metadata) -> dict[str, str]: | ||
| """ | ||
| Returns the set of column type mappings for the metadata | ||
| :param metadata: | ||
| :return: | ||
| """ | ||
| column_types = { | ||
| item.name: get_column_type(item) | ||
| for item in metadata.items | ||
| } | ||
| return column_types | ||
|
|
||
|
|
||
| def to_bool(x): | ||
| if pd.isna(x): | ||
| return pd.NA | ||
|
|
||
| if isinstance(x, bool): | ||
| return x | ||
|
|
||
| # Handle numeric types explicitly | ||
| if isinstance(x, (int, float)): | ||
| if x == 1: | ||
| return True | ||
| if x == 0: | ||
| return False | ||
| return pd.NA | ||
|
|
||
| x_str = str(x).lower().strip() | ||
| if x_str in {"true", "1", "1.0"}: | ||
| return True | ||
| if x_str in {"false", "0", "0.0"}: | ||
| return False | ||
| return pd.NA | ||
|
|
||
|
|
||
| def enforce_schema(df: pd.DataFrame, column_types: dict[str, str]) -> pd.DataFrame: | ||
| for col, dtype in column_types.items(): | ||
| if col not in df.columns: | ||
| continue | ||
|
|
||
| if dtype == "float64": | ||
| df[col] = pd.to_numeric(df[col], errors="coerce").astype("float64") | ||
|
|
||
| elif dtype in INT_TYPES: | ||
| df[col] = pd.to_numeric(df[col], errors="coerce").astype(dtype) | ||
|
|
||
| elif dtype == "boolean": | ||
| df[col] = df[col].map(to_bool).astype("boolean") | ||
|
|
||
| elif dtype == "datetime64[ns]": | ||
| df[col] = pd.to_datetime(df[col], errors="coerce") | ||
|
|
||
| else: | ||
| df[col] = df[col].astype("string") | ||
|
|
||
| return df | ||
|
|
||
|
|
||
| def hyper_to_df( | ||
| rows: list, | ||
| columns: list[str], | ||
| column_types: dict[str, str] | None, | ||
| ) -> pd.DataFrame: | ||
| """ | ||
| Converts Hyper rows to Pandas DataFrame with strict type enforcement. | ||
| """ | ||
| df = pd.DataFrame(rows, columns=columns) | ||
| if not column_types: | ||
| return df | ||
|
|
||
| return enforce_schema(df, column_types) | ||
|
|
||
|
|
||
| def csv_to_df(file_path: str, column_types: dict[str, str]) -> pd.DataFrame: | ||
| """ | ||
| Converts CSV to Pandas DataFrame with strict type enforcement. | ||
| """ | ||
| import csv | ||
|
|
||
| with open(file_path, newline="") as f: | ||
| csv_columns = next(csv.reader(f)) | ||
|
|
||
| column_types = {k: v for k, v in column_types.items() if k in csv_columns} | ||
|
|
||
| safe_dtype_map = {} | ||
| date_cols = [] | ||
|
|
||
| # We read in only types that can't raise errors, but enforce | ||
| # strict types on output | ||
| for col, dtype in column_types.items(): | ||
| if dtype in SAFE_EARLY_TYPES: | ||
| safe_dtype_map[col] = dtype | ||
| elif dtype == "datetime64[ns]": | ||
| date_cols.append(col) | ||
|
|
||
| df = pd.read_csv( | ||
| file_path, | ||
| dtype=safe_dtype_map if safe_dtype_map else None, | ||
| parse_dates=date_cols if date_cols else None, | ||
| ) | ||
|
|
||
| return enforce_schema(df, column_types) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the bool data x=1.0 or 0.0 then this convert function will return pd.NA
There might be a risk that csv save bool as float type and cause this error.
Can also consider to add trim value function for x_str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed some changes to make boolean handling more robust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked. All good