Table Of Contents

Previous topic

Welcome to DateTools’s documentation!

This Page

Introduction

A brief overview of some often re-used date methods, which are collectively referred to as tools.

Date tools

Collection of useful extensions to Python’s datetime module in the standard library. These functions occur so frequently while scripting and have been rewritten in so many places that it makes sense to have one package containing them.

datetools.daterange(start, end=None, fmt='%Y%m%d', interval=datetime.timedelta(1), numdates=None, todatetime=False)

A simple range function over dates

>>> daterange("20130101", "20130103")
["20130101", "20130102", "20130103"]
Parameters:
  • start – string start date in correct format
  • end – optional end date string
  • fmt – optional datetime format string
  • interval – custom timedelta interval between dates
  • numdates – produces a list of dates of length numdates
  • todatetime – chooses to output results as datetime.datetime objects
Returns:

list of dates

datetools.gaps(dates)

Finds missing dates within a sequence of dates

Parameters:dates – list of dates
Returns:list of missing dates
datetools.shiftdate(date, days, fmt='%Y%m%d', interval=datetime.timedelta(1))

Shifts a date by a number of days

>>> shiftdate("20140606", 10)
"20140616"
Parameters:
  • date – string representation of a date
  • days – int number of days to shift by
  • fmt – optional datetime format string
Returns:

string representation of shifted date

datetools.shiftdates(dates, days, fmt='%Y%m%d')

Shifts a list of dates

Same API as shiftdate, except it operates on an iterable of date strings.

>>> # Shift a list of dates forward 10 days
>>> shiftdate(["20140606", "20150301"], 10)
["20140616", "20150311"]
Parameters:
  • date – string representation of a date
  • days – int number of days to shift by
  • fmt – optional datetime format string
Returns:

list of shifted date strings

See also

shiftdate()

datetools.datewindow(dates, start, end, endpoint=False)

Selects indices which satisfy an interval condition

Chooses dates which lie in the interval [start, end).

Parameters:
  • dates – list of dates
  • start – initial date
  • end – final date
Option endpoint:
 

flag to include end point in window

Returns:

slice(istart, iend)

datetools.strptimes(dates, fmt)

Maps datetime.strptime onto iterable of strings

Parameters:
  • dates – list of strings
  • fmt – datetime.strptime format
Returns:

list of datetime.datetime objects