我有一個這樣的數據框(時間戳只包含從 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
添加回答
舉報
0/150
提交
取消