-1

Trying to figure out what I'm doing wrong, new to python and not sure how to solve my AssertionError. If the date in column "sa_death_date" is less than or =to 2020, column "Final Abstraction? Change to 2 if yes." needs to be changed to 2, is greater than 2021, should be 0

Thoughts?

import pandas as pd

import datetime

df=pd.read_csv("C:\\Users\\nm236211\\Desktop\OctoberConsented.DI.REDCap_API.csv")

df.dropna(subset = ["sa_death_date"], inplace=True)
 
df["sa_death_date"] = pd.to_datetime(df['sa_death_date'])

df.sa_death_date.apply(str)

df.loc[df["sa_death_date"]<= pd.to_datetime(2020,1,1),"Final Abstraction? Change to 2 if yes."] = 2
df.loc[df["sa_death_date"]>= pd.to_datetime(2020,12,31),"Final Abstraction? Change to 2 if yes."] = 0 

print(df)
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Tara R
  • 3
  • 1

1 Answers1

0

Change the two places of pd.to_datetime(y,m,d) to

pd.datetime(y,m,d)

which is the correct datetime object constructor.

Bill Huang
  • 4,491
  • 2
  • 13
  • 31