Skip to content

Commit d147881

Browse files
committed
Merge branch 'series_to_dataframe' of https://github.com/drasch/pandas into drasch-series_to_dataframe
Conflicts: doc/source/v0.13.0.txt
2 parents 2dca975 + 844bcff commit d147881

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-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/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Improvements to existing features
176176
- ``DataFrame.from_records()`` will now accept generators (:issue:`4910`)
177177
- ``DataFrame.interpolate()`` and ``Series.interpolate()`` have been expanded to include
178178
interpolation methods from scipy. (:issue:`4434`, :issue:`1892`)
179+
- ``Series`` now supports a ``to_frame`` method to convert it to a single-column DataFrame (:issue:`5164`)
179180

180181
API Changes
181182
~~~~~~~~~~~

doc/source/v0.13.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ Enhancements
492492

493493
:ref:`See the docs<indexing.basics.indexing_isin>` for more.
494494

495+
- ``Series`` now supports a ``to_frame`` method to convert it to a single-column DataFrame (:issue:`5164`)
496+
495497
- All R datasets listed here http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/00Index.html can now be loaded into Pandas objects
496498

497499
.. code-block:: python

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

@@ -3679,6 +3680,21 @@ def test_tolist(self):
36793680
rs = s.tolist()
36803681
self.assertEqual(self.ts.index[0], rs[0])
36813682

3683+
def test_to_frame(self):
3684+
self.ts.name = None
3685+
rs = self.ts.to_frame()
3686+
xp = pd.DataFrame(self.ts.values, index=self.ts.index)
3687+
assert_frame_equal(rs, xp)
3688+
3689+
self.ts.name = 'testname'
3690+
rs = self.ts.to_frame()
3691+
xp = pd.DataFrame(dict(testname=self.ts.values), index=self.ts.index)
3692+
assert_frame_equal(rs, xp)
3693+
3694+
rs = self.ts.to_frame(name='testdifferent')
3695+
xp = pd.DataFrame(dict(testdifferent=self.ts.values), index=self.ts.index)
3696+
assert_frame_equal(rs, xp)
3697+
36823698
def test_to_dict(self):
36833699
self.assert_(np.array_equal(Series(self.ts.to_dict()), self.ts))
36843700

0 commit comments

Comments
 (0)