Skip to content

Commit ed17886

Browse files
rhoborotswast
authored andcommitted
BUG: TIMESTAMP fields were missing seconds and microseconds after to_gbq. (#148)
* BUG: fix bug that all TIMESTAMP fileds become 00 secconds. * add a test for extact timestamp * use fixed value instead of now()
1 parent 369959a commit ed17886

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas_gbq/_load.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def encode_chunk(dataframe):
1515
csv_buffer = six.StringIO()
1616
dataframe.to_csv(
1717
csv_buffer, index=False, header=False, encoding='utf-8',
18-
date_format='%Y-%m-%d %H:%M')
18+
date_format='%Y-%m-%d %H:%M:%S.%f')
1919

2020
# Convert to a BytesIO buffer so that unicode text is properly handled.
2121
# See: https://github.com/pydata/pandas-gbq/issues/106

pandas_gbq/tests/test_gbq.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,29 @@ def test_upload_data_with_missing_schema_fields_raises_error(self):
13891389
private_key=self.credentials,
13901390
table_schema=test_schema)
13911391

1392+
def test_upload_data_with_timestamp(self):
1393+
test_id = "21"
1394+
test_size = 6
1395+
df = DataFrame(np.random.randn(test_size, 4), index=range(test_size),
1396+
columns=list('ABCD'))
1397+
df['times'] = np.datetime64('2018-03-13T05:40:45.348318Z')
1398+
1399+
gbq.to_gbq(
1400+
df, self.destination_table + test_id,
1401+
_get_project_id(),
1402+
private_key=self.credentials)
1403+
1404+
result_df = gbq.read_gbq("SELECT * FROM {0}".format(
1405+
self.destination_table + test_id),
1406+
project_id=_get_project_id(),
1407+
private_key=self.credentials)
1408+
1409+
assert len(result_df) == test_size
1410+
1411+
expected = df['times'].sort_values()
1412+
result = result_df['times'].sort_values()
1413+
tm.assert_numpy_array_equal(expected.values, result.values)
1414+
13921415
def test_list_dataset(self):
13931416
dataset_id = self.dataset_prefix + "1"
13941417
assert dataset_id in self.dataset.datasets()

0 commit comments

Comments
 (0)