Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,464 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | """Normalize calendar / tax / filing year labels to VARCHAR(4)-compatible strings."""
from __future__ import annotations
import math
from datetime import date, datetime
from typing import Any, Optional
try:
import pandas as pd
except ImportError: # pragma: no cover
pd = None # type: ignore
def calendar_year_label(value: Any) -> Optional[str]:
"""Return a four-digit year string, or None if missing or not parseable."""
if value is None:
return None
if pd is not None:
try:
if pd.isna(value):
return None
except (TypeError, ValueError):
pass
if isinstance(value, float) and math.isnan(value):
return None
if isinstance(value, (datetime, date)):
return str(value.year)
if isinstance(value, (int,)) and not isinstance(value, bool):
s = str(value)
if len(s) >= 4 and s[:4].isdigit():
return s[:4]
if s.isdigit() and len(s) < 4:
return s.zfill(4)
return None
s = str(value).strip()
if not s or s.lower() in ("nan", "none", "nat", "<na>"):
return None
if len(s) >= 6 and s.isdigit():
s = s[:4]
if "." in s:
try:
s = str(int(float(s)))
except (ValueError, OverflowError):
return None
if len(s) >= 4 and s[:4].isdigit():
return s[:4]
if s.isdigit() and len(s) < 4:
return s.zfill(4)
return None
|