I am having problems filtering a pandas dataframe - I want to filter all the dates in the "Date" column to return only dates in the last month, but python is mixing up the date and month and returning the wrong answer. Could you let me know how to fix thanks
import pandas as pd
import numpy as np
import datetime as dt
df = pd.DataFrame(pd.read_csv("Dates.csv"))
tday = dt.date.today()
tdelta = dt.timedelta(days=-30)
dt = tday + tdelta
dt = np.datetime64(dt)
print(dt)
df['Date'] = pd.to_datetime(df['Date'])
df = df.loc[df['Date'] >= dt]
print(df)
Dates.csv
df is:
DF =
Met By Date
0 David 06/07/2020
1 Philip 22/06/2020
2 Richard 02/04/2020
3 Richard 09/03/2020
4 Richard 17/02/2020
5 Philip 12/02/2020
6 Philip 29/01/2020
7 Jane 20/01/2020
8 Jane 13/01/2020
9 Richard 10/01/2020
10 Philip 06/01/2020
UPDATE: solved by VALDI - must include dayfirst = True on read_csv
import pandas as pd
df = pd.read_csv("Dates.csv", parse_dates=[1], dayfirst = True)
df = df[df['Date'] > pd.Timestamp.today() - pd.Timedelta('30D')]
print(df)