Skip to content

Commit a989b7f

Browse files
authored
BUG: use object dtype for TIME columns (#341)
1 parent b46e955 commit a989b7f

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

docs/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changelog
99
Bug fixes
1010
~~~~~~~~~
1111

12+
- Use ``object`` dtype for ``TIME`` columns. (:issue:`328`)
1213
- Encode floating point values with greater precision. (:issue:`326`)
1314
- Support ``INT64`` and other standard SQL aliases in
1415
:func:`~pandas_gbq.to_gbq` ``table_schema`` argument. (:issue:`322`)

pandas_gbq/gbq.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,9 @@ def _bqschema_to_nullsafe_dtypes(schema_fields):
725725
"GEOMETRY": "object",
726726
"RECORD": "object",
727727
"STRING": "object",
728-
"TIME": "datetime64[ns]",
728+
# datetime.time objects cannot be case to datetime64.
729+
# https://github.com/pydata/pandas-gbq/issues/328
730+
"TIME": "object",
729731
# pandas doesn't support timezone-aware dtype in DataFrame/Series
730732
# constructors. It's more idiomatic to localize after construction.
731733
# https://github.com/pandas-dev/pandas/issues/25843

tests/system/test_gbq.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import datetime
34
import sys
4-
from datetime import datetime
55

66
import numpy as np
77
import pandas
@@ -39,7 +39,8 @@ def make_mixed_dataframe_v2(test_size):
3939
ints = np.random.randint(1, 10, size=(1, test_size))
4040
strs = np.random.randint(1, 10, size=(1, test_size)).astype(str)
4141
times = [
42-
datetime.now(pytz.timezone("US/Arizona")) for t in range(test_size)
42+
datetime.datetime.now(pytz.timezone("US/Arizona"))
43+
for t in range(test_size)
4344
]
4445
return DataFrame(
4546
{
@@ -248,6 +249,38 @@ def test_should_properly_handle_null_floats(self, project_id):
248249
)
249250
tm.assert_frame_equal(df, DataFrame({"null_float": [np.nan, 1.0]}))
250251

252+
def test_should_properly_handle_date(self, project_id):
253+
query = "SELECT DATE(2003, 1, 4) AS date_col"
254+
df = gbq.read_gbq(
255+
query,
256+
project_id=project_id,
257+
credentials=self.credentials,
258+
)
259+
expected = DataFrame(
260+
{
261+
"date_col": pandas.Series(
262+
[datetime.date(2003, 1, 4)], dtype="datetime64[ns]"
263+
)
264+
},
265+
)
266+
tm.assert_frame_equal(df, expected)
267+
268+
def test_should_properly_handle_time(self, project_id):
269+
query = "SELECT TIME_ADD(TIME(3, 14, 15), INTERVAL 926589 MICROSECOND) AS time_col"
270+
df = gbq.read_gbq(
271+
query,
272+
project_id=project_id,
273+
credentials=self.credentials,
274+
)
275+
expected = DataFrame(
276+
{
277+
"time_col": pandas.Series(
278+
[datetime.time(3, 14, 15, 926589)], dtype="object"
279+
)
280+
},
281+
)
282+
tm.assert_frame_equal(df, expected)
283+
251284
def test_should_properly_handle_timestamp_unix_epoch(self, project_id):
252285
query = 'SELECT TIMESTAMP("1970-01-01 00:00:00") AS unix_epoch'
253286
df = gbq.read_gbq(
@@ -1113,7 +1146,7 @@ def test_google_upload_errors_should_raise_exception(self, project_id):
11131146
raise pytest.skip("buggy test")
11141147

11151148
test_id = "5"
1116-
test_timestamp = datetime.now(pytz.timezone("US/Arizona"))
1149+
test_timestamp = datetime.datetime.now(pytz.timezone("US/Arizona"))
11171150
bad_df = DataFrame(
11181151
{
11191152
"bools": [False, False],

0 commit comments

Comments
 (0)