Skip to content

Commit 0e0ce70

Browse files
committed
Support writing CSV to GCS
1 parent 2b08e06 commit 0e0ce70

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pandas/io/formats/csvs.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
ABCMultiIndex, ABCPeriodIndex, ABCDatetimeIndex, ABCIndexClass)
2323

2424
from pandas.io.common import (
25-
_expand_user,
2625
_get_handle,
2726
_infer_compression,
28-
_stringify_path,
27+
get_filepath_or_buffer,
2928
UnicodeWriter,
3029
)
3130

@@ -45,7 +44,9 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
4544
if path_or_buf is None:
4645
path_or_buf = StringIO()
4746

48-
self.path_or_buf = _expand_user(_stringify_path(path_or_buf))
47+
self.path_or_buf, _, _, _ = get_filepath_or_buffer(
48+
path_or_buf, encoding=encoding, compression=compression, mode=mode
49+
)
4950
self.sep = sep
5051
self.na_rep = na_rep
5152
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)