Skip to content

BUG: fix DataFrame constructor w named Series #9237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ The behavior of a small sub-set of edge cases for using ``.loc`` have changed (:
In [4]: df.loc[2:3]
TypeError: Cannot do slice indexing on <class 'pandas.tseries.index.DatetimeIndex'> with <type 'int'> keys

DataFrame Construction Changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. _whatsnew_0160.api_breaking.construction:




.. _whatsnew_0160.deprecations:

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

- Bug in the Google BigQuery reader where the 'jobComplete' key may be present but False in the query results (:issue:`8728`)


- Fixed bug with DataFrame constructor when passed a Series with a
name and the `columns` keyword argument.
11 changes: 5 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
if columns is None:
columns = data_columns
mgr = self._init_dict(data, index, columns, dtype=dtype)
elif getattr(data, 'name', None):
elif getattr(data, 'name', None) is not None:
mgr = self._init_dict({data.name: data}, index, columns,
dtype=dtype)
else:
Expand Down Expand Up @@ -295,16 +295,15 @@ def _init_dict(self, data, index, columns, dtype=None):
if columns is not None:
columns = _ensure_index(columns)

# prefilter if columns passed

data = dict((k, v) for k, v in compat.iteritems(data)
if k in columns)

if index is None:
index = extract_index(list(data.values()))
else:
index = _ensure_index(index)

# prefilter if columns passed
data = dict((k, v) for k, v in compat.iteritems(data)
if k in columns)

arrays = []
data_names = []
for k in columns:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,18 @@ def test_constructor_Series_named(self):
expected = DataFrame({ 1 : s1, 0 : arr },columns=[0,1])
assert_frame_equal(df,expected)

def test_constructor_Series_named_different(self):
# 9232
x = Series([1, 2], name=0)
expected = DataFrame([np.nan, np.nan], columns=[1])
result = DataFrame(x, columns=[1])
assert_frame_equal(result, expected)

x.name = 1
expected = DataFrame([np.nan, np.nan], columns=[0])
result = DataFrame(x, columns=[0])
assert_frame_equal(result, expected)

def test_constructor_Series_differently_indexed(self):
# name
s1 = Series([1, 2, 3], index=['a', 'b', 'c'], name='x')
Expand Down