Skip to content
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

fix(bigquery): write pandas datetime[ns] columns to BigQuery TIMESTAMP columns #10028

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
doc: show timezone conversions for timestamp columns
Pandas doesn't automatically convert datetime objects to UTC time, so
show how to do this in the code sample.
  • Loading branch information
tswast committed Jan 3, 2020
commit 4fda7f5312eb1780e23a7d09d52926d1a1b6960a
24 changes: 12 additions & 12 deletions bigquery/samples/load_table_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,36 @@ def load_table_dataframe(client, table_id):
"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")
),
"release_date": pytz.timezone("Europe/Paris")
.localize(datetime.datetime(1983, 5, 9, 13, 0, 0))
.astimezone(pytz.utc),
"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")
),
"release_date": pytz.timezone("Europe/London")
.localize(datetime.datetime(1975, 4, 9, 23, 59, 2))
.astimezone(pytz.utc),
"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")
),
"release_date": pytz.timezone("America/New_York")
.localize(datetime.datetime(1979, 8, 17, 23, 59, 5))
.astimezone(pytz.utc),
"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")
),
"release_date": pytz.timezone("Europe/London")
.localize(datetime.datetime(1971, 9, 28, 23, 59, 7))
.astimezone(pytz.utc),
"dvd_release": datetime.datetime(2003, 10, 22, 10, 0, 0),
},
]
Expand Down
29 changes: 7 additions & 22 deletions bigquery/samples/tests/test_load_table_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,18 @@ def test_load_table_dataframe(capsys, client, random_table_id):
u"Life of Brian",
u"The Meaning of Life",
]
assert df["release_year"].tolist() == [
1971,
1975,
1979,
1983,
]
assert df["length_minutes"].tolist() == [
88.0,
91.5,
94.25,
112.5,
]
assert df["release_year"].tolist() == [1971, 1975, 1979, 1983]
assert df["length_minutes"].tolist() == [88.0, 91.5, 94.25, 112.5]
assert df["release_date"].tolist() == [
pandas.Timestamp("1971-09-28T23:59:07+00:00"),
pandas.Timestamp("1975-04-09T23:59:02+00:00"),
pandas.Timestamp("1979-08-17T23:59:05+00:00"),
pandas.Timestamp("1983-05-09T13:00:00+00:00"),
pandas.Timestamp("1971-09-28T22:59:07+00:00"),
pandas.Timestamp("1975-04-09T22:59:02+00:00"),
pandas.Timestamp("1979-08-18T03:59:05+00:00"),
pandas.Timestamp("1983-05-09T11:00:00+00:00"),
]
assert df["dvd_release"].tolist() == [
pandas.Timestamp("2003-10-22T10:00:00+00:00"),
pandas.Timestamp("2002-07-16T09:00:00+00:00"),
pandas.Timestamp("2008-01-14T08:00:00+00:00"),
pandas.Timestamp("2002-01-22T07:00:00+00:00"),
]
assert df["wikidata_id"].tolist() == [
u"Q16403",
u"Q25043",
u"Q24953",
u"Q24980",
]
assert df["wikidata_id"].tolist() == [u"Q16403", u"Q25043", u"Q24953", u"Q24980"]
7 changes: 6 additions & 1 deletion bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,12 @@ def test_load_table_from_dataframe_w_automatic_schema(self):
(
bigquery.SchemaField("bool_col", "BOOLEAN"),
bigquery.SchemaField("ts_col", "TIMESTAMP"),
bigquery.SchemaField("dt_col", "DATETIME"),
# Due to internal bug 147108331, BigQuery always interprets
# DATETIME columns as having the wrong precision. In the
# meantime, workaround this by writing the values as TIMESTAMP.
# See:
# https://github.com/googleapis/google-cloud-python/issues/9996
bigquery.SchemaField("dt_col", "TIMESTAMP"),
bigquery.SchemaField("float32_col", "FLOAT"),
bigquery.SchemaField("float64_col", "FLOAT"),
bigquery.SchemaField("int8_col", "INTEGER"),
Expand Down