Skip to content

Commit 5cd9ef6

Browse files
committed
ENH: start convenience date range functions
1 parent b395e3b commit 5cd9ef6

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

pandas/core/daterange.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pylint: disable=E1101,E1103
22

3-
from pandas.core.index import DatetimeIndex
3+
from pandas.core.index import DatetimeIndex, Index
44
import pandas.core.datetools as datetools
55

66
__all__ = ['DateRange']
@@ -9,6 +9,7 @@
99
# DateRange class
1010

1111
class DateRange(DatetimeIndex):
12+
1213
def __new__(cls, start=None, end=None, periods=None,
1314
offset=datetools.bday, time_rule=None,
1415
tzinfo=None, name=None, **kwds):
@@ -26,3 +27,41 @@ def __new__(cls, start=None, end=None, periods=None,
2627
return super(DateRange, cls).__new__(cls, start=start, end=end,
2728
periods=periods, offset=offset, tzinfo=tzinfo, name=name,
2829
_deprecated=True, **kwds)
30+
31+
32+
def date_range(start=None, end=None, periods=None, freq='D', tz=None):
33+
"""
34+
Return a fixed frequency datetime index, with day (calendar) as the default
35+
frequency
36+
37+
38+
Parameters
39+
----------
40+
start :
41+
end :
42+
43+
Returns
44+
-------
45+
46+
"""
47+
return DatetimeIndex(start=start, end=end, periods=periods,
48+
freq=freq, tz=tz)
49+
50+
51+
def bdate_range(start=None, end=None, periods=None, freq='B', tz=None):
52+
"""
53+
Return a fixed frequency datetime index, with business day as the default
54+
frequency
55+
56+
Parameters
57+
----------
58+
59+
60+
Returns
61+
-------
62+
date_range : DatetimeIndex
63+
64+
"""
65+
66+
return DatetimeIndex(start=start, end=end, periods=periods,
67+
freq=freq, tz=tz)

pandas/core/datetools.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def to_interval(self, freq=None):
9191
# Interval logic
9292

9393

94-
class Interval:
94+
class Interval(object):
95+
9596
def __init__(self, value=None, freq=None,
9697
year=None, month=1, quarter=None, day=1,
9798
hour=0, minute=0, second=0):
@@ -305,7 +306,7 @@ def strftime(self, fmt):
305306
directives as the :func:`time.strftime` function of the standard Python
306307
distribution, as well as the specific additional directives ``%f``,
307308
``%F``, ``%q``. (formatting & docs originally from scikits.timeries)
308-
309+
309310
+-----------+--------------------------------+-------+
310311
| Directive | Meaning | Notes |
311312
+===========+================================+=======+
@@ -392,42 +393,42 @@ def strftime(self, fmt):
392393
+-----------+--------------------------------+-------+
393394
| ``%%`` | A literal ``'%'`` character. | |
394395
+-----------+--------------------------------+-------+
395-
396+
396397
.. note::
397-
398+
398399
(1)
399-
The ``%f`` directive is the same as ``%y`` if the frequency is
400+
The ``%f`` directive is the same as ``%y`` if the frequency is
400401
not quarterly.
401-
Otherwise, it corresponds to the 'fiscal' year, as defined by
402+
Otherwise, it corresponds to the 'fiscal' year, as defined by
402403
the :attr:`qyear` attribute.
403-
404+
404405
(2)
405-
The ``%F`` directive is the same as ``%Y`` if the frequency is
406+
The ``%F`` directive is the same as ``%Y`` if the frequency is
406407
not quarterly.
407-
Otherwise, it corresponds to the 'fiscal' year, as defined by
408+
Otherwise, it corresponds to the 'fiscal' year, as defined by
408409
the :attr:`qyear` attribute.
409-
410+
410411
(3)
411-
The ``%p`` directive only affects the output hour field
412+
The ``%p`` directive only affects the output hour field
412413
if the ``%I`` directive is used to parse the hour.
413-
414+
414415
(4)
415416
The range really is ``0`` to ``61``; this accounts for leap
416417
seconds and the (very rare) double leap seconds.
417-
418+
418419
(5)
419-
The ``%U`` and ``%W`` directives are only used in calculations
420+
The ``%U`` and ``%W`` directives are only used in calculations
420421
when the day of the week and the year are specified.
421-
422+
422423
.. rubric:: Examples
423-
424+
424425
>>> a = Interval(freq='Q@JUL', year=2006, quarter=1)
425426
>>> a.strftime('%F-Q%q')
426427
'2006-Q1'
427428
>>> # Output the last month in the quarter of this date
428429
>>> a.strftime('%b-%Y')
429430
'Oct-2005'
430-
>>>
431+
>>>
431432
>>> a = Interval(freq='D', year=2001, month=1, day=1)
432433
>>> a.strftime('%d-%b-%Y')
433434
'01-Jan-2006'
@@ -597,7 +598,7 @@ def _skts_alias_dictionary():
597598
for k in A_prefixes:
598599
alias_dict[k] = 'A'
599600
for m_tup in month_names:
600-
for sep in seps:
601+
for sep in seps:
601602
m1, m2 = m_tup
602603
alias_dict[k + sep + m1] = 'A@' + m1
603604
alias_dict[k + sep + m2] = 'A@' + m1
@@ -629,7 +630,7 @@ def _skts_alias_dictionary():
629630
alias_dict[k + sep + d1] = 'W@' + d1
630631
alias_dict[k + sep + d2] = 'W@' + d1
631632

632-
return alias_dict
633+
return alias_dict
633634

634635
_reverse_interval_code_map = {}
635636
for k, v in _interval_code_map.iteritems():

0 commit comments

Comments
 (0)