Skip to content
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

Panel constructed with a None as a dict value throws an error #2075

Closed
wants to merge 1 commit into from
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
14 changes: 10 additions & 4 deletions pandas/core/panel.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,11 @@ def _homogenize_dict(frames, intersect=True, dtype=None):
columns = _extract_axis(adj_frames, axis=1, intersect=intersect)

for key, frame in adj_frames.iteritems():
result[key] = frame.reindex(index=index, columns=columns,
copy=False)
if frame is not None:
result[key] = frame.reindex(index=index, columns=columns,
copy=False)
else:
result[key] = None

return result, index, columns

Expand All @@ -1415,15 +1418,15 @@ def _extract_axis(data, axis=0, intersect=False):
elif len(data) > 0:
raw_lengths = []
indexes = []

index = None
have_raw_arrays = False
have_frames = False

for v in data.values():
if isinstance(v, DataFrame):
have_frames = True
indexes.append(v._get_axis(axis))
else:
elif v is not None:
have_raw_arrays = True
raw_lengths.append(v.shape[axis])

Expand All @@ -1440,6 +1443,9 @@ def _extract_axis(data, axis=0, intersect=False):
else:
index = Index(np.arange(lengths[0]))

if index is None:
index = Index([])

return _ensure_index(index)


Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/test_panel.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,9 @@ def test_ctor_dict(self):

d = {'A' : itema, 'B' : itemb[5:]}
d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
d3 = {'A' : DataFrame(itema._series),
'B' : DataFrame(itemb[5:]._series)}
d3 = {'A' : None,
'B' : DataFrame(itemb[5:]._series),
'C' : DataFrame(itema._series)}

wp = Panel.from_dict(d)
wp2 = Panel.from_dict(d2) # nested Dict
Expand All @@ -748,6 +749,11 @@ def test_ctor_dict(self):
assert_panel_equal(Panel(d2), Panel.from_dict(d2))
assert_panel_equal(Panel(d3), Panel.from_dict(d3))

# a pathological case
d4 = { 'A' : None, 'B' : None }
wp4 = Panel.from_dict(d4)
assert_panel_equal(Panel(d4), Panel(items = ['A','B']))

# cast
dcasted = dict((k, v.reindex(wp.major_axis).fillna(0))
for k, v in d.iteritems())
Expand Down