Skip to content

Feature Sugesstion: date_range from end to start #12355

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

Closed
npezolano opened this issue Feb 16, 2016 · 4 comments
Closed

Feature Sugesstion: date_range from end to start #12355

npezolano opened this issue Feb 16, 2016 · 4 comments
Labels
Datetime Datetime data dtype

Comments

@npezolano
Copy link

When using pd.date_range we work our way forwards from start to end. It would be nice to pass a flag or use the dates in reverse to go from end to start when creating the date_range.

#EX:
pd.date_range(end, start)
#or
pd.date_range(start, end, end_to_start=True)
Ex:
start = datetime.datetime(2016 ,2, 8)
end = datetime.datetime(2018 , 6, 1)
pd.date_range(start end, freq='6m', start_to_end=True)
>>> DatetimeIndex(['2016-02-08', '2016-06-01', '2016-12-01', '2017-06-01',
               '2017-12-01', '2018-06-01'])
@max-sixty
Copy link
Contributor

reversed?

In [7]: d=pd.date_range(start='2000', freq='Q', end='2002')

In [8]: d
Out[8]: 
DatetimeIndex(['2000-03-31', '2000-06-30', '2000-09-30', '2000-12-31',
               '2001-03-31', '2001-06-30', '2001-09-30', '2001-12-31'],
              dtype='datetime64[ns]', freq='Q-DEC')

In [9]: reversed(d)
Out[9]: <reversed at 0x1073aaa20>

In [10]: pd.Index(reversed(d))
Out[10]: 
DatetimeIndex(['2001-12-31', '2001-09-30', '2001-06-30', '2001-03-31',
               '2000-12-31', '2000-09-30', '2000-06-30', '2000-03-31'],
              dtype='datetime64[ns]', freq=None)

@npezolano
Copy link
Author

Not reversed, the order doesn't change, but you work the algorithm from the end date to the start. I'll add an example above to make it more clear.

@jreback
Copy link
Contributor

jreback commented Feb 16, 2016

so pls provide a usecase. Generally you would simply reverse sort to work with things in the reverse order.

@rouxrei
Copy link

rouxrei commented Jan 27, 2020

Here's a usecase:
You want to guarantee that your specified end_date is a value in the resulting set of values.

Say you do

start = datetime.datetime(2019 ,1, 1)
end = datetime.datetime(2019 , 6, 1)
pd.date_range(start, end, freq='4MS')

You get

DatetimeIndex(['2019-01-01', '2019-05-01'], dtype='datetime64[ns]', freq='4MS') 

Which means my specified end_date is not a value in my list.
(Note the month values)

With this flag, or "reverse generation" I would expect to get this instead:

start = datetime.datetime(2019 ,1, 1)
end = datetime.datetime(2019 , 6, 1)
pd.date_range(start, end, freq='4MS', reverse=True)

> DatetimeIndex(['2019-02-01', '2019-06-01'], dtype='datetime64[ns]', freq='4MS'))

(Note the month values)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype
Projects
None yet
Development

No branches or pull requests

4 participants