Skip to content

Commit

Permalink
imports pandas at module level and raises exception in to_dataframe()…
Browse files Browse the repository at this point in the history
… if it isn't installed
  • Loading branch information
alixhami committed Nov 10, 2017
1 parent 70d6bd9 commit 4bc85fd
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import six
from six.moves import http_client
try:
import pandas
except ImportError:
pandas = None

import google.api_core.future.polling
from google.cloud import exceptions
Expand Down Expand Up @@ -1957,14 +1961,20 @@ def to_dataframe(self):
and column headers from the query results. The column
headers are derived from the destination table's
schema.
:raises: :exc:`ValueError` if the ``pandas`` library cannot be
imported.
"""
import pandas as pd
if pandas is None:
raise ValueError('The pandas library is not installed, please '
'install pandas to use the to_dataframe() '
'function.')

iterator = self.result()
column_headers = [field.name for field in iterator.schema]
rows = [row.values() for row in iterator]
query_results = self.result()
column_headers = [field.name for field in query_results.schema]
rows = [row.values() for row in query_results]

return pd.DataFrame(rows, columns=column_headers)
return pandas.DataFrame(rows, columns=column_headers)


class QueryPlanEntryStep(object):
Expand Down

0 comments on commit 4bc85fd

Please sign in to comment.