我有一个这样的数据帧(时间戳只包含从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]
我有一个几天的清单,我想在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
我看到距离很近,但出了点问题。。
假设有两个数据帧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
您可以使用布尔索引,并将两列转换为相同格式的字符串进行比较。使用~
和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