Home » Python » sql – I get the following error on SQLAlchemy, Python: Length mismatch: Expected axis has 0 elements, new values have 5 elements-Exceptionshub

sql – I get the following error on SQLAlchemy, Python: Length mismatch: Expected axis has 0 elements, new values have 5 elements-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have two tables, one with around 9971581 rows, the other being only 6628 rows representing only a date column. I want to get rows from the first table that have a date time matching with the dates in the second table.

PS: If there is any other alternatives with Pandas or anything else, then I’m okay with it as long as it would be fast.

query = engine.execute(
    "SELECT * FROM my_table WHERE 'DateTime' IN (SELECT '0' FROM truedate) ORDER BY 5")
raw = pd.DataFrame(query.fetchall())
raw.columns = query.keys()
raw

ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements
How to&Answers:

Your query currently isn’t returning anything.
You’re also trying to match on two strings:

  • the string ‘DateTime’, and
  • the string ‘0’.

Change your query to use an inner join:

query = engine.execute("""SELECT my_table.* 
                          FROM my_table 
                              JOIN truedate 
                              ON my_table.DateTime = truedate.your_date_column
                          ORDER BY 5;"""