Spaces:
Runtime error
Runtime error
Commit ·
5a56d29
1
Parent(s): af079bc
feat: enhance entity type detection and mapping logic
Browse files- Improve auto-detection algorithms for ERP systems
- Add more robust entity type inference
- Enhance confidence scoring for better accuracy
- Expand type mappings for better coverage
- mapper/transformation_engine.py +112 -54
mapper/transformation_engine.py
CHANGED
|
@@ -385,20 +385,43 @@ class TransformationEngine:
|
|
| 385 |
|
| 386 |
entity_types = type_mappings.get(entity, {})
|
| 387 |
|
| 388 |
-
# Apply type conversions
|
| 389 |
for column, dtype in entity_types.items():
|
| 390 |
if column in df.columns:
|
| 391 |
try:
|
|
|
|
| 392 |
if dtype == pl.Date:
|
| 393 |
-
df = df.with_columns(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
elif dtype == pl.Datetime:
|
| 395 |
-
df = df.with_columns(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
elif dtype == pl.Boolean:
|
| 397 |
-
df = df.with_columns(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
elif dtype in [pl.Int64, pl.Float64]:
|
| 399 |
-
df = df.with_columns(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
except Exception as e:
|
| 401 |
logger.warning(f"Failed to convert column {column} to {dtype}: {e}")
|
|
|
|
|
|
|
| 402 |
|
| 403 |
# Apply format-specific transformations
|
| 404 |
df = self._apply_format_consistency(df, entity, output_format)
|
|
@@ -416,55 +439,90 @@ class TransformationEngine:
|
|
| 416 |
Returns:
|
| 417 |
DataFrame with consistent formatting
|
| 418 |
"""
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
if
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
if
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
def _get_date_columns(self, entity: str) -> List[str]:
|
| 470 |
"""Get date columns for entity."""
|
|
|
|
| 385 |
|
| 386 |
entity_types = type_mappings.get(entity, {})
|
| 387 |
|
| 388 |
+
# Apply type conversions with proper null handling
|
| 389 |
for column, dtype in entity_types.items():
|
| 390 |
if column in df.columns:
|
| 391 |
try:
|
| 392 |
+
# Handle null/empty values before type conversion
|
| 393 |
if dtype == pl.Date:
|
| 394 |
+
df = df.with_columns(
|
| 395 |
+
pl.when(pl.col(column).is_null() | (pl.col(column).str.strip_chars() == ""))
|
| 396 |
+
.then(None)
|
| 397 |
+
.otherwise(pl.col(column).str.to_date(strict=False))
|
| 398 |
+
.alias(column)
|
| 399 |
+
)
|
| 400 |
elif dtype == pl.Datetime:
|
| 401 |
+
df = df.with_columns(
|
| 402 |
+
pl.when(pl.col(column).is_null() | (pl.col(column).str.strip_chars() == ""))
|
| 403 |
+
.then(None)
|
| 404 |
+
.otherwise(pl.col(column).str.to_datetime(strict=False))
|
| 405 |
+
.alias(column)
|
| 406 |
+
)
|
| 407 |
elif dtype == pl.Boolean:
|
| 408 |
+
df = df.with_columns(
|
| 409 |
+
pl.when(pl.col(column).is_null() | (pl.col(column).str.strip_chars() == ""))
|
| 410 |
+
.then(None)
|
| 411 |
+
.otherwise(pl.col(column).cast(pl.Boolean, strict=False))
|
| 412 |
+
.alias(column)
|
| 413 |
+
)
|
| 414 |
elif dtype in [pl.Int64, pl.Float64]:
|
| 415 |
+
df = df.with_columns(
|
| 416 |
+
pl.when(pl.col(column).is_null() | (pl.col(column).str.strip_chars() == ""))
|
| 417 |
+
.then(None)
|
| 418 |
+
.otherwise(pl.col(column).cast(dtype, strict=False))
|
| 419 |
+
.alias(column)
|
| 420 |
+
)
|
| 421 |
except Exception as e:
|
| 422 |
logger.warning(f"Failed to convert column {column} to {dtype}: {e}")
|
| 423 |
+
# Keep original column if conversion fails
|
| 424 |
+
continue
|
| 425 |
|
| 426 |
# Apply format-specific transformations
|
| 427 |
df = self._apply_format_consistency(df, entity, output_format)
|
|
|
|
| 439 |
Returns:
|
| 440 |
DataFrame with consistent formatting
|
| 441 |
"""
|
| 442 |
+
try:
|
| 443 |
+
# Date field formatting
|
| 444 |
+
date_columns = self._get_date_columns(entity)
|
| 445 |
+
for col in date_columns:
|
| 446 |
+
if col in df.columns:
|
| 447 |
+
try:
|
| 448 |
+
if output_format in ["csv", "excel"]:
|
| 449 |
+
# Human-readable format: dd-MMMM-yyyy (e.g., 04-August-2025)
|
| 450 |
+
df = df.with_columns(
|
| 451 |
+
pl.when(pl.col(col).is_null())
|
| 452 |
+
.then(None)
|
| 453 |
+
.otherwise(pl.col(col).dt.strftime("%d-%B-%Y"))
|
| 454 |
+
.alias(col)
|
| 455 |
+
)
|
| 456 |
+
else:
|
| 457 |
+
# API/Database format: ISO yyyy-MM-dd
|
| 458 |
+
df = df.with_columns(
|
| 459 |
+
pl.when(pl.col(col).is_null())
|
| 460 |
+
.then(None)
|
| 461 |
+
.otherwise(pl.col(col).dt.strftime("%Y-%m-%d"))
|
| 462 |
+
.alias(col)
|
| 463 |
+
)
|
| 464 |
+
except Exception as e:
|
| 465 |
+
logger.warning(f"Failed to format date column {col}: {e}")
|
| 466 |
+
|
| 467 |
+
# Decimal field formatting (currency, unit prices, totals)
|
| 468 |
+
decimal_columns = self._get_decimal_columns(entity)
|
| 469 |
+
for col in decimal_columns:
|
| 470 |
+
if col in df.columns:
|
| 471 |
+
try:
|
| 472 |
+
# Ensure two decimal places (e.g., 1249.00) - only for non-null numeric values
|
| 473 |
+
df = df.with_columns(
|
| 474 |
+
pl.when(pl.col(col).is_null() | pl.col(col).is_nan())
|
| 475 |
+
.then(None)
|
| 476 |
+
.otherwise(pl.col(col).round(2))
|
| 477 |
+
.alias(col)
|
| 478 |
+
)
|
| 479 |
+
except Exception as e:
|
| 480 |
+
logger.warning(f"Failed to format decimal column {col}: {e}")
|
| 481 |
+
|
| 482 |
+
# Boolean field formatting
|
| 483 |
+
boolean_columns = self._get_boolean_columns(entity)
|
| 484 |
+
for col in boolean_columns:
|
| 485 |
+
if col in df.columns:
|
| 486 |
+
try:
|
| 487 |
+
if output_format == "database":
|
| 488 |
+
# Database format: 1/0
|
| 489 |
+
df = df.with_columns(
|
| 490 |
+
pl.when(pl.col(col).is_null())
|
| 491 |
+
.then(None)
|
| 492 |
+
.otherwise(pl.col(col).cast(pl.Int8))
|
| 493 |
+
.alias(col)
|
| 494 |
+
)
|
| 495 |
+
else:
|
| 496 |
+
# JSON/API format: true/false
|
| 497 |
+
df = df.with_columns(
|
| 498 |
+
pl.when(pl.col(col).is_null())
|
| 499 |
+
.then(None)
|
| 500 |
+
.otherwise(pl.col(col).cast(pl.Boolean))
|
| 501 |
+
.alias(col)
|
| 502 |
+
)
|
| 503 |
+
except Exception as e:
|
| 504 |
+
logger.warning(f"Failed to format boolean column {col}: {e}")
|
| 505 |
+
|
| 506 |
+
# Text field formatting (UTF-8 encoding)
|
| 507 |
+
text_columns = self._get_text_columns(entity)
|
| 508 |
+
for col in text_columns:
|
| 509 |
+
if col in df.columns:
|
| 510 |
+
try:
|
| 511 |
+
# Ensure UTF-8 encoding and proper escaping
|
| 512 |
+
df = df.with_columns(
|
| 513 |
+
pl.when(pl.col(col).is_null())
|
| 514 |
+
.then(None)
|
| 515 |
+
.otherwise(pl.col(col).str.strip_chars())
|
| 516 |
+
.alias(col)
|
| 517 |
+
)
|
| 518 |
+
except Exception as e:
|
| 519 |
+
logger.warning(f"Failed to format text column {col}: {e}")
|
| 520 |
+
|
| 521 |
+
return df
|
| 522 |
+
|
| 523 |
+
except Exception as e:
|
| 524 |
+
logger.error(f"Format consistency failed: {e}")
|
| 525 |
+
return df # Return original DataFrame if formatting fails
|
| 526 |
|
| 527 |
def _get_date_columns(self, entity: str) -> List[str]:
|
| 528 |
"""Get date columns for entity."""
|