DateOffset Conventions - MonthBegin/MonthEnd

68 views
Skip to first unread message

Dave Hirschfeld

unread,
Feb 23, 2015, 1:02:25 PM2/23/15
to pystat...@googlegroups.com
I'd like to get the start of the month for the *current* month but it seems that the `n=0` case gives the same answer as the `n=1` case. Is there a way to get the start of the current month?

Since the month for all reference dates is January my expectation is that for `n=0` below all month starts should be 1-Jan-2015 and for `n=1` all month ends  should be 28-Feb-2015. Is my expectation wrong? If so, what is the logic/rule behind the behaviour shown below?

Shouldn't the month end for a given date always be greater than the month begin for the same date?


Thanks,
Dave


In [1]: import numpy as np

   ...: import pandas as pd

   ...: from pandas.tseries.offsets import MonthBegin, MonthEnd


In [4]: print "Reference Date\tMonth Begin\tMonth End"

   ...: for date in pd.date_range('01-Jan-2015', '31-Jan-2015', freq='D'):

   ...:     msg = "{:%d-%b-%Y}:\t{:%d-%b-%Y}\t{:%d-%b-%Y}"

   ...:     print msg.format(date, date+MonthBegin(0), date+MonthBegin(0))

   ...:


Reference Date  Month Begin     Month End

01-Jan-2015:    01-Jan-2015     31-Jan-2015

02-Jan-2015:    01-Feb-2015     31-Jan-2015

03-Jan-2015:    01-Feb-2015     31-Jan-2015

04-Jan-2015:    01-Feb-2015     31-Jan-2015

05-Jan-2015:    01-Feb-2015     31-Jan-2015

06-Jan-2015:    01-Feb-2015     31-Jan-2015

07-Jan-2015:    01-Feb-2015     31-Jan-2015

08-Jan-2015:    01-Feb-2015     31-Jan-2015

09-Jan-2015:    01-Feb-2015     31-Jan-2015

10-Jan-2015:    01-Feb-2015     31-Jan-2015

11-Jan-2015:    01-Feb-2015     31-Jan-2015

12-Jan-2015:    01-Feb-2015     31-Jan-2015

13-Jan-2015:    01-Feb-2015     31-Jan-2015

14-Jan-2015:    01-Feb-2015     31-Jan-2015

15-Jan-2015:    01-Feb-2015     31-Jan-2015

16-Jan-2015:    01-Feb-2015     31-Jan-2015

17-Jan-2015:    01-Feb-2015     31-Jan-2015

18-Jan-2015:    01-Feb-2015     31-Jan-2015

19-Jan-2015:    01-Feb-2015     31-Jan-2015

20-Jan-2015:    01-Feb-2015     31-Jan-2015

21-Jan-2015:    01-Feb-2015     31-Jan-2015

22-Jan-2015:    01-Feb-2015     31-Jan-2015

23-Jan-2015:    01-Feb-2015     31-Jan-2015

24-Jan-2015:    01-Feb-2015     31-Jan-2015

25-Jan-2015:    01-Feb-2015     31-Jan-2015

26-Jan-2015:    01-Feb-2015     31-Jan-2015

27-Jan-2015:    01-Feb-2015     31-Jan-2015

28-Jan-2015:    01-Feb-2015     31-Jan-2015

29-Jan-2015:    01-Feb-2015     31-Jan-2015

30-Jan-2015:    01-Feb-2015     31-Jan-2015

31-Jan-2015:    01-Feb-2015     31-Jan-2015


In [10]: print "Reference Date\tMonth Begin\tMonth End"

    ...: for date in pd.date_range('01-Jan-2015', '31-Jan-2015', freq='D'):

    ...:     msg = "{:%d-%b-%Y}:\t{:%d-%b-%Y}\t{:%d-%b-%Y}"

    ...:     print msg.format(date, date+MonthBegin(1), date+MonthBegin(1))

    ...:

Reference Date  Month Begin     Month End

01-Jan-2015:    01-Feb-2015     31-Jan-2015

02-Jan-2015:    01-Feb-2015     31-Jan-2015

03-Jan-2015:    01-Feb-2015     31-Jan-2015

04-Jan-2015:    01-Feb-2015     31-Jan-2015

05-Jan-2015:    01-Feb-2015     31-Jan-2015

