Home > database >  only convert to date cells with data
only convert to date cells with data

Time:02-25

I have a data frame with dates and missing dates:

date
2022-02-02
2022-02-03
-
-

I need to convert to date only the ones different from '-', I'm using .loc for this but is not working:

df.loc[oppty['date'] != '-', 'date'] = pd.to_datetime(df['date'])

in parse raise ParserError("String does not contain a date: %s", timestr) dateutil.parser._parser.ParserError: String does not contain a date: -

CodePudding user response:

Will this work?

df1 = pd.DataFrame({'date':['2022-02-02', '2022-02-03', '-','-']})
df1
pd.to_datetime(df1['date'], errors='coerce')

enter image description here

if you want to keep '-' change 'coerce' to 'ignore'

enter image description here

  • Related