У меня есть таблица postgres с содержанием, как показано ниже
date | metric | device | mse
------------+-------------+---------------+---------------------
2018-04-02 | cpu | 10.17.100.1 | 10.8353316326531
2018-04-03 | cpu | 10.17.100.1 | 1.84977070342026
2018-04-04 | cpu | 10.17.100.1 | 6.94701678240741
2018-04-05 | cpu | 10.17.100.1 | 8.38619212962963
2018-04-06 | cpu | 10.17.100.1 | 2.468402069161
2018-04-07 | cpu | 10.17.102.1 | 7.38240908709163
2018-04-08 | cpu | 10.17.102.1 | 11.7493083375325
2018-04-02 | mem | 10.17.102.1 | 3.12962962962963
2018-04-03 | mem | 10.17.102.1 | 6.25925925925926
2018-04-04 | mem | 10.17.102.1 | 3.5697337962963
2018-04-05 | mem | 10.17.102.1 | 0.440104166666667
2018-04-06 | mem | 10.17.102.1 | 0
2018-04-07 | mem | 10.17.102.1 | 28.1666666666667
2018-04-08 | mem | 10.17.102.1 | 28.1666666666667
2018-04-02 | sap_drops | 10.17.102.1 | 0.25067507558579
2018-04-03 | sap_drops | 10.17.102.1 | 0.240537426776266
2018-04-04 | sap_drops | 10.17.102.1 | 0.137486181972789
2018-04-05 | sap_drops | 10.17.102.1 | 0.325178394274376
2018-04-06 | sap_drops | 10.17.102.1 | 0.227994614512472
2018-04-07 | sap_drops | 10.17.102.1 | 0.231201367892836
2018-04-08 | sap_drops | 10.17.102.1 | 0.190689097012262
Как вы можете видеть, в таблице есть несколько экземпляров даты 2018-04-08
. Я хочу удалить из таблицы каждый экземпляр 2018-04-08
и оставить все как есть.
Ниже приведена структура таблицы
Table "public.device_mse"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+-----------+----------+--------------+-------------
date | date | | plain | |
metric | character varying(255) | | extended | |
device | character varying(255) | | extended | |
mse | double precision | | plain | |
Основываясь на ответе, предоставленном в этом вопросе Исключить день из диапазона дат Pandas Dataframe , я пытался сделать что-то подобное
import pandas as pd
import psycopg2
import pandas.io.sql as psql
import datetime
conn = psycopg2.connect(database="metrics", user="souvik", password="********", host="localhost", port="5432")
cur = conn.cursor()
df = psql.read_sql("Select * from device_mse", conn)
df2 = df["date" != datetime.datetime.strptime('2018-04-08', '%Y-%m-%d').date()]
print(df2)
Но я получаю сообщение об ошибке, как показано ниже
Traceback (most recent call last):
File "/home/souvik/utorapp/lib/python3.5/site-packages/pandas/core/indexes/base.py", line 2525, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: True
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/souvik/PycharmProjects/Pandas/compare_stats20.py", line 46, in <module>
df2 = df["date" != datetime.datetime.strptime('2018-04-08', '%Y-%m-%d').date()]
File "/home/souvik/app/lib/python3.5/site-packages/pandas/core/frame.py", line 2139, in __getitem__
return self._getitem_column(key)
File "/home/souvik/app/lib/python3.5/site-packages/pandas/core/frame.py", line 2146, in _getitem_column
return self._get_item_cache(key)
File "/home/souvik/app/lib/python3.5/site-packages/pandas/core/generic.py", line 1842, in _get_item_cache
values = self._data.get(item)
File "/home/souvik/app/lib/python3.5/site-packages/pandas/core/internals.py", line 3843, in get
loc = self.items.get_loc(item)
File "/home/souvik/app/lib/python3.5/site-packages/pandas/core/indexes/base.py", line 2527, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: True
Что я делаю не так?