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
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?
CC'ing PyData list. Might get a better reply there.
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.
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?