06-Jan-2015:    01-Feb-2015     31-Jan-2015

07-Jan-2015:    01-Feb-2015     31-Jan-2015

08-Jan-2015:    01-Feb-2015     31-Jan-2015

09-Jan-2015:    01-Feb-2015     31-Jan-2015

10-Jan-2015:    01-Feb-2015     31-Jan-2015

11-Jan-2015:    01-Feb-2015     31-Jan-2015

12-Jan-2015:    01-Feb-2015     31-Jan-2015

13-Jan-2015:    01-Feb-2015     31-Jan-2015

14-Jan-2015:    01-Feb-2015     31-Jan-2015

15-Jan-2015:    01-Feb-2015     31-Jan-2015

16-Jan-2015:    01-Feb-2015     31-Jan-2015

17-Jan-2015:    01-Feb-2015     31-Jan-2015

18-Jan-2015:    01-Feb-2015     31-Jan-2015

19-Jan-2015:    01-Feb-2015     31-Jan-2015

20-Jan-2015:    01-Feb-2015     31-Jan-2015

21-Jan-2015:    01-Feb-2015     31-Jan-2015

22-Jan-2015:    01-Feb-2015     31-Jan-2015

23-Jan-2015:    01-Feb-2015     31-Jan-2015

24-Jan-2015:    01-Feb-2015     31-Jan-2015

25-Jan-2015:    01-Feb-2015     31-Jan-2015

26-Jan-2015:    01-Feb-2015     31-Jan-2015

27-Jan-2015:    01-Feb-2015     31-Jan-2015

28-Jan-2015:    01-Feb-2015     31-Jan-2015

29-Jan-2015:    01-Feb-2015     31-Jan-2015

30-Jan-2015:    01-Feb-2015     31-Jan-2015

31-Jan-2015:    01-Feb-2015     28-Feb-2015



Skipper Seabold

unread,
Feb 23, 2015, 6:49:15 PM2/23/15
to pystat...@googlegroups.com, pyd...@googlegroups.com
CC'ing PyData list. Might get a better reply there.

On Mon, Feb 23, 2015 at 12:02 PM, Dave Hirschfeld <nov...@gmail.com> wrote:
I'd like to get the start of the month for the *current* month but it seems that the `n=0` case gives the same answer as the `n=1` case. Is there a way to get the start of the current month?

Since the month for all reference dates is January my expectation is that for `n=0` below all month starts should be 1-Jan-2015 and for `n=1` all month ends  should be 28-Feb-2015. Is my expectation wrong? If so, what is the logic/rule behind the behaviour shown below?

Shouldn't the month end for a given date always be greater than the month begin for the same date?

IIRC, the logic is that if the current date is not on the offset, then it is rolled back to be on the offset and then the transform is performed. Does that explain the behavior?


There certainly could be some bugs lurking. I never finished working through the logic for this one.

I assume the latter should be MonthEnd? 

Dave Hirschfeld

unread,
Feb 24, 2015, 3:04:29 AM2/24/15
to pystat...@googlegroups.com, pyd...@googlegroups.com


On Monday, 23 February 2015 23:49:15 UTC, jseabold wrote:
CC'ing PyData list. Might get a better reply there.

Thanks, I accidentally posted to the wrong list after a google search brought up a discussion on offsets on this ML.
 

Shouldn't the month end for a given date always be greater than the month begin for the same date?

IIRC, the logic is that if the current date is not on the offset, then it is rolled back to be on the offset and then the transform is performed. Does that explain the behavior?


There certainly could be some bugs lurking. I never finished working through the logic for this one.



Thanks for the pointers!
 


In [10]: print "Reference Date\tMonth Begin\tMonth End"

    ...: for date in pd.date_range('01-Jan-2015', '31-Jan-2015', freq='D'):

    ...:     msg = "{:%d-%b-%Y}:\t{:%d-%b-%Y}\t{:%d-%b-%Y}"

    ...:     print msg.format(date, date+MonthBegin(1), date+MonthBegin(1))


I assume the latter should be MonthEnd?

Yep, I caught that and posted the correct results but forgot to change the code I'd pasted above, sorry for the confusion.


Thanks for the reply, I'll try to get my head around what it's doing and why and see if I can make use of it. It does seem pretty non-intuitive at first glance.

Cheers,
Dave 
Reply all
Reply to author
Forward
0 new messages