You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BigQuery: Load to table from dataframe without index #5572
changed the title [-][BigQuery] Load to table from dataframe always loads an index when using client.load_table_from_dataframe()[/-][+]BigQuery: Load to table from dataframe without index[/+]on Jul 5, 2018
@sungchun12 I just tried the solution @max-sixty posted above for the bigquery client api and it worked fine. Load the job configuration and override the schema as suggested.
The schema should be a list with the format:
from google.cloud import bigquery
schema=[bigquery.SchemaField('field1_name','field1_type'),...,bigquery.SchemaField('fieldn_name','fieldn_type')]
@mikeymezher , thanks for getting back to me! I'll try it out and let you know. Do you know if one is more performant over the other in your hands-on experience?
Haven't tested, but anecdotally I've noticed pandas-gbq to be faster than the client library. But there are instances where the client library is needed. Writing to partitioned tables for instance.
They are implementation details, but pandas-gbq uses CSV whereas google-cloud-bigquery uses parquet as the serialization format. The reason for this is to support STRUCT / ARRAY BigQuery columns (though these aren't supported in pandas, anyway).
Implementation-wise, I just noticed pandas provides a way to override the parquet engine's default behavior with an index argument. I'm open to adding a similar argument to google-cloud-bigquery.
@tswast , I would LOVE "index=False" functionality in the google-cloud-bigquery package as it would allow me to remove pandas-gbq imports and have a consistent API to work with bigquery...at least for my use cases in having to not build override schema configurations.
@cavindsouza not yet. Right now you can avoid writing indexes by passing in a job_config argument with the desired job_config.schema. I agree this would still be a useful feature.
FYI: #9064 and #9049 are changing the index behavior, as a schema will be automatically populated in more cases now.
We might actually have a need to explicitly add indexes to the table. Currently, it's inconsistent when an index will be added and when not. It depends on if the schema is populated and which parquet engine is used to serialized the DataFrame.
Preferred option
Check if index (or indexes if multi-index) name(s) are present in job_config.schema. If so, include the index(es) that are specified. If not, omit the indexes.
Edge case: What if index name matches that of a column name? Prefer serializing the column. Don't add the index in this case.
Alternative
Add a index=True / index=False option to the load_table_from_dataframe function.
This makes it when to include indexes. This would allow the index dtype to be used to automatically determine the schema in some cases.
When the index dtype is object, we'll need to add the index to the job_config.schema, anyway, so this requires the same implementation as the preferred option.
Update test_load_table_from_dataframe snippets to show overriding schema for columns whose types can't be autodetected. Explicitly add indexes to the partial schema as well.
Activity
[-][BigQuery] Load to table from dataframe always loads an index when using client.load_table_from_dataframe()[/-][+]BigQuery: Load to table from dataframe without index[/+]max-sixty commentedon Aug 7, 2018
Details for how to do this are here: googleapis/python-bigquery-pandas#133 (comment)
More than happy for this to be implemented here rather than
pandas-gbq...sungchun12 commentedon Mar 2, 2019
Any updates on this being updated for the bigquery api vs. the pandas-gbq module?
MEZTech-LLC commentedon Mar 14, 2019
@sungchun12 I just tried the solution @max-sixty posted above for the bigquery client api and it worked fine. Load the job configuration and override the schema as suggested.
The schema should be a list with the format:
sungchun12 commentedon Mar 14, 2019
@mikeymezher , thanks for getting back to me! I'll try it out and let you know. Do you know if one is more performant over the other in your hands-on experience?
MEZTech-LLC commentedon Mar 14, 2019
Haven't tested, but anecdotally I've noticed pandas-gbq to be faster than the client library. But there are instances where the client library is needed. Writing to partitioned tables for instance.
tswast commentedon May 16, 2019
They are implementation details, but pandas-gbq uses CSV whereas google-cloud-bigquery uses parquet as the serialization format. The reason for this is to support STRUCT / ARRAY BigQuery columns (though these aren't supported in pandas, anyway).
Implementation-wise, I just noticed pandas provides a way to override the parquet engine's default behavior with an
indexargument. I'm open to adding a similar argument to google-cloud-bigquery.From https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#io-parquet
sungchun12 commentedon May 17, 2019
@tswast , I would LOVE "index=False" functionality in the google-cloud-bigquery package as it would allow me to remove pandas-gbq imports and have a consistent API to work with bigquery...at least for my use cases in having to not build override schema configurations.
cavindsouza commentedon Jun 3, 2019
@tswast Has the
index=Falsefunctionality been added to google-cloud-bigquery?? Thanks.tswast commentedon Jun 3, 2019
@cavindsouza not yet. Right now you can avoid writing indexes by passing in a
job_configargument with the desiredjob_config.schema. I agree this would still be a useful feature.tswast commentedon Aug 22, 2019
FYI: #9064 and #9049 are changing the index behavior, as a schema will be automatically populated in more cases now.
We might actually have a need to explicitly add indexes to the table. Currently, it's inconsistent when an index will be added and when not. It depends on if the schema is populated and which parquet engine is used to serialized the DataFrame.
Preferred option
Check if index (or indexes if multi-index) name(s) are present in
job_config.schema. If so, include the index(es) that are specified. If not, omit the indexes.Edge case: What if index name matches that of a column name? Prefer serializing the column. Don't add the index in this case.
Alternative
Add a
index=True/index=Falseoption to theload_table_from_dataframefunction.This makes it when to include indexes. This would allow the index dtype to be used to automatically determine the schema in some cases.
When the index dtype is object, we'll need to add the index to the
job_config.schema, anyway, so this requires the same implementation as the preferred option.tswast commentedon Aug 22, 2019
Follow-up from #9064
When this feature is added,
test_load_table_from_dataframesnippets to show overriding schema for columns whose types can't be autodetected. Explicitly add indexes to the partial schema as well.tswast commentedon Aug 28, 2019
Once this feature is released, do the following to omit indexes:
pyarrowlibrary.LoadJobConfig.schemafor anyobjectdtype columns.To include indexes:
pyarrowlibrary.name="my_index_name"to the Index constructo.index=my_indexargument.LoadJobConfig.schemafor anyobjectdtype columns and any indexes you want to include.Code sample:
google-cloud-python/bigquery/samples/load_table_dataframe.py
Lines 18 to 71 in a6ed945