Skip to content

Commit 844bcff

Browse files
committed
ENH: add to_dataframe method to Series
1 parent 27e4fb1 commit 844bcff

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ Serialization / IO / Conversion
438438
Series.to_pickle
439439
Series.to_csv
440440
Series.to_dict
441+
Series.to_frame
441442
Series.to_sparse
442443
Series.to_string
443444
Series.to_clipboard

doc/source/v0.13.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ Enhancements
492492
- DatetimeIndex is now in the API documentation, see :ref:`here<api.datetimeindex>`
493493
- :meth:`~pandas.io.json.json_normalize` is a new method to allow you to create a flat table
494494
from semi-structured JSON data. :ref:`See the docs<io.json_normalize>` (:issue:`1067`)
495+
- ``Series`` now supports ``to_frame`` method to convert it to a single-column DataFrame
496+
(:issue:`5164`)
495497

496498
.. _whatsnew_0130.experimental:
497499

pandas/core/series.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
827827
raise TypeError('Cannot reset_index inplace on a Series '
828828
'to create a DataFrame')
829829
else:
830-
from pandas.core.frame import DataFrame
831-
if name is None:
832-
df = DataFrame(self)
833-
else:
834-
df = DataFrame({name: self})
835-
830+
df = self.to_frame(name)
836831
return df.reset_index(level=level, drop=drop)
837832

838833
def __unicode__(self):
@@ -1028,6 +1023,27 @@ def to_dict(self):
10281023
"""
10291024
return dict(compat.iteritems(self))
10301025

1026+
def to_frame(self, name=None):
1027+
"""
1028+
Convert Series to DataFrame
1029+
1030+
Parameters
1031+
----------
1032+
name : object, default None
1033+
The passed name should substitute for the series name (if it has one).
1034+
1035+
Returns
1036+
-------
1037+
data_frame : DataFrame
1038+
"""
1039+
from pandas.core.frame import DataFrame
1040+
if name is None:
1041+
df = DataFrame(self)
1042+
else:
1043+
df = DataFrame({name: self})
1044+
1045+
return df
1046+
10311047
def to_sparse(self, kind='block', fill_value=None):
10321048
"""
10331049
Convert Series to SparseSeries

pandas/tests/test_series.py

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas import compat
3131
from pandas.util.testing import (assert_series_equal,
3232
assert_almost_equal,
33+
assert_frame_equal,
3334
ensure_clean)
3435
import pandas.util.testing as tm
3536

@@ -3606,6 +3607,21 @@ def test_tolist(self):
36063607
rs = s.tolist()
36073608
self.assertEqual(self.ts.index[0], rs[0])
36083609

3610+
def test_to_frame(self):
3611+
self.ts.name = None
3612+
rs = self.ts.to_frame()
3613+
xp = pd.DataFrame(self.ts.values, index=self.ts.index)
3614+
assert_frame_equal(rs, xp)
3615+
3616+
self.ts.name = 'testname'
3617+
rs = self.ts.to_frame()
3618+
xp = pd.DataFrame(dict(testname=self.ts.values), index=self.ts.index)
3619+
assert_frame_equal(rs, xp)
3620+
3621+
rs = self.ts.to_frame(name='testdifferent')
3622+
xp = pd.DataFrame(dict(testdifferent=self.ts.values), index=self.ts.index)
3623+
assert_frame_equal(rs, xp)
3624+
36093625
def test_to_dict(self):
36103626
self.assert_(np.array_equal(Series(self.ts.to_dict()), self.ts))
36113627

0 commit comments

Comments
 (0)