I scraped tweets using Snscrape and now trying to filter the dates with pandas. I converted the column to Datetime but I'm getting a KeyError
:
df['Datetime'] = pd.to_datetime(df['Datetime'], format = '%Y-%m-%d').dt.date
startdate = pd.to_datetime('2022-02-24').date()
enddate = pd.to_datetime('2022-03-03').date()
df.loc[startdate:enddate]
TypeError: '<' not supported between instances of 'int' and 'datetime.date'
How do I fix this?
CodePudding user response:
First, parse the dates (noticed I've omitted .dt.date
at the end):
df['Datetime'] = pd.to_datetime(df['Datetime'], format = '%Y-%m-%d')
Then you can use .between
instead of .loc
for this:
filtered = df[df['Datetime'].between('2022-02-24', '2022-03-03')]
CodePudding user response:
You need to filter using the 'Datetime' column as follows:
filtered_df = df.loc[(df['Datetime'] >= startdate) & (df['Datetime'] <= enddate)]