Skip to content

Faster dataframe construction #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dist
**/wheelhouse/*
# coverage
.coverage
.testmondata
.pytest_cache
.nox

# OS generated files #
Expand Down
10 changes: 10 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

.. _changelog-0.6.1:

0.6.1 / [unreleased]
--------------------

- Improved ``read_gbq`` performance and memory consumption by delegating
``DataFrame`` construction to the Pandas library, radically reducing
the number of loops that execute in python
(:issue:`128`)

.. _changelog-0.6.0:

0.6.0 / 2018-08-15
Expand Down
38 changes: 19 additions & 19 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import os
import time
import warnings
from collections import OrderedDict
from datetime import datetime

import numpy as np
from pandas import DataFrame
from pandas.compat import lzip

from pandas_gbq.exceptions import AccessDenied


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -444,29 +443,30 @@ def _get_credentials_file():
'PANDAS_GBQ_CREDENTIALS_FILE')


def _parse_data(schema, rows):
def _parse_schema(schema_fields):
# see:
# http://pandas.pydata.org/pandas-docs/dev/missing_data.html
# #missing-data-casting-rules-and-indexing
dtype_map = {'FLOAT': np.dtype(float),
'TIMESTAMP': 'M8[ns]'}

fields = schema['fields']
col_types = [field['type'] for field in fields]
col_names = [str(field['name']) for field in fields]
col_dtypes = [
dtype_map.get(field['type'].upper(), object)
if field['mode'].lower() != 'repeated'
else object
for field in fields
]
page_array = np.zeros((len(rows),), dtype=lzip(col_names, col_dtypes))
for row_num, entries in enumerate(rows):
for col_num in range(len(col_types)):
field_value = entries[col_num]
page_array[row_num][col_num] = field_value

return DataFrame(page_array, columns=col_names)
for field in schema_fields:
name = str(field['name'])
if field['mode'].upper() == 'REPEATED':
yield name, object
else:
dtype = dtype_map.get(field['type'].upper(), object)
yield name, dtype


def _parse_data(schema, rows):

column_dtypes = OrderedDict(_parse_schema(schema['fields']))

df = DataFrame(data=(iter(r) for r in rows), columns=column_dtypes.keys())
for column in df:
df[column] = df[column].astype(column_dtypes[column])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need the astype? I recall that when I dropped it last time I encountered #174. A behavior change like that would require a version bump, though, so I'm happy with keeping this a 0.6.1 release if you'd prefer to get this out as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point re int with nulls. I'll add a test and see what happens.

I think we do need this for dates; otherwise they'll be str

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there's already a test: https://github.com/max-sixty/pandas-gbq/blob/no-python-loop/tests/system/test_gbq.py#L165

What's happening: pandas is ignoring setting a column with NaN to int, and keeping it as object

return df


def read_gbq(query, project_id=None, index_col=None, col_order=None,
Expand Down
16 changes: 4 additions & 12 deletions tests/system/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

from pandas_gbq import gbq


TABLE_ID = 'new_test'


def _get_dataset_prefix_random():
return ''.join(['pandas_gbq_', str(randint(1, 100000))])


@pytest.fixture(autouse=True, scope='module')
def _test_imports():
def test_imports():
try:
import pkg_resources # noqa
except ImportError:
Expand Down Expand Up @@ -143,13 +141,6 @@ def setup(self, project, credentials):
project, private_key=credentials)
self.credentials = credentials

def test_should_properly_handle_valid_strings(self, project_id):
query = 'SELECT "PI" AS valid_string'
df = gbq.read_gbq(query, project_id=project_id,
private_key=self.credentials,
dialect='legacy')
tm.assert_frame_equal(df, DataFrame({'valid_string': ['PI']}))

def test_should_properly_handle_empty_strings(self, project_id):
query = 'SELECT "" AS empty_string'
df = gbq.read_gbq(query, project_id=project_id,
Expand Down Expand Up @@ -392,7 +383,8 @@ def test_bad_project_id(self):
with pytest.raises(gbq.GenericGBQException):
gbq.read_gbq('SELCET * FROM [publicdata:samples.shakespeare]',
project_id='not-my-project',
private_key=self.credentials)
private_key=self.credentials,
dialect='legacy')

def test_bad_table_name(self, project_id):
with pytest.raises(gbq.GenericGBQException):
Expand Down Expand Up @@ -427,7 +419,7 @@ def test_zero_rows(self, project_id):
('is_bot', np.dtype(bool)), ('ts', 'M8[ns]')])
expected_result = DataFrame(
page_array, columns=['title', 'id', 'is_bot', 'ts'])
tm.assert_frame_equal(df, expected_result)
tm.assert_frame_equal(df, expected_result, check_index_type=False)

def test_legacy_sql(self, project_id):
legacy_sql = "SELECT id FROM [publicdata.samples.wikipedia] LIMIT 10"
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
except ImportError: # pragma: NO COVER
from unittest import mock

pytestmark = pytest.mark.filter_warnings(
"ignore:credentials from Google Cloud SDK")


@pytest.fixture
def min_bq_version():
Expand Down