-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: linearly spaced date_range (GH 20808) #20846
Changes from 2 commits
ddc4479
e07fd0d
2cb62c4
73870a7
580bca2
a46307a
a9b875f
2595977
5928bd7
887e3bb
1015e65
4d6cd23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2583,13 +2583,15 @@ def _generate_regular_range(start, end, periods, freq): | |
return data | ||
|
||
|
||
def date_range(start=None, end=None, periods=None, freq='D', tz=None, | ||
def date_range(start=None, end=None, periods=None, freq=None, tz=None, | ||
normalize=False, name=None, closed=None, **kwargs): | ||
""" | ||
Return a fixed frequency DatetimeIndex. | ||
|
||
Exactly two of the three parameters `start`, `end` and `periods` | ||
must be specified. | ||
Two or three of the three parameters `start`, `end` and `periods` | ||
must be specified. If all three parameters are specified, and `freq` is | ||
omitted, the resulting DatetimeIndex will have `periods` linearly spaced | ||
elements between `start` and `end` (closed on both sides). | ||
|
||
Parameters | ||
---------- | ||
|
@@ -2616,6 +2618,8 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None, | |
the 'left', 'right', or both sides (None, the default). | ||
**kwargs | ||
For compatibility. Has no effect on the result. | ||
Can be used to pass arguments to `pd.to_datetime` when specifying | ||
`start`, `end`, and `periods`, but not `freq`. | ||
|
||
Returns | ||
------- | ||
|
@@ -2631,7 +2635,7 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None, | |
-------- | ||
**Specifying the values** | ||
|
||
The next three examples generate the same `DatetimeIndex`, but vary | ||
The next four examples generate the same `DatetimeIndex`, but vary | ||
the combination of `start`, `end` and `periods`. | ||
|
||
Specify `start` and `end`, with the default daily frequency. | ||
|
@@ -2655,6 +2659,13 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None, | |
'2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01'], | ||
dtype='datetime64[ns]', freq='D') | ||
|
||
Specify `start`, `end`, and `periods`; the frequency is generated | ||
automatically (linearly spaced). | ||
|
||
>>> pd.date_range(start='2018-04-24', end='2018-04-27', periods=3) | ||
DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00', | ||
'2018-04-27 00:00:00'], freq=None) | ||
|
||
**Other Parameters** | ||
|
||
Changed the `freq` (frequency) to ``'M'`` (month end frequency). | ||
|
@@ -2704,7 +2715,36 @@ def date_range(start=None, end=None, periods=None, freq='D', tz=None, | |
>>> pd.date_range(start='2017-01-01', end='2017-01-04', closed='right') | ||
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], | ||
dtype='datetime64[ns]', freq='D') | ||
|
||
Declare extra parameters (kwargs) to be used with the pd.to_datetime | ||
function that is used when all three parameters `start`, `end`, and | ||
`periods` are declared. | ||
|
||
>>> date_range('2018-04-24', '2018-04-27', periods=3, box=False) | ||
array(['2018-04-24T00:00:00.000000000', '2018-04-25T12:00:00.000000000', | ||
'2018-04-27T00:00:00.000000000'], dtype='datetime64[ns]') | ||
""" | ||
|
||
# See https://github.com/pandas-dev/pandas/issues/20808 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to go in the DTI constructor itself. we already do some of this validation, it needs to fit in there. Further you don't need to worry about lots of other things that you are repeating, e.g. tz, which are already handled. |
||
if freq is None and periods is not None and start is not None \ | ||
and end is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little bit cleaner to use |
||
if is_float(periods): | ||
periods = int(periods) | ||
elif not is_integer(periods): | ||
msg = 'periods must be a number, got {periods}' | ||
raise TypeError(msg.format(periods=periods)) | ||
|
||
start = Timestamp(start) | ||
end = Timestamp(end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
di = tools.to_datetime(np.linspace(start.value, end.value, periods), | ||
**kwargs) | ||
if name: | ||
di.name = name | ||
return di | ||
|
||
if freq is None: | ||
freq = 'D' | ||
|
||
return DatetimeIndex(start=start, end=end, periods=periods, | ||
freq=freq, tz=tz, normalize=normalize, name=name, | ||
closed=closed, **kwargs) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,21 @@ def test_date_range_ambiguous_arguments(self): | |
with tm.assert_raises_regex(ValueError, msg): | ||
date_range(start, end, periods=10, freq='s') | ||
|
||
def test_date_range_convenience_periods(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also test this with the |
||
# GH 20808 | ||
rng = date_range('2018-04-24', '2018-04-27', periods=3) | ||
exp = pd.DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00', | ||
'2018-04-27 00:00:00'], freq=None) | ||
|
||
tm.assert_index_equal(rng, exp) | ||
|
||
# Test if kwargs work for the to_datetime function used | ||
rng = date_range('2018-04-24', '2018-04-27', periods=3, box=False) | ||
exp = np.array(['2018-04-24T00:00:00', '2018-04-25T12:00:00', | ||
'2018-04-27T00:00:00'], dtype='datetime64[ns]') | ||
|
||
assert (rng == exp).all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
|
||
def test_date_range_businesshour(self): | ||
idx = DatetimeIndex(['2014-07-04 09:00', '2014-07-04 10:00', | ||
'2014-07-04 11:00', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.