I'm trying the below code, but I get an error when fetching data to a dataframe. I think this is because I declared the table as class:
def get_task_products(task_id):
before = time.time()
session = get_new_session()
persisted_task_products =session.query(TaskProducts). \
filter(TaskProducts.task_id == task_id). \
options(load_only(TaskProducts.product_id)).all()
df = pd.DataFrame(persisted_task_products)
df.columns = persisted_task_products[0].keys()
print_debug(
'db_manager.persisted_motionlesstime: query result (after {0} seconds) : {1})'. \
format(time.time() - before, df))
if not df or len(df) != 0:
return df
session.close()
The table declaration:
class TaskProducts(Base):
__tablename__ = 'task_products'
id = Column(BigInteger, autoincrement=True, primary_key=True)
task_id = Column(Integer, nullable=False)
product_id = Column(Integer, nullable=False)
def __repr__(self):
return """
<TaskProducts
(
id=%s, task_id=%s, product_id=%s
)>""".replace(" ", "").replace("\n", " ") % (
self.id, self.task_id, self.product_id
)
but I got an error
df.columns = persisted_task_products[0].keys()
AttributeError: 'TaskProducts' object has no attribute 'keys'
How could I solve this issue?