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

我有一个这样的数据框(时间戳只包含从 9:00 到 20:00)


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

...

?   2020-07-20 12:00:00

Name: Time, dtype: datetime64[ns]

我有几天的清单(在“incomplete_days”中)我想在 df 中排除


0    2020-05-18

1    2020-05-19

3    2020-05-21

4    2020-05-22

5    2020-05-23

6    2020-05-24


Name: 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-18

1         2020-05-18

2         2020-05-18

3         2020-05-18

4         2020-05-18

             ...    

110077    2020-08-02

110078    2020-08-02

110079    2020-08-02

110080    2020-08-02

110081    2020-08-02

Name: 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

我看到它非常接近但出了点问题..


倚天杖
浏览 49回答 1
1回答

牛魔王的故事

假设您有两个数据框df,并且df1它们的列采用日期时间格式:df    Date0   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:005   2020-07-20 12:00:00df1    incomplete_days0   2020-05-181   2020-05-193   2020-05-214   2020-05-225   2020-05-236   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]: Date5 2020-07-20 12:00:00
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python