Closed onnoeberhard closed 6 years ago
I suppose if freq is NOT specified then you could accept all three and give a linspace repr. What breaks if you do that?
If freq is specified then this should for sure raise. The idea of the error is simply to be helpful in telling you that you may have overspecified the args.
Your workaround from SO is a pretty reasonable solution, not sure we should support this. I definitely would not want to change the default behavior, but suppose could with with something like freq=None
or freq='interpolate'
I agree to definetely don't change the default behaviour. As I see, the default freq
is 'D', so this:
pd.date_range(start, end, periods=1000)
would not work, because it is the same as
pd.date_range(start, end, periods=1000, freq='D')
which really should not work. However, if the user explicitly sets freq=None
, the linspace behaviour would be practical:
pd.date_range(start, end, periods=1000, freq=None)
I suggest a simple change in the date_range
function like so:
def date_range(start=None, end=None, periods=None, freq='D', tz=None,
normalize=False, name=None, closed=None, **kwargs):
"""
...
"""
# Return a linearly spaced DatetimeIndex if `freq` is not set, but `start`, `end`, and `periods` are
if start and end and periods and not freq:
di = tools.to_datetime(np.linspace(start, end, periods), **kwargs)
if name:
di.name = name
return di
return DatetimeIndex(start=start, end=end, periods=periods,
freq=freq, tz=tz, normalize=normalize, name=name,
closed=closed, **kwargs)
together with appropriate changes in the docstring and test functions. This would provide the desired behaviour, while not changing anything else.
I am not yet a contributor, so I cannot implement this myself (unless I am made one :upside_down_face:)
Because the change is really just a convenience feature of the date_range
function, I don't think it would be wise to implement this directly in the DatetimeIndex constructor, or in another function, like bdate_range
.
FWI, we could change the default freq
to None
, and document that it's 'D'
when only two of star, end, and freq are specified. That way pd.date_range(start, end, periods=100)
will work.
I am not yet a contributor, so I cannot implement this myself (unless I am made one 🙃)
Anyone can make a pull request: http://pandas-docs.github.io/pandas-docs-travis/contributing.html
That's a good idea, I like it much better.
Anyone can make a pull request
Oh okay I didn't know that (never done this before), then I am going to try to implement it :)
Code Sample, a copy-pastable example if possible
Problem description
I need a DatetimeIndex object to later use as index in a Series. DatetimeIndex should start at
start
, end atend
and have a fixed number of elements (1000). Intuitively, this should work withpd.date_range
, but it doesn't, and I haven't found a good explanation about why this is the case. I have found a workaround on Stackoverflow (https://stackoverflow.com/questions/25796030/how-can-i-use-pandas-date-range-to-obtain-a-time-series-with-n-specified-perio) that does work:Expected Output
Output of
pd.show_versions()