CanopyTax/asyncpgsa

Any convenient way to get the column names from result?

DeoLeung opened this issue · 3 comments

I want to turn the result into pandas.DataFrame, so I need an easy way to get the values as list of list and keys as list
currently I can only do

result = await conn.fetch(expression)
df = pd.DataFrame(list(dict(i) for i in result))

which seems not very efficient, I would like something

result = await conn.fetch(expression)
df = pd.DataFrame(result.values(), columns=result.keys())

to avoid turning into a dict for every record

thx

have you tried using the data property? A result should have the data property to get the raw data. (list of dictionaries)

So

result = await conn.fetch(expression)
df = pd.DataFrame(result.data, columns=result.data[0].keys())

should work.

There is a possibility to remove the wrapping around data, as it seams to frustrate most people. So later versions of asyncpgsa might just be result is the list of dictionaries instead of a "RecordGenerator" object. (TBD)

After #50 there is no .data anymore and fetch returns a list of asyncpg.Record objects.

Also imho you should get columns list out of your expression instead of results, e.g.:

query = SomeTable.select()
columns = list(map(str, query.columns)) # query.columns is a sqlalchemy.sql.base.ImmutableColumnCollection object

This also works fine if you make your queries through sa.select([col1, col2, col3]).

The values list can be fetched as values = [r.values() for r in result].

closing as no recent comments from OP. Feel free to comment on to reopen.