Skip to content

Commit

Permalink
doc(bigquery): add more data types to load dataframe sample
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Dec 30, 2019
1 parent 154c8ec commit e10f755
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
52 changes: 46 additions & 6 deletions bigquery/samples/load_table_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
def load_table_dataframe(client, table_id):

# [START bigquery_load_table_dataframe]
from google.cloud import bigquery
import datetime

from google.cloud import bigquery
import pandas
import pytz

# TODO(developer): Construct a BigQuery client object.
# client = bigquery.Client()
Expand All @@ -27,16 +29,54 @@ def load_table_dataframe(client, table_id):
# table_id = "your-project.your_dataset.your_table_name"

records = [
{"title": u"The Meaning of Life", "release_year": 1983},
{"title": u"Monty Python and the Holy Grail", "release_year": 1975},
{"title": u"Life of Brian", "release_year": 1979},
{"title": u"And Now for Something Completely Different", "release_year": 1971},
{
"title": u"The Meaning of Life",
"release_year": 1983,
"length_minutes": 112.5,
"release_date": datetime.datetime(
1983, 5, 9, 13, 0, 0, tzinfo=pytz.timezone("Europe/Paris")
),
"dvd_release": datetime.datetime(2002, 1, 22, 7, 0, 0),
},
{
"title": u"Monty Python and the Holy Grail",
"release_year": 1975,
"length_minutes": 91.5,
"release_date": datetime.datetime(
1975, 4, 9, 23, 59, 2, tzinfo=pytz.timezone("Europe/London")
),
"dvd_release": datetime.datetime(2002, 7, 16, 9, 0, 0),
},
{
"title": u"Life of Brian",
"release_year": 1979,
"length_minutes": 94.25,
"release_date": datetime.datetime(
1979, 8, 17, 23, 59, 5, tzinfo=pytz.timezone("America/New_York")
),
"dvd_release": datetime.datetime(2008, 1, 14, 8, 0, 0),
},
{
"title": u"And Now for Something Completely Different",
"release_year": 1971,
"length_minutes": 88.0,
"release_date": datetime.datetime(
1971, 9, 28, 23, 59, 7, tzinfo=pytz.timezone("Europe/London")
),
"dvd_release": datetime.datetime(2003, 10, 22, 10, 0, 0),
},
]
dataframe = pandas.DataFrame(
records,
# In the loaded table, the column order reflects the order of the
# columns in the DataFrame.
columns=["title", "release_year"],
columns=[
"title",
"release_year",
"length_minutes",
"release_date",
"dvd_release",
],
# Optionally, set a named index, which can also be written to the
# BigQuery table.
index=pandas.Index(
Expand Down
14 changes: 12 additions & 2 deletions bigquery/samples/tests/test_load_table_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ def test_load_table_dataframe(capsys, client, random_table_id):

table = load_table_dataframe.load_table_dataframe(client, random_table_id)
out, _ = capsys.readouterr()
assert "Loaded 4 rows and 3 columns" in out
assert "Loaded 4 rows and 6 columns" in out

column_names = [field.name for field in table.schema]
assert column_names == ["wikidata_id", "title", "release_year"]
assert column_names == [
"wikidata_id",
"title",
"release_year",
"length_minutes",
"release_date",
"dvd_release",
]

df = client.list_rows(table).to_dataframe()
# TODO: sort and check values

0 comments on commit e10f755

Please sign in to comment.