Skip to content

now sectionwise: date_converter: delta / time #4632

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions pandas/io/date_converters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
30 changes: 29 additions & 1 deletion pandas/io/tests/test_date_converters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down