Search code examples
pythondatecomparestring-to-datetime

Comparing date column of the csv file to today date


I need to compare my large data's date with today date to calculate another column value if the date is more than or less than today.

My code is as below however I'm keep getting error!

I have tried Timestamp.now() but my python 3.6 can not recognise it.

import datetime
def midspread_calcs():
    for row in df_midspread:
        if df_midspread['Date']<datetime.datetime.now():
           df_midspread['Midspread']= df_midspread.Oldrate*df_midspread.Value
        else:
           df_midspread['Midspread']= df_midspread.Newrate*df_midspread.Value 

result = midspread_calcs()

Solution

  • Thanks for your advice,It works now :) for row in df_midspread['Date']:

        if row<datetime.datetime.now():
           df_midspread['Midspread']= df_midspread.Oldrate*df_midspread.Value
        else:
           df_midspread['Midspread']= df_midspread.Newrate*df_midspread.Value