Skip to content

Commit c2adf08

Browse files
committed
API: Series.to_csv() should return string when path is None
Test cases for when path is None Return string when path is None Add note to release notes under API Add comments to test cases
1 parent 815b19e commit c2adf08

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

doc/source/v0.15.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ API changes
309309
df
310310
df.dtypes
311311

312+
- ``Series.to_csv()`` now returns a string when ``path=None``, matching the behaviour of
313+
``DataFrame.to_csv()`` (:issue:`8215`).
314+
312315
.. _whatsnew_0150.dt:
313316

314317
.dt accessor

pandas/core/series.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,8 @@ def to_csv(self, path, index=True, sep=",", na_rep='',
22002200
22012201
Parameters
22022202
----------
2203-
path : string file path or file handle / StringIO
2203+
path : string file path or file handle / StringIO. If None is provided
2204+
the result is returned as a string.
22042205
na_rep : string, default ''
22052206
Missing data representation
22062207
float_format : string, default None
@@ -2224,10 +2225,13 @@ def to_csv(self, path, index=True, sep=",", na_rep='',
22242225
"""
22252226
from pandas.core.frame import DataFrame
22262227
df = DataFrame(self)
2227-
df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
2228+
# result is only a string if no path provided, otherwise None
2229+
result = df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
22282230
float_format=float_format, header=header,
22292231
index_label=index_label, mode=mode, nanRep=nanRep,
22302232
encoding=encoding, date_format=date_format)
2233+
if path is None:
2234+
return result
22312235

22322236
def dropna(self, axis=0, inplace=False, **kwargs):
22332237
"""

pandas/tests/test_frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6428,6 +6428,14 @@ def test_to_csv_from_csv_categorical(self):
64286428
df2.to_csv(exp)
64296429
self.assertEqual(res.getvalue(), exp.getvalue())
64306430

6431+
def test_to_csv_path_is_none(self):
6432+
# GH 8215
6433+
# Make sure we return string for consistency with
6434+
# Series.to_csv()
6435+
csv_str = self.frame.to_csv(path=None)
6436+
self.assertIsInstance(csv_str, str)
6437+
recons = pd.read_csv(StringIO(csv_str), index_col=0)
6438+
assert_frame_equal(self.frame, recons)
64316439

64326440
def test_info(self):
64336441
io = StringIO()

pandas/tests/test_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,6 +4371,14 @@ def test_to_csv_list_entries(self):
43714371
buf = StringIO()
43724372
split.to_csv(buf)
43734373

4374+
def test_to_csv_path_is_none(self):
4375+
# GH 8215
4376+
# Series.to_csv() was returning None, inconsistent with
4377+
# DataFrame.to_csv() which returned string
4378+
s = Series([1, 2, 3])
4379+
csv_str = s.to_csv(path=None)
4380+
self.assertIsInstance(csv_str, str)
4381+
43744382
def test_clip(self):
43754383
val = self.ts.median()
43764384

0 commit comments

Comments
 (0)