Skip to content

Commit

Permalink
BUG: TIMESTAMP fields were missing seconds and microseconds after `to…
Browse files Browse the repository at this point in the history
…_gbq`. (#148)

* BUG: fix bug that all TIMESTAMP fileds become 00 secconds.

* add a test for extact timestamp

* use fixed value instead of now()
  • Loading branch information
rhoboro authored and tswast committed Mar 13, 2018
1 parent 369959a commit ed17886
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas_gbq/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def encode_chunk(dataframe):
csv_buffer = six.StringIO()
dataframe.to_csv(
csv_buffer, index=False, header=False, encoding='utf-8',
date_format='%Y-%m-%d %H:%M')
date_format='%Y-%m-%d %H:%M:%S.%f')

# Convert to a BytesIO buffer so that unicode text is properly handled.
# See: https://github.com/pydata/pandas-gbq/issues/106
Expand Down
23 changes: 23 additions & 0 deletions pandas_gbq/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,29 @@ def test_upload_data_with_missing_schema_fields_raises_error(self):
private_key=self.credentials,
table_schema=test_schema)

def test_upload_data_with_timestamp(self):
test_id = "21"
test_size = 6
df = DataFrame(np.random.randn(test_size, 4), index=range(test_size),
columns=list('ABCD'))
df['times'] = np.datetime64('2018-03-13T05:40:45.348318Z')

gbq.to_gbq(
df, self.destination_table + test_id,
_get_project_id(),
private_key=self.credentials)

result_df = gbq.read_gbq("SELECT * FROM {0}".format(
self.destination_table + test_id),
project_id=_get_project_id(),
private_key=self.credentials)

assert len(result_df) == test_size

expected = df['times'].sort_values()
result = result_df['times'].sort_values()
tm.assert_numpy_array_equal(expected.values, result.values)

def test_list_dataset(self):
dataset_id = self.dataset_prefix + "1"
assert dataset_id in self.dataset.datasets()
Expand Down

0 comments on commit ed17886

Please sign in to comment.