pythonpandasdate-range

Creating a new column to find the range of date


I am working on a data frame which contains date. I want to create a new column to classify the period of the date in a year. That means that I will assume as every 2 months as a range. But my starting date will be August. Thus,

Aug to Sep is 1,
Oct to Nov is 2,
Dec to Jan is 3,
Feb to Mar is 4 and
Apr to May is 5.

The sample date is as follow:

data =pd.DataFrame()
data ['Date'] = ['27-03-2019','08-10-2019','09-04-2019','09-08-2018']
data.period = pd.to_datetime(data['Date'])

The expected outcome as range in a new column would be as follows:

enter image description here

How can I do that?


Solution

  • I try create dictionary with numpy.roll, numpy.repeat, numpy.arange and last use Series.map with Series.dt.month:

    a = np.arange(1, 13)
    b = np.roll(np.repeat(np.arange(1,7), 2), 7)
    d = dict(zip(a, b))
    print (d)
    {1: 3, 2: 4, 3: 4, 4: 5, 5: 5, 6: 6, 7: 6, 8: 1, 9: 1, 10: 2, 11: 2, 12: 3}
    
    df['Range'] = df['Date'].dt.month.map(d)
    print (df)
            Date  Range
    0 2019-03-27      4
    1 2019-10-08      2
    2 2019-04-09      5
    3 2018-08-09      1