Skip to content

Commit 04d39bf

Browse files
jrebackwesm
authored andcommitted
a panel constructed with a None value throws an error (0.9 exhibits the same behavior)
Python 2.7.3 (default, Jun 21 2012, 07:50:29) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas >>> print pandas.__version__ 0.8.1 >>> print pandas.Panel(dict(a = None, b = pandas.DataFrame(index=[1,2,3],columns=[1,2,3]))) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/core/panel.py", line 219, in __init__ mgr = self._init_dict(data, passed_axes, dtype=dtype) File "/usr/local/lib/python2.7/site-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/core/panel.py", line 256, in _init_dict major = _extract_axis(data, axis=0) File "/usr/local/lib/python2.7/site-packages/pandas-0.8.1-py2.7-linux-x86_64.egg/pandas/core/panel.py", line 1396, in _extract_axis raw_lengths.append(v.shape[axis]) AttributeError: 'NoneType' object has no attribute 'shape' added tests and fixes for this case (and the pathological case of constructing with dict(a=None))
1 parent 9b0eaa4 commit 04d39bf

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pandas/core/panel.py

100644100755
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,8 +1409,11 @@ def _homogenize_dict(frames, intersect=True, dtype=None):
14091409
columns = _extract_axis(adj_frames, axis=1, intersect=intersect)
14101410

14111411
for key, frame in adj_frames.iteritems():
1412-
result[key] = frame.reindex(index=index, columns=columns,
1413-
copy=False)
1412+
if frame is not None:
1413+
result[key] = frame.reindex(index=index, columns=columns,
1414+
copy=False)
1415+
else:
1416+
result[key] = None
14141417

14151418
return result, index, columns
14161419

@@ -1421,15 +1424,15 @@ def _extract_axis(data, axis=0, intersect=False):
14211424
elif len(data) > 0:
14221425
raw_lengths = []
14231426
indexes = []
1424-
1427+
index = None
14251428
have_raw_arrays = False
14261429
have_frames = False
14271430

14281431
for v in data.values():
14291432
if isinstance(v, DataFrame):
14301433
have_frames = True
14311434
indexes.append(v._get_axis(axis))
1432-
else:
1435+
elif v is not None:
14331436
have_raw_arrays = True
14341437
raw_lengths.append(v.shape[axis])
14351438

@@ -1446,6 +1449,9 @@ def _extract_axis(data, axis=0, intersect=False):
14461449
else:
14471450
index = Index(np.arange(lengths[0]))
14481451

1452+
if index is None:
1453+
index = Index([])
1454+
14491455
return _ensure_index(index)
14501456

14511457

pandas/tests/test_panel.py

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,9 @@ def test_ctor_dict(self):
730730

731731
d = {'A' : itema, 'B' : itemb[5:]}
732732
d2 = {'A' : itema._series, 'B' : itemb[5:]._series}
733-
d3 = {'A' : DataFrame(itema._series),
734-
'B' : DataFrame(itemb[5:]._series)}
733+
d3 = {'A' : None,
734+
'B' : DataFrame(itemb[5:]._series),
735+
'C' : DataFrame(itema._series)}
735736

736737
wp = Panel.from_dict(d)
737738
wp2 = Panel.from_dict(d2) # nested Dict
@@ -748,6 +749,11 @@ def test_ctor_dict(self):
748749
assert_panel_equal(Panel(d2), Panel.from_dict(d2))
749750
assert_panel_equal(Panel(d3), Panel.from_dict(d3))
750751

752+
# a pathological case
753+
d4 = { 'A' : None, 'B' : None }
754+
wp4 = Panel.from_dict(d4)
755+
assert_panel_equal(Panel(d4), Panel(items = ['A','B']))
756+
751757
# cast
752758
dcasted = dict((k, v.reindex(wp.major_axis).fillna(0))
753759
for k, v in d.iteritems())

0 commit comments

Comments
 (0)