Skip to content

Commit ff877b4

Browse files
committed
ENH: use series name as the column name if passed to DataFrame constructor, GH #373
1 parent ba26b4c commit ff877b4

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pandas 0.5.1
6363
index labels achieving maximum and minimum values (PR #286)
6464
- Add `read_clipboard` function for parsing DataFrame from OS clipboard,
6565
should work across platforms (GH #300)
66+
- Add `nunique` function to Series for counting unique elements (GH #297)
67+
- DataFrame constructor will use Series name if no columns passed (GH #373)
6668
6769
**Improvements to existing features**
6870

pandas/core/frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ def _init_dict(self, data, index, columns, dtype=None):
238238

239239
def _init_ndarray(self, values, index, columns, dtype=None,
240240
copy=False):
241+
if isinstance(values, Series) and values.name is not None:
242+
if columns is None:
243+
columns = [values.name]
244+
241245
values = _prep_ndarray(values, copy=copy)
242246

243247
if dtype is not None:

pandas/src/cppsandbox.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@ def map_indices(ndarray[i4] values):
1313
n = len(values)
1414
for i in range(n):
1515
mapping[i] = values[i]
16-
17-

pandas/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ def test_constructor_orient(self):
11431143
expected = self.mixed_frame.sort_index()
11441144
assert_frame_equal(recons, expected)
11451145

1146+
def test_constructor_Series_named(self):
1147+
a = Series([1,2,3], index=['a','b','c'], name='x')
1148+
df = DataFrame(a)
1149+
self.assert_(df.columns[0] == 'x')
1150+
11461151
def test_astype(self):
11471152
casted = self.frame.astype(int)
11481153
expected = DataFrame(self.frame.values.astype(int),

0 commit comments

Comments
 (0)