Question: I have three columns in a pandas dataframe that I want to convert into a single date column. The problem is that one of the columns is day column. I am not able to convert into exact date of that month and year. Can anyone please help me to solve this issue. It looks something like this:

```
   BirthMonth BirthYear Day
0   5           88      1st Monday
1   10          87      3rd Tuesday
2   12          87      2nd Saturday
3   1           88      1st Tuesday
4   2           88      1st Monday

```

Answer: 
use the calendar module to get the `day` from days. then convert `day,monyh,year` to `DateTime`

```
import calendar
import datetime
def get_date(rows):
    day = {'monday':0,'tuesday':1,'wednesday':2,'thursday':3,'friday':4,'saturday':5,'sunday':6}
    day_num = day.get(rows.days.split()[1].lower())
    weekday_num = [week[day_num] for week in calendar.monthcalendar(rows.years, rows.months) if week[day_num] >0][int(rows.days.split()[0][0])-1]
    return datetime.date(rows.years, rows.months, weekday_num)

```

apply the above function to all rows

```
df['date'] = df(lambda row: get_date(row), axis=1)
df
>>
          days   months  years  date
0   1st Monday        8  2015   2015-08-03
1   3rd Tuesday      12  2017   2017-12-19
2   4th wednesday     5  2019   2019-05-22

```
**Edit** to match SO new dataframe

My solution using pandas dayofweek function:

```
import numpy as np
import pandas as pd
from datetime import date
from dateutil.relativedelta import relativedelta

#generate dataframe
df=pd.DataFrame({'BirthMonth':[5, 10, 12, 1 ,2],
         'BirthYear':[88, 87, 87, 88, 88],
         'Day':['1st Monday', '3rd Tuesday', '2nd Saturday','1st Tuesday','1st Monday']})
#Assuming the year refers to 19xx
df.BirthYear=1900+df.BirthYear
#list of day names
weekday=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

#Identify day name in input df
days_ex=[s.split()[1].title() for s in df.Day]

#initialize output list
dateout= ["" for x in range(len(days_ex))]
for j in range(len(days_ex)):
    #Identify the day number in the week (Monday is 1, Sunday is 7)
    daynum=np.nonzero(np.char.rfind(weekday,days_ex[j])==0)[0][0]

    #create start and end date for the month
    date_start=date(df.BirthYear[j],df.BirthMonth[j],1)
    date_end=date_start+relativedelta(months=+1)

    #daily index range within month of interest
    idx=pd.date_range(date_start,date_end,freq='d').dayofweek

    # Find matching date based on input df
    realday=np.where(idx==daynum)[0][int(df.Day[j][0])-1]+1
    #output list
    dateout[j]=str(realday)+'/'+str(df.BirthMonth[j])+'/'+str(df.BirthYear[j])

```

the result i got is:

```
['2/5/1988', '20/10/1987', '12/12/1987', '5/1/1988', '1/2/1988']

```