Pandas 日期时间键错误过滤
我使用 Snscrape 抓取推文,现在尝试使用 pandas 过滤日期。我将该列转换为日期时间,但收到 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'
如何修复此问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用“日期时间”列进行过滤,如下所示:
You need to filter using the 'Datetime' column as follows:
首先,解析日期(注意到我在末尾省略了
.dt.date
):然后您可以使用
. Between
而不是.loc
> 为此:First, parse the dates (noticed I've omitted
.dt.date
at the end):Then you can use
.between
instead of.loc
for this: