Skip to content

test: Add test of datetime and time pandas load #895

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 6 commits into from
Aug 24, 2021
Merged
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
64 changes: 53 additions & 11 deletions tests/system/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,14 @@ def test_load_table_from_dataframe_w_required(bigquery_client, dataset_id):

def test_load_table_from_dataframe_w_explicit_schema(bigquery_client, dataset_id):
# Schema with all scalar types.
# TODO: Uploading DATETIME columns currently fails, thus that field type
# is temporarily removed from the test.
# See:
# https://github.com/googleapis/python-bigquery/issues/61
# https://issuetracker.google.com/issues/151765076
scalars_schema = (
bigquery.SchemaField("bool_col", "BOOLEAN"),
bigquery.SchemaField("bytes_col", "BYTES"),
bigquery.SchemaField("date_col", "DATE"),
# bigquery.SchemaField("dt_col", "DATETIME"),
bigquery.SchemaField("dt_col", "DATETIME"),
bigquery.SchemaField("float_col", "FLOAT"),
bigquery.SchemaField("geo_col", "GEOGRAPHY"),
bigquery.SchemaField("int_col", "INTEGER"),
Expand All @@ -313,14 +311,14 @@ def test_load_table_from_dataframe_w_explicit_schema(bigquery_client, dataset_id
("bool_col", [True, None, False]),
("bytes_col", [b"abc", None, b"def"]),
("date_col", [datetime.date(1, 1, 1), None, datetime.date(9999, 12, 31)]),
# (
# "dt_col",
# [
# datetime.datetime(1, 1, 1, 0, 0, 0),
# None,
# datetime.datetime(9999, 12, 31, 23, 59, 59, 999999),
# ],
# ),
(
"dt_col",
[
datetime.datetime(1, 1, 1, 0, 0, 0),
None,
datetime.datetime(9999, 12, 31, 23, 59, 59, 999999),
],
),
("float_col", [float("-inf"), float("nan"), float("inf")]),
(
"geo_col",
Expand Down Expand Up @@ -800,6 +798,50 @@ def test_list_rows_max_results_w_bqstorage(bigquery_client):
assert len(dataframe.index) == 100


def test_upload_time_and_datetime_56(bigquery_client, dataset_id):
df = pandas.DataFrame(
dict(
dt=[
datetime.datetime(2020, 1, 8, 8, 0, 0),
datetime.datetime(
2020,
1,
8,
8,
0,
0,
tzinfo=datetime.timezone(datetime.timedelta(hours=-7)),
),
],
t=[datetime.time(0, 0, 10, 100001), None],
)
)
table = f"{dataset_id}.test_upload_time_and_datetime"
bigquery_client.load_table_from_dataframe(df, table).result()
data = list(map(list, bigquery_client.list_rows(table)))
assert data == [
[
datetime.datetime(2020, 1, 8, 8, 0, tzinfo=datetime.timezone.utc),
datetime.time(0, 0, 10, 100001),
],
[datetime.datetime(2020, 1, 8, 15, 0, tzinfo=datetime.timezone.utc), None],
]

from google.cloud.bigquery import job, schema

table = f"{dataset_id}.test_upload_time_and_datetime_dt"
config = job.LoadJobConfig(
schema=[schema.SchemaField("dt", "DATETIME"), schema.SchemaField("t", "TIME")]
)

bigquery_client.load_table_from_dataframe(df, table, job_config=config).result()
data = list(map(list, bigquery_client.list_rows(table)))
assert data == [
[datetime.datetime(2020, 1, 8, 8, 0), datetime.time(0, 0, 10, 100001)],
[datetime.datetime(2020, 1, 8, 15, 0), None],
]


def test_to_dataframe_geography_as_objects(bigquery_client, dataset_id):
wkt = pytest.importorskip("shapely.wkt")
bigquery_client.query(
Expand Down