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

```
Not very fast solution(since it involves 2 nested loops) but I hope this solves your question

```
import pandas as pd
import datetime
import calendar

pd.set_option('display.max_rows', 100)

cols = ['day', 'month', 'year']

data = [
    ['1st Monday', 8, 2015],
    ['3rd Tuesday', 12, 2017],
    ['4th Wednesday', 5, 2019]
]

df = pd.DataFrame(data=data, columns=cols)
df['week_number'] = df['day'].str.slice(0, 1)
df['week_number'] = df['week_number'].astype('int')
df['day_name'] = df['day'].str.slice(4)

def generate_dates(input_df, index_num):
    _, days = calendar.monthrange(input_df.loc[index_num, 'year'], input_df.loc[index_num, 'month'])
    df_dates = pd.DataFrame()
    for i in range(1, days + 1):
        df_dates.loc[i - 1, 'date'] = datetime.date(input_df.loc[index_num, 'year'], input_df.loc[index_num, 'month'],
                                                    i)
        df_dates.loc[i - 1, 'year'] = input_df.loc[index_num, 'year']
        df_dates.loc[i - 1, 'days'] = calendar.weekday(input_df.loc[index_num, 'year'],
                                                       input_df.loc[index_num, 'month'], i)
        df_dates.loc[i - 1, 'day_name'] = df_dates.loc[i - 1, 'date'].strftime("%A")

    df_dates['week_number'] = 1
    df_dates['week_number'] = df_dates.groupby('day_name')['week_number'].cumsum()
    return df_dates

dates = pd.DataFrame(columns=['date', 'year', 'days', 'day_name', 'week_number'])
for row in df.index:
    dates = pd.concat([dates, generate_dates(df, row)])

df2 = df.merge(dates, on=['year', 'day_name', 'week_number'])

print(df2)

```