Extracting the first day of month of a datetime type column in pandas

前端 未结 8 1195
你的背包
你的背包 2020-12-14 06:11

I have the following dataframe:

user_id    purchase_date 
  1        2015-01-23 14:05:21
  2        2015-02-05 05:07:30
  3        2015-02-18 17:08:51
  4            


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-14 06:50

    To extract the first day of every month, you could write a little helper function that will also work if the provided date is already the first of month. The function looks like this:

    def first_of_month(date):
        return date + pd.offsets.MonthEnd(-1) + pd.offsets.Day(1)
    

    You can apply this function on pd.Series:

    df['month'] = df['purchase_date'].apply(first_of_month)
    

    With that you will get the month column as a Timestamp. If you need a specific format, you might convert it with the strftime() method.

    df['month_str'] = df['month'].dt.strftime('%Y-%m-%d')
    

提交回复
热议问题