【发布时间】:2022-09-23 19:23:25
【问题描述】:
我有下面的数据框,想按时间过滤。当我使用 dtypes 时,时间列作为一个对象出现。
为了获得时间用作过滤条件,我使用拆分:
start_time = \"25 September 2022, 13:00:00\"
split_time = start_time.split(\", \")[1]
我尝试将 split_time 和 df 列转换为 datime 但在 df 列转换时出现错误:
TypeError: <class \'datetime.time\'> 不能转换为日期时间
我也尝试过简单的字符串搜索,但这不会返回任何结果。
我已经能够使用以下方法按日期过滤:
split_date = start_time.split(\", \")[0]
event_date = datetime.strptime(split_date, \"%d %B %Y\")
events_df[\'start_date\'] = pd.to_datetime(events_df[\'start_date\'])
filtered_df = events_df.loc[(events_df[\'start_date\'] == event_date)]
但似乎无法在时间上做同样的事情。任何人都可以看到问题吗?
谢谢
| fixture_id | name | start_date | time | |
|---|---|---|---|---|
| 145 | 9394134 | Plymouth Argyle v Ipswich Town | 2022-09-25 00:00:00 | 12:30:00 |
| 146 | 9694948 | Grays Athletic v Merstham FC | 2022-09-25 00:00:00 | 13:00:00 |
| 147 | 9694959 | FC Romania v Faversham Town | 2022-09-25 00:00:00 | 15:00:00 |