1

I have a dataframe

    colA                      ColB
0   2020-11-30 00:00:00       546
1   Vendor account statement  yrt22
2   :doflat:                  ttys7
3   2020-1-30 00:00:00        970

I want to filter 'colA' dates only (any date) other then dates not required. Required dataframe is as below.

    colA                      ColB
0   2020-11-30 00:00:00       546
1   2020-1-30 00:00:00        970

3 Answers 3

2

If need datetimes in output replace non datetimes no misisng values and rmeove rows:

df['colA'] = pd.to_datetime(df['colA'], errors='coerce')
df = df.dropna(subset=['colA'])

If need original values test for values possible converted, so here not missing values:

df = df[pd.to_datetime(df['colA'], errors='coerce').notna()]
Sign up to request clarification or add additional context in comments.

Comments

2

You may use:

df['colA'].str.contains('^\d+-\d+-\d+ \d{2}:\d{2}:\d{2}$')

The regex pattern ^\d+-\d+-\d+ \d{2}:\d{2}:\d{2}$ should match the datetime/timestamp values shown which you want to retain, see the demo:

Demo

Comments

1

try this:

df[[type(i)==pd.Timestamp for i in df['colA']]]

1 Comment

We're just creating a Boolean list by comparing each element type with timestamp using list comprehension . And we're finally filtering the df only for True outcomes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.