I have a similar case, but apparently solutions in this post do not work for me. I will be grateful for any hints?
I have a custom class for SQLAlchemy connection:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class RevenueForecast(db.Model):
__tablename__ = 'revenue_forecast'
id = db.Column(db.Integer(), primary_key=True)
manager = db.Column(db.String(100), nullable=False)
client_name = db.Column(db.String(200), nullable=False)
job_description = db.Column(db.String(200), nullable=False)
jul = db.Column(db.Integer)
aug = db.Column(db.Integer)
sep = db.Column(db.Integer)
oct = db.Column(db.Integer)
nov = db.Column(db.Integer)
dec = db.Column(db.Integer)
jan = db.Column(db.Integer)
feb = db.Column(db.Integer)
mar = db.Column(db.Integer)
apr = db.Column(db.Integer)
may = db.Column(db.Integer)
jun = db.Column(db.Integer)
is_active = db.Column(db.Boolean)
I read the data in very simple way not to create engine, etc.:
forecasts = RevenueForecast.query.all()
I would like to conver it to a DataFrame. Wanted to use from_records as suggested in this thread:
forecast_data = pd.DataFrame.from_records(
forecasts,
index = 'id'
)
print(df_from_records.head(5))
However, this prodcues error:
TypeError: 'RevenueForecast' object is not iterable
What works is the following (columns is simply a list of columns in a class):
forecast_data = pd.DataFrame()
for i in columns:
br = 'forecast_data[i] = [k.%s for k in forecasts]' % i
exec(br)
Still, I would like ot use pandas to convers list of classes from SQLAlchemy to a DataFrame (more error-proof). Where do I make a mistake? Should I just make RevenueForecast class iterable and that's it?