-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return dataframe with columns properly cast (#104)
* feat: add dateparser * refactor: more robust date detection * feat: add datetime to fields * feat: create bool casting function * feat: add datetime to init * feat: add more date labels * refactor: consistency renaming * feat: add df casting process * fix: better label test * feat: add cast test * refactor: cast column as int * feat: tests * fix: use optional instead of pipe * docs: update changelog * docs: hint type Co-authored-by: Adrien Carpentier <[email protected]> * docs: hint type Co-authored-by: Adrien Carpentier <[email protected]> * docs: hint type Co-authored-by: Adrien Carpentier <[email protected]> * docs: hint type Co-authored-by: Adrien Carpentier <[email protected]> * fix: add missing import * fix: cast specific datetime formats to datetime * feat: add option to not cast json columns --------- Co-authored-by: Adrien Carpentier <[email protected]>
- Loading branch information
1 parent
abe0b14
commit 0bc8d8b
Showing
12 changed files
with
187 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
PROPORTION = 1 | ||
liste_bool = { | ||
'0', | ||
'1', | ||
'vrai', | ||
'faux', | ||
'true', | ||
'false', | ||
'oui', | ||
'non', | ||
'yes', | ||
'no', | ||
'y', | ||
'n', | ||
'o' | ||
bool_mapping = { | ||
"1": True, | ||
"0": False, | ||
"vrai": True, | ||
"faux": False, | ||
"true": True, | ||
"false": False, | ||
"oui": True, | ||
"non": False, | ||
"yes": True, | ||
"no": False, | ||
"y": True, | ||
"n": False, | ||
"o": True, | ||
} | ||
|
||
liste_bool = set(bool_mapping.keys()) | ||
|
||
def _is(val): | ||
'''Détection les booléens''' | ||
|
||
def bool_casting(val: str) -> bool: | ||
return bool_mapping.get(val) | ||
|
||
|
||
def _is(val: str) -> bool: | ||
'''Détecte les booléens''' | ||
return isinstance(val, str) and val.lower() in liste_bool |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,46 +1,30 @@ | ||
import re | ||
from dateutil.parser import parse, ParserError | ||
from csv_detective.detect_fields.other.float import _is as is_float | ||
from unidecode import unidecode | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from dateparser import parse as date_parser | ||
from dateutil.parser import parse as dateutil_parser, ParserError | ||
|
||
PROPORTION = 1 | ||
# /!\ this is only for dates, not datetimes which are handled by other utils | ||
|
||
|
||
def is_dateutil_date(val: str) -> bool: | ||
# we don't want to get datetimes here, so length restriction | ||
# longest date string expected here is DD-septembre-YYYY, so 17 characters | ||
if len(val) > 17: | ||
return False | ||
def date_casting(val: str) -> Optional[datetime]: | ||
"""For performance reasons, we try first with dateutil and fallback on dateparser""" | ||
try: | ||
res = parse(val, fuzzy=False) | ||
if res.hour or res.minute or res.second: | ||
return False | ||
return True | ||
except (ParserError, ValueError, TypeError, OverflowError): | ||
return False | ||
|
||
|
||
seps = r'[\s/\-\*_\|;.,]' | ||
# matches JJ-MM-AAAA with any of the listed separators | ||
pat = r'^(0[1-9]|[12][0-9]|3[01])SEP(0[1-9]|1[0-2])SEP((19|20)\d{2})$'.replace('SEP', seps) | ||
# matches AAAA-MM-JJ with any of the listed separators OR NO SEPARATOR | ||
tap = r'^((19|20)\d{2})SEP(0[1-9]|1[0-2])SEP(0[1-9]|[12][0-9]|3[01])$'.replace('SEP', seps + '?') | ||
# matches JJ-mmm-AAAA and JJ-mmm...mm-AAAA with any of the listed separators OR NO SEPARATOR | ||
letters = ( | ||
r'^(0[1-9]|[12][0-9]|3[01])SEP(jan|fev|feb|mar|avr|apr' | ||
r'|mai|may|jun|jui|jul|aou|aug|sep|oct|nov|dec|janvier|fevrier|mars|avril|' | ||
r'mai|juin|jullet|aout|septembre|octobre|novembre|decembre)SEP' | ||
r'(\d{2}|\d{4})$' | ||
).replace('SEP', seps + '?') | ||
return dateutil_parser(val) | ||
except ParserError: | ||
return date_parser(val) | ||
|
||
|
||
def _is(val): | ||
'''Renvoie True si val peut être une date, False sinon | ||
On ne garde que les regex pour les cas où parse() ne convient pas''' | ||
return isinstance(val, str) and ( | ||
(is_dateutil_date(val) and not is_float(val)) | ||
or bool(re.match(letters, unidecode(val))) | ||
or bool(re.match(pat, val)) | ||
or bool(re.match(tap, val)) | ||
) | ||
'''Renvoie True si val peut être une date, False sinon''' | ||
# early stops, to cut processing time | ||
if not isinstance(val, str) or len(val) > 20 or len(val) < 8: | ||
return False | ||
threshold = 0.3 | ||
if sum([char.isdigit() for char in val]) / len(val) < threshold: | ||
return False | ||
res = date_casting(val) | ||
if not res or res.hour or res.minute or res.second: | ||
return False | ||
return True |
This file contains 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,19 @@ | ||
from typing import Any, Optional | ||
|
||
from csv_detective.detect_fields.temp.date import date_casting | ||
|
||
PROPORTION = 1 | ||
|
||
|
||
def _is(val: Optional[Any]) -> bool: | ||
'''Renvoie True si val peut être un datetime, False sinon''' | ||
# early stops, to cut processing time | ||
if not isinstance(val, str) or len(val) > 30 or len(val) < 15: | ||
return False | ||
threshold = 0.7 | ||
if sum([char.isdigit() for char in val]) / len(val) < threshold: | ||
return False | ||
res = date_casting(val) | ||
if res and (res.hour or res.minute or res.second): | ||
return True | ||
return False |
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
boto3==1.34.0 | ||
dateparser==1.2.0 | ||
faust-cchardet==2.1.19 | ||
pandas==2.2.0 | ||
pytest==8.3.0 | ||
|
This file contains 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
This file contains 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