Skip to content

Commit 969238d

Browse files
committed
BUG: (GH4851) path for 0-dim arrays in DataFrame construction were incorrect
1 parent 1d1317d commit 969238d

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Improvements to existing features
114114
- ``Panel.to_excel()`` now accepts keyword arguments that will be passed to
115115
its ``DataFrame``'s ``to_excel()`` methods. (:issue:`4750`)
116116
- allow DataFrame constructor to accept more list-like objects, e.g. list of
117-
``collections.Sequence`` and ``array.Array`` objects (:issue:`3783`,:issue:`42971`),
117+
``collections.Sequence`` and ``array.Array`` objects (:issue:`3783`,:issue:`4297`, :issue:`4851`),
118118
thanks @lgautier
119119
- DataFrame constructor now accepts a numpy masked record array (:issue:`3478`),
120120
thanks @jnothman

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
429429
if index is None and isinstance(data[0], Series):
430430
index = _get_names_from_index(data)
431431

432-
if is_list_like(data[0]) and getattr(data[0],'ndim',0) <= 1:
432+
if is_list_like(data[0]) and getattr(data[0],'ndim',1) == 1:
433433
arrays, columns = _to_arrays(data, columns, dtype=dtype)
434434
columns = _ensure_index(columns)
435435

@@ -4710,7 +4710,7 @@ def extract_index(data):
47104710
elif isinstance(v, dict):
47114711
have_dicts = True
47124712
indexes.append(list(v.keys()))
4713-
elif is_list_like(v) and getattr(v,'ndim',0) <= 1:
4713+
elif is_list_like(v) and getattr(v,'ndim',1) == 1:
47144714
have_raw_arrays = True
47154715
raw_lengths.append(len(v))
47164716

pandas/tests/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,13 @@ def test_constructor_list_of_lists(self):
26752675
self.assert_(com.is_integer_dtype(df['num']))
26762676
self.assert_(df['str'].dtype == np.object_)
26772677

2678+
# GH 4851
2679+
# list of 0-dim ndarrays
2680+
expected = DataFrame({ 0: range(10) })
2681+
data = [np.array(x) for x in range(10)]
2682+
result = DataFrame(data)
2683+
assert_frame_equal(result, expected)
2684+
26782685
def test_constructor_sequence_like(self):
26792686
# GH 3783
26802687
# collections.Squence like

0 commit comments

Comments
 (0)