Skip to content

Commit 62e65f1

Browse files
committed
Support writing CSV to GCS
1 parent 2b08e06 commit 62e65f1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/io/formats/csvs.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
_get_handle,
2727
_infer_compression,
2828
_stringify_path,
29+
get_filepath_or_buffer,
2930
UnicodeWriter,
3031
)
3132

@@ -45,7 +46,9 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
4546
if path_or_buf is None:
4647
path_or_buf = StringIO()
4748

48-
self.path_or_buf = _expand_user(_stringify_path(path_or_buf))
49+
self.path_or_buf, _, _, _ = get_filepath_or_buffer(
50+
path_or_buf, encoding=encoding, compression=compression, mode=mode
51+
)
4952
self.sep = sep
5053
self.na_rep = na_rep
5154
self.float_format = float_format

pandas/tests/io/test_gcs.py

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ def test_read_csv_gcs(mock):
2626
assert_frame_equal(df1, df2)
2727

2828

29+
@td.skip_if_no('gcsfs')
30+
def test_to_csv_gcs(mock):
31+
df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'],
32+
'dt': date_range('2018-06-18', periods=2)})
33+
with mock.patch('gcsfs.GCSFileSystem') as MockFileSystem:
34+
s = StringIO()
35+
instance = MockFileSystem.return_value
36+
instance.open.return_value = s
37+
38+
df1.to_csv('gs://test/test.csv', index=True)
39+
df2 = read_csv(StringIO(s.getvalue()), parse_dates=['dt'], index_col=0)
40+
41+
assert_frame_equal(df1, df2)
42+
43+
2944
@td.skip_if_no('gcsfs')
3045
def test_gcs_get_filepath_or_buffer(mock):
3146
df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'],

0 commit comments

Comments
 (0)