fix: ensure SchemaField.field_dtype returns a string · googleapis/python-bigquery@b7696b3 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit b7696b3

Browse files
committed
fix: ensure SchemaField.field_dtype returns a string
1 parent 5805066 commit b7696b3

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

google/cloud/bigquery/_pandas_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def _to_wkb(v):
110110

111111
_PANDAS_DTYPE_TO_BQ = {
112112
"bool": "BOOLEAN",
113+
"boolean": "BOOLEAN",
113114
"datetime64[ns, UTC]": "TIMESTAMP",
114115
"datetime64[ns]": "DATETIME",
115116
"float32": "FLOAT",
@@ -122,6 +123,7 @@ def _to_wkb(v):
122123
"uint16": "INTEGER",
123124
"uint32": "INTEGER",
124125
"geometry": "GEOGRAPHY",
126+
"string": "STRING",
125127
date_dtype_name: "DATE",
126128
time_dtype_name: "TIME",
127129
}
@@ -520,7 +522,7 @@ def dataframe_to_bq_schema(dataframe, bq_schema):
520522

521523
# Otherwise, try to automatically determine the type based on the
522524
# pandas dtype.
523-
bq_type = _PANDAS_DTYPE_TO_BQ.get(dtype.name)
525+
bq_type = _PANDAS_DTYPE_TO_BQ.get(dtype.name.lower())
524526
if bq_type is None:
525527
sample_data = _first_valid(dataframe.reset_index()[column])
526528
if (

google/cloud/bigquery/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def field_type(self):
292292
"""
293293
type_ = self._properties.get("type")
294294
if type_ is None: # Shouldn't happen, but some unit tests do this.
295-
return None
295+
return ""
296296
return cast(str, type_).upper()
297297

298298
@property

tests/unit/test__pandas_helpers.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,14 +1298,15 @@ def test_dataframe_to_bq_schema_returns_schema_with_pandas_gbq(
12981298
def test_dataframe_to_bq_schema_w_named_index(module_under_test, monkeypatch):
12991299
monkeypatch.setattr(module_under_test, "pandas_gbq", None)
13001300

1301-
df_data = collections.OrderedDict(
1302-
[
1303-
("str_column", ["hello", "world"]),
1304-
("int_column", [42, 8]),
1305-
("bool_column", [True, False]),
1306-
]
1307-
)
1308-
index = pandas.Index(["a", "b"], name="str_index")
1301+
str_column = pandas.Series(["hello", "world"], dtype="string")
1302+
int_column = pandas.Series([42, 8], dtype="Int64")
1303+
bool_column = pandas.Series([True, False], dtype="boolean")
1304+
df_data = {
1305+
"str_column": str_column,
1306+
"int_column": int_column,
1307+
"bool_column": bool_column,
1308+
}
1309+
index = pandas.Index(["a", "b"], name="str_index", dtype="string")
13091310
dataframe = pandas.DataFrame(df_data, index=index)
13101311

13111312
with pytest.warns(FutureWarning, match="pandas-gbq"):
@@ -1324,20 +1325,24 @@ def test_dataframe_to_bq_schema_w_named_index(module_under_test, monkeypatch):
13241325
def test_dataframe_to_bq_schema_w_multiindex(module_under_test, monkeypatch):
13251326
monkeypatch.setattr(module_under_test, "pandas_gbq", None)
13261327

1327-
df_data = collections.OrderedDict(
1328-
[
1329-
("str_column", ["hello", "world"]),
1330-
("int_column", [42, 8]),
1331-
("bool_column", [True, False]),
1332-
]
1333-
)
1328+
str_column = pandas.Series(["hello", "world"], dtype="string")
1329+
int_column = pandas.Series([42, 8], dtype="Int64")
1330+
bool_column = pandas.Series([True, False], dtype="boolean")
1331+
df_data = {
1332+
"str_column": str_column,
1333+
"int_column": int_column,
1334+
"bool_column": bool_column,
1335+
}
13341336
index = pandas.MultiIndex.from_tuples(
13351337
[
13361338
("a", 0, datetime.datetime(1999, 12, 31, 23, 59, 59, 999999)),
13371339
("a", 0, datetime.datetime(2000, 1, 1, 0, 0, 0)),
13381340
],
13391341
names=["str_index", "int_index", "dt_index"],
13401342
)
1343+
index = index.set_levels(
1344+
[index.levels[0].astype("string"), index.levels[1], index.levels[2]]
1345+
)
13411346
dataframe = pandas.DataFrame(df_data, index=index)
13421347

13431348
with pytest.warns(FutureWarning, match="pandas-gbq"):

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.