1

I need to drop rows where column X value is a datetime.

I have tried this

from pandas.api.types import is_datetime64_any_dtype as is_datetime
indexNames = df[is_datetime(df['Column X'])].index
df.drop(indexNames, inplace=True)

but it returns

KeyError: False

This below doesnt work either (no error)

indexNames = df[df['Column X'] == is_datetime(df['Column X'])].index

I have still values like this datetime.datetime(2016, 6, 29, 8, 24, 19)

Ive found a related question but inversed Pandas delete all rows that are not a 'datetime' type

have you any idea please ?

1 Answers1

1

Use to_datetime with errors='coerce' for convert non datetimelike to NaNs, so filter by Series.isna in boolean indexing:

df = df[pd.to_datetime(df['Column X'], errors='coerce').isna()]

But sometimes pandas recognise some integers like 2000 for datetimes, so if possible more accurate is specify format of datetimes, e.g. here YYYY-MM-DD:

df = df[pd.to_datetime(df['Column X'], errors='coerce', format='%Y-%m-%d').isna()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252