Extracting just Month and Year separately from Pandas Datetime column

后端 未结 11 1724
抹茶落季
抹茶落季 2020-11-22 09:09

I have a Dataframe, df, with the following column:

df[\'ArrivalDate\'] =
...
936   2012-12-31
938   2012-12-29
965   2012-12-31
966   2012-12-31
967   2012-1         


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 09:41

    Best way found!!

    the df['date_column'] has to be in date time format.

    df['month_year'] = df['date_column'].dt.to_period('M')
    

    You could also use D for Day, 2M for 2 Months etc. for different sampling intervals, and in case one has time series data with time stamp, we can go for granular sampling intervals such as 45Min for 45 min, 15Min for 15 min sampling etc.

提交回复
热议问题