-
-
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: date_range not working as it intuitively should when specifying start, end, and periods #20808
Comments
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 |
I agree to definetely don't change the default behaviour. As I see, the default 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 pd.date_range(start, end, periods=1000, freq=None) I suggest a simple change in the 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 🙃) Because the change is really just a convenience feature of the |
FWI, we could change the default
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.
Oh okay I didn't know that (never done this before), then I am going to try to implement it :) |
date_range
not working as it intuitively should when specifying start, end, and periods
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()
INSTALLED VERSIONS
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 44 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None
pandas: 0.22.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.1
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.2.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: