Overview
Thefmt_fecha() function provides robust date formatting for invoice processing, converting various date input formats into a standardized string format. It handles invalid dates gracefully and uses day-first parsing to accommodate Latin American date conventions.
Function Signature
main.py:25-34
Parameters
The date value to format. Can be:
- String date representation (e.g., “15/03/2026”, “2026-03-15”)
- Pandas datetime object
- Python datetime object
- Empty string or NaN value
The output date format using Python strftime codes. Common formats:
"%d/%m/%Y"→ “15/03/2026” (default)"%Y-%m-%d"→ “2026-03-15”"%d-%m-%Y"→ “15-03-2026”"%B %d, %Y"→ “March 15, 2026”
Return Value
Returns the formatted date string according to the specified format. Returns
None if the input is empty or NaN. Returns the original valor unchanged if parsing fails.Date Parsing Behavior
The function uses
dayfirst=True when parsing dates with pd.to_datetime(). This means ambiguous dates like “03/04/2026” are interpreted as April 3rd, 2026 (not March 4th), following Latin American date conventions.Parsing Logic
- Empty/NaN Check: If
valoris NaN or empty string, returnsNoneimmediately - Date Parsing: Attempts to parse using
pd.to_datetime()withdayfirst=Trueanderrors="coerce" - Validation: If parsing results in NaT (Not a Time), returns original
valorunchanged - Formatting: If successful, formats the datetime using the specified
formato - Error Handling: If any exception occurs, returns original
valorunchanged
Error Handling
The function includes multiple layers of error protection:Null/Empty Values
Returns
None for NaN or empty strings without attempting parsingInvalid Dates
Returns original value if date cannot be parsed (e.g., “invalid-date”)
Exception Safety
Catches all exceptions and returns original value to prevent crashes
Coerce Errors
Uses
errors="coerce" to convert unparseable dates to NaT gracefullyExamples
Basic Usage
Day-First Parsing
Error Handling
Invoice Processing Context
Common Use Cases
| Input Type | Example Input | Output (Default) | Notes |
|---|---|---|---|
| String (DMY) | “15/03/2026" | "15/03/2026” | Direct passthrough if already formatted |
| String (ISO) | “2026-03-15" | "15/03/2026” | Converted to day-first format |
| Pandas Timestamp | pd.Timestamp("2026-03-15") | ”15/03/2026” | Formatted from datetime object |
| Excel Serial | 46095 | ”15/03/2026” | Pandas auto-converts Excel date serials |
| Empty/NaN | "" or pd.NA | None | Null-safe handling |
| Invalid | ”invalid" | "invalid” | Returns original value |
Technical Details
Dependencies
- pandas: For robust date parsing with
pd.to_datetime()and null handling withpd.isna() - datetime: For strftime formatting codes
Performance Considerations
- The function uses
errors="coerce"which is safer but slightly slower thanerrors="raise" - For bulk processing of thousands of dates, consider vectorizing with pandas Series operations instead of row-by-row calls
Integration Points
This utility is called from:transformar_fila()function (main.py:74) - Used to format invoice dates before JSON transformation- Invoice document emission dates:
IdentificacionDocumento.FechaEmision - Payment due dates:
IdentificacionDocumento.FechaVencimiento - Payment dates:
FormasPago[].Fecha