Skip to content

Commit 33bb872

Browse files
committed
fix: convert to datetime64[us] when TIMESTAMP or DATETIME is empty
1 parent 67df733 commit 33bb872

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pandas_gbq/gbq.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def _finalize_dtypes(
630630
"""
631631
import db_dtypes
632632
import pandas.api.types
633+
import pandas
633634

634635
# If you update this mapping, also update the table at
635636
# `docs/reading.rst`.
@@ -638,6 +639,14 @@ def _finalize_dtypes(
638639
"DATETIME": "datetime64[ns]",
639640
"TIMESTAMP": "datetime64[ns]",
640641
}
642+
if pandas.__version__ > "2.0.0":
643+
# when pandas is 2.0.0 or later, default timestamp dtype is 'datetime64[us]'
644+
# and we should use 'datetime64[us]' instead of 'datetime64[ns]'
645+
dtype_map = {
646+
"DATE": db_dtypes.DateDtype(),
647+
"DATETIME": "datetime64[us]",
648+
"TIMESTAMP": pandas.DatetimeTZDtype(unit="us", tz="UTC"),
649+
}
641650

642651
for field in schema_fields:
643652
# This method doesn't modify ARRAY/REPEATED columns.

tests/unit/test_gbq.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ def test__bqschema_to_nullsafe_dtypes(type_, expected):
113113
assert result == {"x": expected}
114114

115115

116+
@pytest.mark.parametrize(
117+
("data", "schema_type", "expected"),
118+
[
119+
(
120+
pandas.to_datetime(["2017-01-01T12:00:00Z"]).astype(pandas.DatetimeTZDtype(unit="us", tz="UTC")),
121+
"TIMESTAMP",
122+
pandas.DatetimeTZDtype(unit="us", tz="UTC"),
123+
),
124+
(
125+
pandas.to_datetime([]).astype(object),
126+
"TIMESTAMP",
127+
pandas.DatetimeTZDtype(unit="us", tz="UTC"),
128+
),
129+
(
130+
pandas.to_datetime(["2017-01-01T12:00:00"]).astype("datetime64[us]"),
131+
"DATETIME",
132+
numpy.dtype("datetime64[us]"),
133+
),
134+
(
135+
pandas.to_datetime([]).astype(object),
136+
"DATETIME",
137+
numpy.dtype("datetime64[us]"),
138+
),
139+
],
140+
)
141+
def test__finalize_dtypes(data, schema_type, expected):
142+
result = gbq._finalize_dtypes(
143+
pandas.DataFrame(dict(x=data)),
144+
[dict(name="x", type=schema_type, mode="NULLABLE")],
145+
)
146+
assert result["x"].dtype == expected
147+
148+
116149
@pytest.mark.parametrize(
117150
["query_or_table", "expected"],
118151
[

0 commit comments

Comments
 (0)