Skip to content

Commit cc8bf97

Browse files
committed
support constructing Panel or Panel4D with scalar data, fixes pandas-dev#8285
1 parent 09ea608 commit cc8bf97

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,4 @@ Bug Fixes
622622
- Bug in ``Series.values_counts`` with excluding ``NaN`` for categorical type ``Series`` with ``dropna=True`` (:issue:`9443`)
623623

624624
- Fixed mising numeric_only option for ``DataFrame.std/var/sem`` (:issue:`9201`)
625+
- Support constructing ``Panel`` or ``Panel4D`` with scalar data (:issue:`8285`)

pandas/core/panel.py

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ def _init_data(self, data, copy, dtype, **kwargs):
165165
mgr = self._init_matrix(data, passed_axes, dtype=dtype, copy=copy)
166166
copy = False
167167
dtype = None
168+
elif lib.isscalar(data) and all(x is not None for x in passed_axes):
169+
if dtype is None:
170+
dtype, data = _infer_dtype_from_scalar(data)
171+
values = np.empty([len(x) for x in passed_axes], dtype=dtype)
172+
values.fill(data)
173+
mgr = self._init_matrix(values, passed_axes, dtype=dtype, copy=False)
174+
copy = False
168175
else: # pragma: no cover
169176
raise PandasError('Panel constructor not properly called!')
170177

pandas/tests/test_panel.py

+15
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,21 @@ def test_constructor(self):
888888
wp = Panel(vals, copy=True)
889889
self.assertIsNot(wp.values, vals)
890890

891+
# GH #8285, test when scalar data is used to construct a Panel
892+
# if dtype is not passed, it should be inferred
893+
value_and_dtype = [(1, int), (3.14, float), ('foo', np.object_)]
894+
for (val, dtype) in value_and_dtype:
895+
wp = Panel(val, items=range(2), major_axis=range(3), minor_axis=range(4))
896+
vals = np.empty((2, 3, 4), dtype=dtype)
897+
vals.fill(val)
898+
assert_panel_equal(wp, Panel(vals, dtype=dtype))
899+
900+
# test the case when dtype is passed
901+
wp = Panel(1, items=range(2), major_axis=range(3), minor_axis=range(4), dtype=float)
902+
vals = np.empty((2, 3, 4), dtype=float)
903+
vals.fill(1)
904+
assert_panel_equal(wp, Panel(vals, dtype=float))
905+
891906
def test_constructor_cast(self):
892907
zero_filled = self.panel.fillna(0)
893908

pandas/tests/test_panel4d.py

+15
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,21 @@ def test_constructor(self):
629629
panel4d = Panel4D(vals, copy=True)
630630
self.assertIsNot(panel4d.values, vals)
631631

632+
# GH #8285, test when scalar data is used to construct a Panel4D
633+
# if dtype is not passed, it should be inferred
634+
value_and_dtype = [(1, int), (3.14, float), ('foo', np.object_)]
635+
for (val, dtype) in value_and_dtype:
636+
panel4d = Panel4D(val, labels=range(2), items=range(3), major_axis=range(4), minor_axis=range(5))
637+
vals = np.empty((2, 3, 4, 5), dtype=dtype)
638+
vals.fill(val)
639+
assert_panel4d_equal(panel4d, Panel4D(vals, dtype=dtype))
640+
641+
# test the case when dtype is passed
642+
panel4d = Panel4D(1, labels=range(2), items=range(3), major_axis=range(4), minor_axis=range(5), dtype=float)
643+
vals = np.empty((2, 3, 4, 5), dtype=float)
644+
vals.fill(1)
645+
assert_panel4d_equal(panel4d, Panel4D(vals, dtype=float))
646+
632647
def test_constructor_cast(self):
633648
zero_filled = self.panel4d.fillna(0)
634649

0 commit comments

Comments
 (0)