title: getting bound Selectable from ORM query
body: <p>I'm trying to use <a href="https://github.com/tomasd/pandas-sqlalchemy-pivot" rel="nofollow">pandas-sqlalchemy-pivot</a> to create pivot tables based on SQLAlchemy ORM query results.</p>
  
  <p>The functions of this module (e.g. <code>pivots.pivot_table_from_select</code>) are designed to work with SQLAlchemy <a href="http://docs.sqlalchemy.org/en/latest/core/selectable.html" rel="nofollow">core selectables</a>, rather than with ORM queries.</p>
  
  <p>I figured I could use the <a href="http://docs.sqlalchemy.org/en/rel_0_9/orm/query.html#sqlalchemy.orm.query.Query.selectable" rel="nofollow">Query.selectable</a> attribute to convert the ORM query to a core selectable object, but this gives an error:</p>
  
  <pre><code>&gt;&gt;&gt; q=session.query( ... )
  &gt;&gt;&gt; df = pivots.pivot_table_from_select(q.selectable(), rows="field", cols="state", values="count")
    File "...\site-packages\pivots\table.py", line 25, in pivot_table_from_select
      data = pivot_data(select, rows, cols, values)
    File "...\site-packages\pivots\table.py", line 74, in pivot_data
      data = pivot_select.execute(bind=select.bind).fetchall()
    File "...\lib\site-packages\sqlalchemy\sql\base.py", line 385, in execute
      raise exc.UnboundExecutionError(msg)
  sqlalchemy.exc.UnboundExecutionError: This Select object is not directly bound to a Connection or Engine.Use the .execute() method of a Connection
  Engine to execute this construct.
  </code></pre>
  
  <p>I can't figure out why this object is unbound, since the original <code>Query</code> object is certainly bound to a specific session and engine.</p>
  
  <p>I get the same error if I try to execute the query, even with an explicit bind:</p>
  
  <pre><code>&gt;&gt;&gt; q.selectable.execute(bind=engine)
  </code></pre>
  
  <p>... but if I do this instead, it works:</p>
  
  <pre><code>&gt;&gt;&gt; engine.execute(q.selectable)
  </code></pre>
  
  <p>I'd like to be able to use the unmodified <code>pandas-sqlalchemy-pivot</code> module; is there a way to get a bound selectable from the query object which I'm overlooking?</p>