为了账号安全,请及时绑定邮箱和手机立即绑定

删除不同时间分辨率的天数列表(分钟数据)

删除不同时间分辨率的天数列表(分钟数据)

倚天杖 2023-04-25 16:29:30
我有一个这样的数据框(时间戳只包含从 9:00 到 20:00)0   2020-05-18 10:18:001   2020-05-18 10:19:002   2020-05-18 10:20:003   2020-05-18 10:21:004   2020-05-18 10:22:00...?   2020-07-20 12:00:00Name: Time, dtype: datetime64[ns]我有几天的清单(在“incomplete_days”中)我想在 df 中排除0    2020-05-181    2020-05-193    2020-05-214    2020-05-225    2020-05-236    2020-05-24Name: Time, dtype: datetime64[ns]我简单地尝试过,df[df['Time'] != incomplete_days]但是,错误说ValueError: Can only compare identically-labeled Series objects我应该用天数列表制作一个时间戳(1 分钟分辨率)以在 df 中排除它们吗?如果是这样,我怎样才能在给定的日子里安排开始时间和结束时间?有什么办法可以让我不需要用 1 分钟的分辨率制作时间戳吗?(我已经从 20:01 到 08:59 删除了不相关的时间,并在 df 中保留了从 09:00 到 20:00 的时间。我不想再次使用要排除的天数列表制作每小时时间戳。我使用了以下变量来减少不相关的时间)start = time(6)end = time(20)-----编辑我做了df['Time'].dt.date给0         2020-05-181         2020-05-182         2020-05-183         2020-05-184         2020-05-18             ...    110077    2020-08-02110078    2020-08-02110079    2020-08-02110080    2020-08-02110081    2020-08-02Name: Time, Length: 69042, dtype: object和list_incomplete=incomplete_days.tolist()list_incomplete给[Timestamp('2020-05-18 00:00:00'), Timestamp('2020-05-19 00:00:00'), Timestamp('2020-05-21 00:00:00'), Timestamp('2020-05-22 00:00:00'), Timestamp('2020-05-23 00:00:00'), Timestamp('2020-05-24 00:00:00'), Timestamp('2020-05-25 00:00:00'), Timestamp('2020-05-26 00:00:00'), Timestamp('2020-05-27 00:00:00'), Timestamp('2020-05-28 00:00:00'), Timestamp('2020-05-29 00:00:00'), Timestamp('2020-05-30 00:00:00'), Timestamp('2020-05-31 00:00:00'), Timestamp('2020-06-01 00:00:00'), Timestamp('2020-06-02 00:00:00'), Timestamp('2020-06-03 00:00:00'), Timestamp('2020-06-10 00:00:00'), Timestamp('2020-07-02 00:00:00'), Timestamp('2020-07-05 00:00:00'), Timestamp('2020-07-06 00:00:00')]当我做df.drop([df['Time'].dt.date not in incomplete_days],inplace=True)我收到以下错误。TypeError: 'Series' objects are mutable, thus they cannot be hashed我看到它非常接近但出了点问题..
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

假设您有两个数据框df,并且df1它们的列采用日期时间格式:


df


    Date

0   2020-05-18 10:18:00

1   2020-05-18 10:19:00

2   2020-05-18 10:20:00

3   2020-05-18 10:21:00

4   2020-05-18 10:22:00

5   2020-07-20 12:00:00

df1


    incomplete_days

0   2020-05-18

1   2020-05-19

3   2020-05-21

4   2020-05-22

5   2020-05-23

6   2020-05-24

您可以使用布尔索引并将两列转换为具有相同格式的字符串以进行比较。使用~with isin(实际上是“不在”)而不是!=. 您不能用于!=将行与整个系列进行比较,因此您当前的方法是语法错误。在布尔索引中转换格式[]将保持数据框的初始格式,并且不会从日期更改为字符串。


df = df[~(df['Date'].dt.strftime('%Y-%m-%d').isin(df1['incomplete_days'].dt.strftime('%Y-%m-%d')))]


Out[38]: 

Date

5 2020-07-20 12:00:00


查看完整回答
反对 回复 2023-04-25
  • 1 回答
  • 0 关注
  • 48 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信