Derek Krantz
Derek Krantz

Reputation: 487

Not understanding output of MonthEnd() in pandas

Say i have the following:

start = dt.datetime(2015,07,01)
end = dt.datetime(2016,06,30)

rng = pd.date_range(start, end,freq='M')

print rng

DatetimeIndex(['2015-07-31', '2015-08-31', '2015-09-30', '2015-10-31',
           '2015-11-30', '2015-12-31', '2016-01-31', '2016-02-29',
           '2016-03-31', '2016-04-30', '2016-05-31', '2016-06-30'],
          dtype='datetime64[ns]', freq='M')

I would expect :

rng[0] + MonthBegin()

to give an output of '2015-07-01'. instead it shows '2015-08-01'

what am i missing?

Upvotes: 1

Views: 1637

Answers (1)

root
root

Reputation: 33793

See the Anchored Offset Semantics section of the documentation.

Anchored offsets will snap to the next anchor point. If your date is within a month, MonthBegin() will snap to the beginning of the next month.

If you want the beginning of the current month, you can either subtract MonthBegin():

rng[0] - MonthBegin()

Or you can add MonthBegin(-1):

rng[0] + MonthBegin(-1)

Upvotes: 1

Related Questions