Skip to content

Commit

Permalink
v1.20.5 Improve date inferring
Browse files Browse the repository at this point in the history
  • Loading branch information
akariv committed Mar 12, 2024
1 parent 5e9d7f6 commit c81eb66
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tableschema/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.20.4
1.20.5
15 changes: 10 additions & 5 deletions tableschema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def __hash__(self):
'array',
'datetime',
'time',
'date',
('date', ('%Y-%m-%d', '%Y/%m/%d', '%d/%m/%Y', '%m/%d/%Y', '%Y%m%d', '%Y.%m.%d')),
'integer',
'number',
'boolean',
Expand All @@ -569,12 +569,17 @@ def __init__(self, missing_values):
self.missing_values = missing_values

def cast(self, value):
for priority, name in enumerate(_INFER_TYPE_ORDER):
for priority, type_rec in enumerate(_INFER_TYPE_ORDER):
if isinstance(type_rec, tuple):
name, formats = type_rec
else:
name, formats = type_rec, ['default']
cast = getattr(types, 'cast_%s' % name)
if value not in self.missing_values:
result = cast('default', value)
if result != config.ERROR:
yield (name, 'default', priority)
for format in formats:
result = cast(format, value)
if result != config.ERROR:
yield (name, format, priority)


class _TypeResolver(object):
Expand Down
14 changes: 8 additions & 6 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,21 @@ def test_save(tmpdir, apply_defaults):

def test_infer():
data = [
['id', 'age', 'name'],
['1','39','Paul'],
['2','23','Jimmy'],
['3','36','Jane'],
['4','N/A','Judy'],
['id', 'age', 'name', 'dob'],
['1','39','Paul','28/1/1979'],
['2','23','Jimmy','13/6/1995'],
['3','36','Jane','17/9/1980'],
['4','N/A','Judy','19/4/1983'],
]
schema = Schema()
schema.infer(data)
assert schema.descriptor == {
'fields': [
{'format': 'default', 'name': 'id', 'type': 'integer'},
{'format': 'default', 'name': 'age', 'type': 'integer'},
{'format': 'default', 'name': 'name', 'type': 'string'}],
{'format': 'default', 'name': 'name', 'type': 'string'},
{'format': '%d/%m/%Y', 'name': 'dob', 'type': 'date'},
],
'missingValues': ['']}
data = [
['id', 'age', 'name'],
Expand Down

0 comments on commit c81eb66

Please sign in to comment.