The _metadata attribute of the Pandas DataFrame was removed in version 1.3.0, which is why you are receiving a "list indices must be integers or slices, not str" error. The approach I suggested in my previous response would only work for Pandas versions earlier than 1.3.0.
In newer versions of Pandas, you can still access the compiled SQL statement by using the query attribute of the sqlalchemy.engine.ResultProxy object that is returned by pandas.read_sql_query. Here's an example:
import pandas as pd
from sqlalchemy import create_engine
# create a SQLAlchemy engine
engine = create_engine('postgresql://user:password@host:port/database')