Skip to content

Commit 241bde1

Browse files
bnaulWillAyd
authored andcommitted
Support writing CSV to GCS (pandas-dev#22704)
1 parent e4b67ca commit 241bde1

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Other Enhancements
181181
- :func:`to_csv` now supports ``compression`` keyword when a file handle is passed. (:issue:`21227`)
182182
- :meth:`Index.droplevel` is now implemented also for flat indexes, for compatibility with :class:`MultiIndex` (:issue:`21115`)
183183
- :meth:`Series.droplevel` and :meth:`DataFrame.droplevel` are now implemented (:issue:`20342`)
184-
- Added support for reading from Google Cloud Storage via the ``gcsfs`` library (:issue:`19454`)
184+
- Added support for reading from/writing to Google Cloud Storage via the ``gcsfs`` library (:issue:`19454`, :issue:`23094`)
185185
- :func:`to_gbq` and :func:`read_gbq` signature and documentation updated to
186186
reflect changes from the `Pandas-GBQ library version 0.6.0
187187
<https://pandas-gbq.readthedocs.io/en/latest/changelog.html#changelog-0-6-0>`__.

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)