Search code examples
pythonpandasdataframepython-datetimepandasql

How to groupby datetime column by starting day of week in pandas


I have the following dataframe:

transaction_date      gp    
2017-01-17         3477.92  
2017-01-18         5839.64  
2017-01-19         5082.19  
2017-01-20         5761.63  
2017-01-21         6705.89  
2017-01-22         9937.17  
2017-01-23         9432.93  
2017-01-24         7965.56  
2017-01-25         8517.26  
2017-01-26         8098.36  
2017-01-27         8947.25  
2017-01-28         8473.38  
2017-01-29         11660.13 
2017-01-30         10266.24 
2017-01-31         4350.21  
2017-02-01         10820.15 
2017-02-02         8554.61  
2017-02-03         10689.69 

i need to group by transaction_date column by starting day of it week , for example for all transaction between 2017-01-23 to 2017-01-30 it should be Jan-23 so basically i need a new column 'first_day_week' that shows starting day of the week that transaction happened in.


Solution

  • Firstly, you can convert the datetime series object to a period object having the desired weekly frequency. Access it's start_time attribute to get the starting day of every week later. Since we're operating on a series object, remember to provide the .dt accessor each time while carrying out datetime related operations.

    df.assign(first_day_week=
              df['transaction_date'].dt.to_period('W').dt.start_time.dt.strftime("%b-%d"))
    

    enter image description here