From 231c2e486182a8c65e14a4ee86eb7bdf39720c75 Mon Sep 17 00:00:00 2001 From: "Brandon M. Burroughs" Date: Sat, 29 Oct 2016 09:11:03 -0400 Subject: [PATCH] BUG: Dataframe constructor when given dict with None value --- doc/source/whatsnew/v0.19.1.txt | 1 + pandas/core/series.py | 4 ++-- pandas/tests/frame/test_constructors.py | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 3edb8c1fa9071..6dddebecd06e8 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -32,6 +32,7 @@ Bug Fixes ~~~~~~~~~ +- Bug in ``pd.DataFrame`` where constructor fails when given dict with ``None`` value (:issue:`14381`) diff --git a/pandas/core/series.py b/pandas/core/series.py index 1c6b13885dd01..188204d83d985 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2915,8 +2915,8 @@ def create_from_value(value, index, dtype): return subarr - # scalar like - if subarr.ndim == 0: + # scalar like, GH + if getattr(subarr, 'ndim', 0) == 0: if isinstance(data, list): # pragma: no cover subarr = np.array(data, dtype=object) elif index is not None: diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index d21db5ba52a45..e55ba3e161ed9 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -259,6 +259,14 @@ def test_constructor_dict(self): frame = DataFrame({'A': [], 'B': []}, columns=['A', 'B']) self.assert_index_equal(frame.index, Index([], dtype=np.int64)) + # GH 14381 + # Dict with None value + frame_none = DataFrame(dict(a=None), index=[0]) + frame_none_list = DataFrame(dict(a=[None]), index=[0]) + tm.assert_equal(frame_none.get_value(0, 'a'), None) + tm.assert_equal(frame_none_list.get_value(0, 'a'), None) + tm.assert_frame_equal(frame_none, frame_none_list) + # GH10856 # dict with scalar values should raise error, even if columns passed with tm.assertRaises(ValueError):