From 969238d1c1481ec5b30ad706030774f0388df580 Mon Sep 17 00:00:00 2001 From: jreback Date: Mon, 16 Sep 2013 13:11:39 -0400 Subject: [PATCH] BUG: (GH4851) path for 0-dim arrays in DataFrame construction were incorrect --- doc/source/release.rst | 2 +- pandas/core/frame.py | 4 ++-- pandas/tests/test_frame.py | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 0ed1f39d72cb5..5c6af024f1663 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -114,7 +114,7 @@ Improvements to existing features - ``Panel.to_excel()`` now accepts keyword arguments that will be passed to its ``DataFrame``'s ``to_excel()`` methods. (:issue:`4750`) - allow DataFrame constructor to accept more list-like objects, e.g. list of - ``collections.Sequence`` and ``array.Array`` objects (:issue:`3783`,:issue:`42971`), + ``collections.Sequence`` and ``array.Array`` objects (:issue:`3783`,:issue:`4297`, :issue:`4851`), thanks @lgautier - DataFrame constructor now accepts a numpy masked record array (:issue:`3478`), thanks @jnothman diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f56b6bc00cf15..a51bfee371b89 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -429,7 +429,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, if index is None and isinstance(data[0], Series): index = _get_names_from_index(data) - if is_list_like(data[0]) and getattr(data[0],'ndim',0) <= 1: + if is_list_like(data[0]) and getattr(data[0],'ndim',1) == 1: arrays, columns = _to_arrays(data, columns, dtype=dtype) columns = _ensure_index(columns) @@ -4710,7 +4710,7 @@ def extract_index(data): elif isinstance(v, dict): have_dicts = True indexes.append(list(v.keys())) - elif is_list_like(v) and getattr(v,'ndim',0) <= 1: + elif is_list_like(v) and getattr(v,'ndim',1) == 1: have_raw_arrays = True raw_lengths.append(len(v)) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index a5c1941a7f2d3..ff4cf1ca821ce 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2675,6 +2675,13 @@ def test_constructor_list_of_lists(self): self.assert_(com.is_integer_dtype(df['num'])) self.assert_(df['str'].dtype == np.object_) + # GH 4851 + # list of 0-dim ndarrays + expected = DataFrame({ 0: range(10) }) + data = [np.array(x) for x in range(10)] + result = DataFrame(data) + assert_frame_equal(result, expected) + def test_constructor_sequence_like(self): # GH 3783 # collections.Squence like