Skip to content

ENH: adds ability to generate bq schema from df #8915

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 1 commit into from
Dec 3, 2014
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
8 changes: 8 additions & 0 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,14 @@ data quickly, but it is not a direct replacement for a transactional database.
You can access the management console to determine project id's by:
<https://code.google.com/apis/console/b/0/?noredirect>

As of 0.15.2, the gbq module has a function ``generate_bq_schema`` which
will produce the dictionary representation of the schema.

.. code-block:: python

df = pandas.DataFrame({'A': [1.0]})
gbq.generate_bq_schema(df, default_type='STRING')

.. warning::

To use this module, you will need a valid BigQuery account. See
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Enhancements
- ``Timedelta`` arithmetic returns ``NotImplemented`` in unknown cases, allowing extensions by custom classes (:issue:`8813`).
- ``Timedelta`` now supports arithemtic with ``numpy.ndarray`` objects of the appropriate dtype (numpy 1.8 or newer only) (:issue:`8884`).
- Added ``Timedelta.to_timedelta64`` method to the public API (:issue:`8884`).
- Added ``gbq.generate_bq_schema`` function to the gbq module (:issue:`8325`).

.. _whatsnew_0152.performance:

Expand Down
28 changes: 28 additions & 0 deletions pandas/io/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,31 @@ def to_gbq(dataframe, destination_table, project_id=None, chunksize=10000,
dataset_id, table_id = destination_table.rsplit('.',1)

connector.load_data(dataframe, dataset_id, table_id, chunksize, verbose)

def generate_bq_schema(df, default_type='STRING'):
""" Given a passed df, generate the associated big query schema.

Parameters
----------
df : DataFrame
default_type : string
The default big query type in case the type of the column
does not exist in the schema.
"""

type_mapping = {
'i': 'INTEGER',
'b': 'BOOLEAN',
'f': 'FLOAT',
'O': 'STRING',
'S': 'STRING',
'U': 'STRING',
'M': 'TIMESTAMP'
}

fields = []
for column_name, dtype in df.dtypes.iteritems():
fields.append({'name': column_name,
'type': type_mapping.get(dtype.kind, default_type)})

return {'fields': fields}
11 changes: 11 additions & 0 deletions pandas/io/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ def test_google_upload_errors_should_raise_exception(self):
with tm.assertRaises(gbq.UnknownGBQException):
gbq.to_gbq(bad_df, 'pydata_pandas_bq_testing.new_test', project_id = PROJECT_ID)

def test_generate_bq_schema(self):

df = tm.makeMixedDataFrame()
schema = gbq.generate_bq_schema(df)

test_schema = {'fields': [{'name': 'A', 'type': 'FLOAT'},
{'name': 'B', 'type': 'FLOAT'},
{'name': 'C', 'type': 'STRING'},
{'name': 'D', 'type': 'TIMESTAMP'}]}

self.assertEqual(schema, test_schema)

@classmethod
def tearDownClass(cls):
Expand Down