Skip to content

Feature request: Add annual offset that is not anchored #15685

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

Open
rs2 opened this issue Mar 14, 2017 · 5 comments
Open

Feature request: Add annual offset that is not anchored #15685

rs2 opened this issue Mar 14, 2017 · 5 comments
Labels

Comments

@rs2
Copy link
Contributor

rs2 commented Mar 14, 2017

Code Sample, a copy-pastable example if possible

pd.bdate_range(end='today', periods=2, freq='1A').freq.name
# Prints 'A-DEC'
pd.bdate_range(end='today', periods=2, freq='1A')[0].date()
# prints datetime.date(2015, 12, 31)

Problem description

Pandas divides offsets into regular and anchored (see Offset aliases).

There are regular aliases for any frequency but annual. A typical use case - give me a bdate_range with dates spaced 1 year apart ending today. 12M and 52W are only proxies, but won't give the exact expected result.

Expected Output

pd.bdate_range(end='2017-03-12', periods=2, freq='1A-actual-for-real')[0].date() # Saturday
# prints datetime.date(2016, 3, 11) # Friday

Output of pd.show_versions()

pandas: 0.19.2
@chris-b1
Copy link
Contributor

There could be a better API, or at minimum better docs, but you can accomplish this with a DateOffset.

In [216]: pd.date_range(end='2017-03-12', periods=12, freq=pd.DateOffset(years=1))
Out[216]: 
DatetimeIndex(['2006-03-12', '2007-03-12', '2008-03-12', '2009-03-12',
               '2010-03-12', '2011-03-12', '2012-03-12', '2013-03-12',
               '2014-03-12', '2015-03-12', '2016-03-12', '2017-03-12'],
              dtype='datetime64[ns]', freq='<DateOffset: kwds={'years': 1}>')

@jreback jreback added the Frequency DateOffsets label Mar 14, 2017
@rs2
Copy link
Contributor Author

rs2 commented Mar 14, 2017

@chris-b1 2016-03-12 is a Saturday, whereas bdate_range is expected to yield at least Mon-Fri?

@chris-b1
Copy link
Contributor

Oh, one to to note is that bdate_range is just an alias for date_range with freq='B', it doesn't do anything special with other offsets.

Getting hackier, but I think this would work?

pd.date_range(end='2017-03-12', periods=12, freq=pd.DateOffset(years=1)) + pd.offsets.BusinessDay(n=0)

Out[225]: 
DatetimeIndex(['2006-03-13', '2007-03-12', '2008-03-12', '2009-03-12',
               '2010-03-12', '2011-03-14', '2012-03-12', '2013-03-12',
               '2014-03-12', '2015-03-12', '2016-03-14', '2017-03-13'],
              dtype='datetime64[ns]', freq=None)

@rs2
Copy link
Contributor Author

rs2 commented Mar 14, 2017

@chris-b1 When using either DateOffset or freq, is there a way to pass offset as a literal, e.g. '1Y' or '2W' and still get '2016-03-14' when using '1Y'?

@chris-b1
Copy link
Contributor

@rs2 - no, that is currently not supported some discussion about that in #11375

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

No branches or pull requests

4 participants