truncate dates to month start

51 views
Skip to first unread message

Jared Thompson

unread,
Jul 13, 2016, 2:49:46 PM7/13/16
to PyData
I have a column of dates and I am trying to truncate them to be the first of the month for all the dates, for whatever month they are.

example:

dr = pd.date_range('20130101 09:12:56.1234', periods=3)

bs = pd.DataFrame(dr)

bs.columns = ['what']

bs



I can round to the Day as seen here:
bs.what.dt.round('D')

But if I Try to do Month, it fails:
bs.what.dt.round('MS')
ValueError: <MonthBegin> is a non-fixed frequency


Is there another way to do this?

I have been trying other means by trying to create a new column with a map and pulling out year, month etc.. but nothing I have tried yet has worked.

Pratap Vardhan

unread,
Jul 13, 2016, 5:01:30 PM7/13/16
to PyData
Does bs.what.dt.to_period('M').dt.to_timestamp() work for you?

Jared Thompson

unread,
Jul 14, 2016, 11:24:55 AM7/14/16
to PyData
Pretrap,
This will get me to where I need for now.
For the future I will continue to hunt down and see if ther is something that will just truncate down to the 1st of the month like 2016-11-01.

Thanks alot for your help.
Reply all
Reply to author
Forward
0 new messages