Home > Mobile >  try convert string to date row per row in pandas or similar
try convert string to date row per row in pandas or similar

Time:08-24

I need to join dataframes with dates in the format '%Y%m%d'. Some data is wrong or missing and when I put pandas with:

try: df['data'] = pd.to_datetime(df['data'], format='%Y%m%d')
except: pass

If 1 row is wrong, it fails to convert the whole column. I would like it to skip only the rows with error without converting.

I could solve this by lopping with datetime, but my question is, is there a better solution for this with pandas?

CodePudding user response:

Pass errors = 'coerce' to pd.to_datetime to convert the values with wrong date format to NaT. Then you can use Series.fillna to fill those NaT with the input values.

df['data'] = (
    pd.to_datetime(df['data'], format='%Y%m%d', errors='coerce')
      .fillna(df['data'])
)

From the docs

errors : {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’

  • If 'raise', then invalid parsing will raise an exception.
  • If 'coerce', then invalid parsing will be set as NaT.
  • If 'ignore', then invalid parsing will return the input.
  • Related