From a1edabe8526d92fafd10156a073e84f0a321a048 Mon Sep 17 00:00:00 2001 From: timmie Date: Thu, 22 Aug 2013 03:18:26 +0200 Subject: [PATCH] now sectionwise: date_converter: delta / time --- pandas/io/date_converters.py | 75 +++++++++++++++++++++++++ pandas/io/tests/test_date_converters.py | 30 +++++++++- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/pandas/io/date_converters.py b/pandas/io/date_converters.py index 2be477f49e28b..3aa9955a6affe 100644 --- a/pandas/io/date_converters.py +++ b/pandas/io/date_converters.py @@ -1,4 +1,6 @@ """This module is designed for community supported date conversion functions""" +from datetime import datetime, timedelta, time + from pandas.compat import range import numpy as np import pandas.lib as lib @@ -56,3 +58,76 @@ def _check_columns(cols): raise AssertionError() return N + + +## Datetime Conversion for date_parsers +## see also: create a community supported set of typical converters +## https://github.com/pydata/pandas/issues/1180 + +def offset_datetime(dt_in, days=0, hours=0, minutes=0, + seconds=0, microseconds=0): + '''appply corrective time offset using datetime.timedelta + + input + ----- + dt_in : datetime.time or datetime.datetime object + days : integer value (positive or negative) for days component of offset + hours : integer value (positive or negative) for hours component of offset + minutes : integer value (positive or negative) for + minutes component of offset + seconds : integer value (positive or negative) for + seconds component of offset + microseconds : integer value (positive or negative) for + microseconds component of offset + + output + ------ + ti_corr : datetime.time or datetime.datetime object + + + ''' + # if a excel time like '23.07.2013 24:00' they actually mean + # in Python '23.07.2013 23:59', must be converted +# offset = -10 # minutes + delta = timedelta(days=days, hours=hours, minutes=minutes, + seconds=seconds, microseconds=microseconds) + + #check if offset it to me applied on datetime or time + if type(dt_in) is time: + #create psydo datetime + dt_now = datetime.now() + dt_base = datetime.combine(dt_now, dt_in) + else: + dt_base = dt_in + + dt_corr = (dt_base) + delta + + #if input is time, we return it. + if type(dt_in) is time: + dt_corr = dt_corr.time() + + return dt_corr + + +def dt2ti(dt_in): + '''converts wrong datetime.datetime to datetime.time + + input + ----- + dt_in : dt_in : datetime.time or datetime.datetime object + + output + ------- + ti_corr : datetime.time object + ''' + # so we correct those which are not of type :mod:datetime.time + # impdt2tiortant hint: + # http://stackoverflow.com/a/12906456 + if type(dt_in) is not time: + dt_in = dt_in.time() + elif type(dt_in) is datetime: + dt_in = dt_in.time() + else: + pass + + return dt_in diff --git a/pandas/io/tests/test_date_converters.py b/pandas/io/tests/test_date_converters.py index 8c1009b904857..7bba0212b1348 100644 --- a/pandas/io/tests/test_date_converters.py +++ b/pandas/io/tests/test_date_converters.py @@ -1,5 +1,5 @@ from pandas.compat import StringIO, BytesIO -from datetime import date, datetime +from datetime import datetime, time, timedelta, date import csv import os import sys @@ -120,6 +120,34 @@ def test_generic(self): self.assert_('ym' in df) self.assert_(df.ym.ix[0] == date(2001, 1, 1)) + def test_offset_datetime(self): + #test with a datetime.datetime object + dt_in = datetime(2013, 1, 1, 1, 10, 10, 100000) + dt_target = datetime(2013, 1, 2, 6, 20, 40, 100600) + dt_res = conv.offset_datetime(dt_in, days=1, hours=5, minutes=10, + seconds=30, microseconds=600) + + assert(dt_res == dt_target) + #test with a datetime.time object + ti_in = time(1, 10, 20, 100000) + ti_target = time(6, 20, 50, 100600) + ti_res = conv.offset_datetime(ti_in, hours=5, minutes=10, + seconds=30, microseconds=600) + assert(ti_res == ti_target) + + def test_dt2ti(self): + #a datetime.datetime object + dt_in = datetime(2013, 1, 1, 1, 10, 10, 100000) + ti_target = time(1, 10, 10, 100000) + dt2ti_dt_res = conv.dt2ti(dt_in) + assert(ti_target == dt2ti_dt_res) + + #a datetime.time object + ti_in = time(1, 10, 20, 100000) + ti_target_dt2ti = time(1, 10, 20, 100000) + dt2ti_ti_res = conv.dt2ti(ti_in) + assert(ti_target_dt2ti == dt2ti_ti_res) + if __name__ == '__main__': import nose