Skip to content

Commit fca5d29

Browse files
committed
Py2/Py3 compat
1 parent b3bbff0 commit fca5d29

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

pandas_gbq/gbq.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,11 @@ def load_data(self, dataframe, dataset_id, table_id, chunksize):
581581
self._print("\rLoad is {0}% Complete".format(
582582
((total_rows - remaining_rows) * 100) / total_rows))
583583

584-
body = BytesIO('{}\n'.format('\n'.join(rows)).encode('utf-8'))
584+
body = '{}\n'.format('\n'.join(rows))
585+
if isinstance(body, bytes):
586+
body = body.decode('utf-8')
587+
body = body.encode('utf-8')
588+
body = BytesIO(body)
585589

586590
try:
587591
self.client.load_table_from_file(

pandas_gbq/tests/test_gbq.py

+52-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from random import randint
1111
import logging
12+
import sys
1213

1314
import numpy as np
1415

@@ -1161,25 +1162,33 @@ def test_upload_chinese_unicode_data(self):
11611162
test_size = 6
11621163
df = DataFrame(np.random.randn(6, 4), index=range(6),
11631164
columns=list('ABCD'))
1164-
df.A = u'信用卡'
1165+
df['s'] = u'信用卡'
11651166

11661167
gbq.to_gbq(df, self.destination_table + test_id, _get_project_id(),
11671168
chunksize=10000)
11681169

1169-
result = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}".format(
1170+
result = gbq.read_gbq("SELECT * FROM {0}".format(
11701171
self.destination_table + test_id),
11711172
project_id=_get_project_id())
11721173

1173-
assert result['num_rows'][0] == test_size
1174-
tm.assert_series_equal(result['A'], df['A'])
1174+
assert len(result_df) == test_size
1175+
1176+
pytest.skipif(
1177+
sys.version_info.major < 3,
1178+
reason='Unicode comparison in Py2 not working')
1179+
1180+
result = result_df['s'].sort_values()
1181+
expected = df['s'].sort_values()
1182+
1183+
tm.assert_numpy_array_equal(expected.values, result.values)
11751184

11761185
def test_upload_other_unicode_data(self):
11771186
test_id = "3"
11781187
test_size = 3
11791188
df = DataFrame({
1180-
'string': ['Skywalker™', 'lego', 'hülle'],
1181-
'integer': [200, 300, 400],
1182-
'Date': [
1189+
's': ['Skywalker™', 'lego', 'hülle'],
1190+
'i': [200, 300, 400],
1191+
'd': [
11831192
'2017-12-13 17:40:39', '2017-12-13 17:40:39',
11841193
'2017-12-13 17:40:39'
11851194
]
@@ -1188,12 +1197,20 @@ def test_upload_other_unicode_data(self):
11881197
gbq.to_gbq(df, self.destination_table + test_id, _get_project_id(),
11891198
chunksize=10000)
11901199

1191-
result = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}".format(
1200+
result_df = gbq.read_gbq("SELECT * FROM {0}".format(
11921201
self.destination_table + test_id),
11931202
project_id=_get_project_id())
11941203

1195-
assert result['num_rows'][0] == test_size
1196-
tm.assert_series_equal(result['string'], df['string'])
1204+
assert len(result_df) == test_size
1205+
1206+
pytest.skipif(
1207+
sys.version_info.major < 3,
1208+
reason='Unicode comparison in Py2 not working')
1209+
1210+
result = result_df['s'].sort_values()
1211+
expected = df['s'].sort_values()
1212+
1213+
tm.assert_numpy_array_equal(expected.values, result.values)
11971214

11981215
def test_generate_schema(self):
11991216
df = tm.makeMixedDataFrame()
@@ -1513,25 +1530,32 @@ def test_upload_chinese_unicode_data(self):
15131530
test_size = 6
15141531
df = DataFrame(np.random.randn(6, 4), index=range(6),
15151532
columns=list('ABCD'))
1516-
df.A = u'信用卡'
1533+
df['s'] = u'信用卡'
15171534

15181535
gbq.to_gbq(df, self.destination_table + test_id, _get_project_id(),
15191536
chunksize=10000)
15201537

1521-
result = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}".format(
1538+
result_df = gbq.read_gbq("SELECT * FROM {0}".format(
15221539
self.destination_table + test_id),
15231540
project_id=_get_project_id())
15241541

1525-
assert result['num_rows'][0] == test_size
1526-
tm.assert_series_equal(result['A'], df['A'])
1542+
assert len(result_df) == test_size
1543+
1544+
if sys.version_info.major < 3:
1545+
pytest.skip(msg='Unicode comparison in Py2 not working')
1546+
1547+
result = result_df['s'].sort_values()
1548+
expected = df['s'].sort_values()
1549+
1550+
tm.assert_numpy_array_equal(expected.values, result.values)
15271551

15281552
def test_upload_other_unicode_data(self):
15291553
test_id = "3"
15301554
test_size = 3
15311555
df = DataFrame({
1532-
'string': ['Skywalker™', 'lego', 'hülle'],
1533-
'integer': [200, 300, 400],
1534-
'Date': [
1556+
's': ['Skywalker™', 'lego', 'hülle'],
1557+
'i': [200, 300, 400],
1558+
'd': [
15351559
'2017-12-13 17:40:39', '2017-12-13 17:40:39',
15361560
'2017-12-13 17:40:39'
15371561
]
@@ -1540,12 +1564,20 @@ def test_upload_other_unicode_data(self):
15401564
gbq.to_gbq(df, self.destination_table + test_id, _get_project_id(),
15411565
chunksize=10000)
15421566

1543-
result = gbq.read_gbq("SELECT COUNT(*) AS num_rows FROM {0}".format(
1567+
result_df = gbq.read_gbq("SELECT * FROM {0}".format(
15441568
self.destination_table + test_id),
15451569
project_id=_get_project_id())
15461570

1547-
assert result['num_rows'][0] == test_size
1548-
tm.assert_series_equal(result['string'], df['string'])
1571+
assert len(result_df) == test_size
1572+
1573+
if sys.version_info.major < 3:
1574+
pytest.skip(msg='Unicode comparison in Py2 not working')
1575+
1576+
result = result_df['s'].sort_values()
1577+
expected = df['s'].sort_values()
1578+
1579+
tm.assert_numpy_array_equal(expected.values, result.values)
1580+
15491581

15501582
class TestToGBQIntegrationWithServiceAccountKeyContents(object):
15511583
# Changes to BigQuery table schema may take up to 2 minutes as of May 2015

0 commit comments

Comments
 (0)