hyper-api-samples icon indicating copy to clipboard operation
hyper-api-samples copied to clipboard

Request: Column Names Added to Results Object

Open successfulmike opened this issue 3 years ago • 4 comments

Request: I'd like to request the connection.execute_query function to automatically output the column names to the Results object created. These should be the list of column names from submitted query (all columns if SELECT * used)

Purpose: Automatically creates column names for Results object (dataframe), which makes it easier for reference later on if needed.

Alternative (in case of technical limitation):

  1. create empty list
  2. iterate over list of columns for desired table using <table_obj_name>.columns 2.a.) extract column name using name method from object 2.b.) convert returned column name to string (using __str__() ), because it's originally returned as tabledefinition object 2.c.) append to list
  3. use connection.execute_query function for desired query that creates Results object, and then pass completed list of columns from 2.c. above as argument for columns when dataframe is created

Screenshot attached with example image

successfulmike avatar May 26 '22 13:05 successfulmike

Hi @successfulmike,

The results object contains a schema which holds the columns.

You can access it via result.schema.columns

Please let me know if this solves your issue.

Cheers Jonas

jonas-eckhardt avatar May 27 '22 15:05 jonas-eckhardt

@successfulmike for integration with pandas, you might want to look into pantab.

https://pantab.readthedocs.io/en/latest/examples.html contains a couple of usage examples

vogelsgesang avatar May 27 '22 15:05 vogelsgesang

@jonas-eckhardt , though that suggestion is valid, it still returns a TableDefinition object like shown in the example in my screenshot so you'd still need to perform transformation to extract column name and convert to string type.

The good thing about your approach though is that we don't need to worry about figuring out what the returned list of columns from the queries would be, so I guess that's certainly a plus. Thanks for sharing.

successfulmike avatar May 30 '22 17:05 successfulmike

@successfulmike does pantab (as suggested by Adrian) solve the issue?

jonas-eckhardt avatar May 31 '22 07:05 jonas-eckhardt

solving based on inactivity; in case the issue wasn't resolved, yet, feel free to reopen

vogelsgesang avatar Mar 15 '23 13:03 vogelsgesang