Skip to content

Commit 12d39c6

Browse files
author
TomAugspurger
committed
BUG: fix DataFrame constructor w named Series
Pushing for now [ci skip]
1 parent 671c4b3 commit 12d39c6

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

doc/source/whatsnew/v0.16.0.txt

+11
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ The behavior of a small sub-set of edge cases for using ``.loc`` have changed (:
367367
In [4]: df.loc[2:3]
368368
TypeError: Cannot do slice indexing on <class 'pandas.tseries.index.DatetimeIndex'> with <type 'int'> keys
369369

370+
DataFrame Construction Changes
371+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
372+
373+
.. _whatsnew_0160.api_breaking.construction:
374+
375+
376+
370377

371378
.. _whatsnew_0160.deprecations:
372379

@@ -535,3 +542,7 @@ Bug Fixes
535542
- Fixed bug with reading CSV files from Amazon S3 on python 3 raising a TypeError (:issue:`9452`)
536543

537544
- Bug in the Google BigQuery reader where the 'jobComplete' key may be present but False in the query results (:issue:`8728`)
545+
546+
547+
- Fixed bug with DataFrame constructor when passed a Series with a
548+
name and the `columns` keyword argument.

pandas/core/frame.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
231231
if columns is None:
232232
columns = data_columns
233233
mgr = self._init_dict(data, index, columns, dtype=dtype)
234-
elif getattr(data, 'name', None):
234+
elif getattr(data, 'name', None) is not None:
235235
mgr = self._init_dict({data.name: data}, index, columns,
236236
dtype=dtype)
237237
else:
@@ -295,16 +295,15 @@ def _init_dict(self, data, index, columns, dtype=None):
295295
if columns is not None:
296296
columns = _ensure_index(columns)
297297

298-
# prefilter if columns passed
299-
300-
data = dict((k, v) for k, v in compat.iteritems(data)
301-
if k in columns)
302-
303298
if index is None:
304299
index = extract_index(list(data.values()))
305300
else:
306301
index = _ensure_index(index)
307302

303+
# prefilter if columns passed
304+
data = dict((k, v) for k, v in compat.iteritems(data)
305+
if k in columns)
306+
308307
arrays = []
309308
data_names = []
310309
for k in columns:

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,18 @@ def test_constructor_Series_named(self):
33623362
expected = DataFrame({ 1 : s1, 0 : arr },columns=[0,1])
33633363
assert_frame_equal(df,expected)
33643364

3365+
def test_constructor_Series_named_different(self):
3366+
# 9232
3367+
x = Series([1, 2], name=0)
3368+
expected = DataFrame([np.nan, np.nan], columns=[1])
3369+
result = DataFrame(x, columns=[1])
3370+
assert_frame_equal(result, expected)
3371+
3372+
x.name = 1
3373+
expected = DataFrame([np.nan, np.nan], columns=[0])
3374+
result = DataFrame(x, columns=[0])
3375+
assert_frame_equal(result, expected)
3376+
33653377
def test_constructor_Series_differently_indexed(self):
33663378
# name
33673379
s1 = Series([1, 2, 3], index=['a', 'b', 'c'], name='x')

0 commit comments

Comments
 (0)