Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When converting a date time where the time is '00:00:00' infer returns a date type instead of date time #11

Merged
merged 8 commits into from
Jan 14, 2018
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
.coverage
dist
MANIFEST
.idea*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: python

python:
- "2.7"
- "3.2"
- "3.3"
- "3.5"
- "3.6"

install:
- pip install -q coveralls --use-mirrors
- pip install -q coveralls
- pip install -r requirements.txt
- pip install flake8

Expand Down
38 changes: 20 additions & 18 deletions strconv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import re
import sys

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

from collections import Counter
from datetime import datetime


__version__ = '0.4.1'
# strconv.py
# Copyright (c) 2013 Byron Ruth
# BSD License

__version__ = '0.4.1'

from collections import Counter


class TypeInfo(object):
"Sampling and frequency of a type for a sample of values."
Expand Down Expand Up @@ -190,10 +194,6 @@ def infer_matrix(self, matrix, n=None, size=10):

# Built-in converters

import re

from datetime import datetime

# Use dateutil for more robust parsing
try:
from dateutil.parser import parse as duparse
Expand All @@ -208,6 +208,7 @@ def infer_matrix(self, matrix, n=None, size=10):
DATE_FORMATS = (
'%Y-%m-%d',
'%m-%d-%Y',
'%Y/%m/%d',
'%m/%d/%Y',
'%m.%d.%Y',
'%m-%d-%y',
Expand All @@ -221,7 +222,9 @@ def infer_matrix(self, matrix, n=None, size=10):
'%H:%M:%S',
'%H:%M',
'%I:%M:%S %p',
'%I:%M:%S %z',
'%I:%M %p',
'%I:%M %z',
'%I:%M',
)

Expand All @@ -248,22 +251,21 @@ def convert_bool(s):


def convert_datetime(s, date_formats=DATE_FORMATS, time_formats=TIME_FORMATS):
if duparse:
try:
dt = duparse(s)
if dt.time():
return duparse(s)
except TypeError: # parse may throw this in py3
raise ValueError
if sys.version < '3.5':
if duparse:
try:
dt = duparse(s)
if dt.time():
return duparse(s)
except TypeError: # parse may throw this in py3
raise ValueError

for df in date_formats:
for tf in time_formats:
for sep in DATE_TIME_SEPS:
f = '{0}{1}{2}'.format(df, sep, tf)
try:
dt = datetime.strptime(s, f)
if dt.time():
return dt
return datetime.strptime(s, f)
except ValueError:
pass
raise ValueError
Expand Down
10 changes: 10 additions & 0 deletions test_strconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def test_convert(self):
self.assertEqual(strconv.convert('5:40 PM'), time(17, 40))
self.assertEqual(strconv.convert('March 4, 2013 5:40 PM'),
datetime(2013, 3, 4, 17, 40, 0))
self.assertEqual(strconv.convert('March 4, 2013 12:00 AM'),
datetime(2013, 3, 4, 0, 0))

def test_convert_include_type(self):
self.assertEqual(strconv.convert('-3', include_type=True), (-3, 'int'))
Expand All @@ -67,6 +69,13 @@ def test_infer(self):
self.assertEqual(strconv.infer('3/20/2013'), 'date')
self.assertEqual(strconv.infer('5:40 PM'), 'time')
self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime')
self.assertEqual(strconv.infer('2018-12-01 00:00:00'), 'datetime')
self.assertEqual(strconv.infer('March 4, 2013 12:00 PM'), 'datetime')
# Midnight
self.assertEqual(strconv.convert_datetime('2013-03-01 00:00:00'),
datetime(2013, 3, 1, 0, 0, 0))
self.assertEqual(strconv.convert_datetime('2018/03/01 00:00:00'),
datetime(2018, 3, 1, 0, 0, 0))

def test_infer_converted(self):
self.assertEqual(strconv.infer('-3', converted=True), int)
Expand Down Expand Up @@ -159,5 +168,6 @@ def test_convert_datetime(self):
self.assertEqual(strconv.convert_datetime('2013-03-01 5:30:40 -0500'),
datetime(2013, 3, 1, 5, 30, 40, tzinfo=tzoff))


if __name__ == '__main__':
unittest.main()