prestodb / presto-python-client

Python DB-API client for Presto

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide convenient way for loading query results into a data frame

kebab-mai-haddi opened this issue · comments

I want to create a data frame from hive using Presto. I am done with it but with one exception: there are empty strings in my data which would be NaN if the corresponding CSV file is read using Pandas (pd.read_csv()).

I read several documentations but none address this issue.

My code for creating a data frame from Presto is pretty straightforward:

def get_pandas_dataframe(self, hql, parameters=None):
        if not self.airflow_conn_reqd:
            import pandas
            cursor = self.presto_client.cursor()
            try:
                cursor.execute(self._strip_sql(hql), parameters)
                data = cursor.fetchall()
            except DatabaseError as e:
                raise PrestoException(self._get_pretty_exception_message(e))
            column_descriptions = cursor.description
            if data:
                df = pandas.DataFrame(data)
                df.columns = [c[0] for c in column_descriptions]
            else:
                df = pandas.DataFrame()
            return df
        else:
            return(
                self.get_pandas_df(query)
            )

As you can see, there exists a line df = pandas.DataFrame(data). Now, I read the documentation of pandas.DataFrame() and could not find any method of keeping keep_default_na to True so that the empty string '' are treated as NaN as Pandas would treat them.

@avisrivastava254084 can you describe the schema of the tables you are reading and provide an example with rows from this table? In other words, could you describe the data that you're querying from Presto and the expected values in the pandas Dataframe.

@ggreg sorry for responding late, can we close this issue and instead discuss the main topic "Creating pandas data frame in presto client" instead? This issue

This is the repository for the Presto Python client. The other issue is in the Presto engine codebase.

@avisrivastava254084 Aviral, prestodb/presto-python-client (this repo) is a proper place for Python client enhancements. The other repo, prestodb/presto, contains code for the Presto itself. Are you thinking of making changes in the presto client or presto code?

@avisrivastava254084 Makes sense to me.

I'd like to avoid introducing a dependency to pandas. I was thinking of providing an interface to return the data in the right format.

@ggreg
I am telling you from my own experience, this library is in use and giving the data scientists the ability to create data frames is needed. Imagine being a data scientist and having rightful abstractions over S3(where data is stored), Hive(where meta is stored) and SQL(Presto, which is an engine).

Not only Pandas, we should also think of implementing the same for pyspark i.e. creating Spark data frame from Presto.

But I would like your input on this and your thought process, maybe I am having a bias.

@avisrivastava254084 i appreciate the value of support this. My main concern is to not conflate too many things into the Presto Python client library.

What do you think about providing these features in another library, called presto-python-common (i welcome better names too :) )?

I'm preparing the repo so it follows the requirements for an open source repository and allow to easily contribute new modules. I'll ping when it is ready.

Meanwhile how would we call the module to manipulate dataframes? Should we just call it prestodb.dataframe and provide a get(sql, **kwargs) -> pandas.DataFrame function?

Is there any progress on this? It would be incredibly helpful for my use cases.

@ggreg any update? I am still up for this.

@ggreg no longer works on the project. @mayankgarg1990 Do you know what might help here?

Thanks @mbasmanova !

@mayankgarg1990
As you can see, there is clearly a need for this. Let me know if you guys need some context and if time is an issue, we can fasten this up on a call or something.

hi at aviralsrivastava dot com