Skip to content

Commit 7327645

Browse files
committed
more panel test coverage'
1 parent 437ecde commit 7327645

File tree

6 files changed

+120
-86
lines changed

6 files changed

+120
-86
lines changed

pandas/core/common.py

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def null_out_axis(arr, mask, axis):
8585
#-------------------------------------------------------------------------------
8686
# Lots of little utilities
8787

88+
def _default_index(n):
89+
from pandas.core.index import NULL_INDEX
90+
if n == 0:
91+
return NULL_INDEX
92+
else:
93+
return np.arange(n)
94+
8895
def ensure_float(arr):
8996
if issubclass(arr.dtype.type, np.integer):
9097
arr = arr.astype(float)

pandas/core/frame.py

+2-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import numpy as np
2323

2424
from pandas.core.common import (isnull, notnull, PandasError, _ensure_index,
25-
_try_sort, _pfixed)
25+
_try_sort, _pfixed, _default_index)
2626
from pandas.core.daterange import DateRange
2727
from pandas.core.generic import PandasGeneric
2828
from pandas.core.index import Index, NULL_INDEX
@@ -191,7 +191,7 @@ def _init_matrix(self, values, index, columns, dtype=None,
191191
try:
192192
values = values.astype(dtype)
193193
except Exception:
194-
pass
194+
raise ValueError('failed to cast to %s' % dtype)
195195

196196
N, K = values.shape
197197

@@ -226,11 +226,6 @@ def _constructor(self):
226226
#----------------------------------------------------------------------
227227
# Class behavior
228228

229-
@property
230-
def _is_mixed_type(self):
231-
self._consolidate_inplace()
232-
return len(self._data.blocks) > 1
233-
234229
def __nonzero__(self):
235230
# e.g. "if frame: ..."
236231
return len(self.columns) > 0 and len(self.index) > 0
@@ -701,27 +696,6 @@ def _union_index(self, other):
701696

702697
return union_index
703698

704-
#----------------------------------------------------------------------
705-
# Consolidation of internals
706-
707-
def _consolidate_inplace(self):
708-
self._data = self._data.consolidate()
709-
710-
def consolidate(self):
711-
"""
712-
Compute DataFrame with "consolidated" internals (data of each dtype
713-
grouped together in a single ndarray). Mainly an internal API function,
714-
but available here to the savvy user
715-
716-
Returns
717-
-------
718-
consolidated : DataFrame
719-
"""
720-
cons_data = self._data.consolidate()
721-
if cons_data is self._data:
722-
cons_data = cons_data.copy()
723-
return type(self)(cons_data)
724-
725699
#----------------------------------------------------------------------
726700
# Array interface
727701

@@ -2583,12 +2557,6 @@ def _homogenize_series(data, index, dtype=None):
25832557

25842558
return homogenized
25852559

2586-
def _default_index(n):
2587-
if n == 0:
2588-
return NULL_INDEX
2589-
else:
2590-
return np.arange(n)
2591-
25922560
def _put_str(s, space):
25932561
return ('%s' % s)[:space].ljust(space)
25942562

pandas/core/generic.py

+47-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,17 @@ class NDFrame(object):
2929
N-dimensional labeled array data structure with potentially heterogenous
3030
dtypes along one axis
3131
"""
32-
def __init__(self, data):
33-
pass
32+
def __init__(self, data, axes=None, copy=False):
33+
self._data = data
34+
self.axes = axes
35+
36+
def __repr__(self):
37+
# TODO
38+
return 'NDFrame'
39+
40+
@property
41+
def ndim(self):
42+
return self._data.ndim
3443

3544
class PandasGeneric(Picklable):
3645

@@ -43,6 +52,35 @@ class PandasGeneric(Picklable):
4352

4453
_AXIS_NAMES = dict((v, k) for k, v in _AXIS_NUMBERS.iteritems())
4554

55+
#----------------------------------------------------------------------
56+
# Consolidation of internals
57+
58+
def _consolidate_inplace(self):
59+
self._data = self._data.consolidate()
60+
61+
def consolidate(self):
62+
"""
63+
Compute DataFrame with "consolidated" internals (data of each dtype
64+
grouped together in a single ndarray). Mainly an internal API function,
65+
but available here to the savvy user
66+
67+
Returns
68+
-------
69+
consolidated : DataFrame
70+
"""
71+
cons_data = self._data.consolidate()
72+
if cons_data is self._data:
73+
cons_data = cons_data.copy()
74+
return type(self)(cons_data)
75+
76+
@property
77+
def _is_mixed_type(self):
78+
self._consolidate_inplace()
79+
return len(self._data.blocks) > 1
80+
81+
#----------------------------------------------------------------------
82+
# Axis name business
83+
4684
@classmethod
4785
def _get_axis_number(cls, axis):
4886
axis = cls._AXIS_ALIASES.get(axis, axis)
@@ -108,3 +146,10 @@ def _select_generic(self, crit, axis=0):
108146
new_axis = axis[np.asarray([crit(label) for label in axis])]
109147
return self.reindex(**{axis_name : new_axis})
110148

149+
def _reindex_axis(self, new_index, fill_method, axis):
150+
if axis == 0:
151+
new_data = self._data.reindex_items(new_index)
152+
else:
153+
new_data = self._data.reindex_axis(new_index, axis=axis,
154+
method=fill_method)
155+
return type(self)(new_data)

pandas/core/panel.py

+22-48
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import numpy as np
1111

12-
from pandas.core.common import (PandasError, _mut_exclusive,
13-
_ensure_index, _pfixed)
12+
from pandas.core.common import (PandasError, _mut_exclusive, _ensure_index,
13+
_pfixed, _default_index)
1414
from pandas.core.index import Index
1515
from pandas.core.internals import BlockManager, make_block
1616
from pandas.core.frame import DataFrame
@@ -210,38 +210,20 @@ def __init__(self, data, items=None, major_axis=None, minor_axis=None,
210210
elif isinstance(data, np.ndarray):
211211
mgr = self._init_matrix(data, [items, major_axis, minor_axis],
212212
dtype=dtype, copy=copy)
213-
else:
213+
else: # pragma: no cover
214214
raise PandasError('Panel constructor not properly called!')
215215

216216
self.factors = {}
217217
self._data = mgr
218218

219-
def _consolidate_inplace(self):
220-
self._data = self._data.consolidate()
221-
222-
def consolidate(self):
223-
"""
224-
Compute DataFrame with "consolidated" internals (data of each dtype
225-
grouped together in a single ndarray). Mainly an internal API function,
226-
but available here to the savvy user
227-
228-
Returns
229-
-------
230-
consolidated : DataFrame
231-
"""
232-
cons_data = self._data.consolidate()
233-
if cons_data is self._data:
234-
cons_data = cons_data.copy()
235-
return type(self)(cons_data)
236-
237219
def _init_matrix(self, data, axes, dtype=None, copy=False):
238220
values = _prep_ndarray(data, copy=copy)
239221

240222
if dtype is not None:
241223
try:
242224
values = values.astype(dtype)
243225
except Exception:
244-
pass
226+
raise ValueError('failed to cast to %s' % dtype)
245227

246228
shape = values.shape
247229
fixed_axes = []
@@ -254,7 +236,7 @@ def _init_matrix(self, data, axes, dtype=None, copy=False):
254236

255237
items = fixed_axes[0]
256238
block = make_block(values, items, items)
257-
return BlockManager([block], axes)
239+
return BlockManager([block], fixed_axes)
258240

259241
def _get_plane_axes(self, axis):
260242
"""
@@ -487,47 +469,39 @@ def reindex_like(self, other, method=None):
487469
return self.reindex(major=other.major_axis, items=other.items,
488470
minor=other.minor_axis, method=method)
489471

490-
def _reindex_axis(self, new_index, fill_method, axis):
491-
if axis == 0:
492-
new_data = self._data.reindex_items(new_index)
493-
else:
494-
new_data = self._data.reindex_axis(new_index, axis=axis,
495-
method=fill_method)
496-
return WidePanel(new_data)
497-
498472
def _combine(self, other, func, axis=0):
499473
if isinstance(other, DataFrame):
500-
return self._combineFrame(other, func, axis=axis)
474+
return self._combine_frame(other, func, axis=axis)
501475
elif isinstance(other, Panel):
502-
return self._combinePanel(other, func)
476+
return self._combine_panel(other, func)
503477
elif np.isscalar(other):
504-
newValues = func(self.values, other)
478+
new_values = func(self.values, other)
505479

506-
return WidePanel(newValues, self.items, self.major_axis,
480+
return WidePanel(new_values, self.items, self.major_axis,
507481
self.minor_axis)
508482

509483
def __neg__(self):
510484
return -1 * self
511485

512-
def _combineFrame(self, other, func, axis=0):
486+
def _combine_frame(self, other, func, axis=0):
513487
index, columns = self._get_plane_axes(axis)
514488
axis = self._get_axis_number(axis)
515489

516490
other = other.reindex(index=index, columns=columns)
517491

518492
if axis == 0:
519-
newValues = func(self.values, other.values)
493+
new_values = func(self.values, other.values)
520494
elif axis == 1:
521-
newValues = func(self.values.swapaxes(0, 1), other.values.T)
522-
newValues = newValues.swapaxes(0, 1)
495+
new_values = func(self.values.swapaxes(0, 1), other.values.T)
496+
new_values = new_values.swapaxes(0, 1)
523497
elif axis == 2:
524-
newValues = func(self.values.swapaxes(0, 2), other.values)
525-
newValues = newValues.swapaxes(0, 2)
498+
new_values = func(self.values.swapaxes(0, 2), other.values)
499+
new_values = new_values.swapaxes(0, 2)
526500

527-
return WidePanel(newValues, self.items, self.major_axis,
501+
return WidePanel(new_values, self.items, self.major_axis,
528502
self.minor_axis)
529503

530-
def _combinePanel(self, other, func):
504+
def _combine_panel(self, other, func):
531505
if isinstance(other, LongPanel):
532506
other = other.to_wide()
533507

@@ -1240,14 +1214,14 @@ def __setstate__(self, state):
12401214

12411215
def _combine(self, other, func, axis='items'):
12421216
if isinstance(other, DataFrame):
1243-
return self._combineFrame(other, func, axis=axis)
1217+
return self._combine_frame(other, func, axis=axis)
12441218
elif isinstance(other, Panel):
1245-
return self._combinePanel(other, func)
1219+
return self._combine_panel(other, func)
12461220
elif np.isscalar(other):
12471221
return LongPanel(func(self.values, other), self.items,
12481222
self.index, factors=self.factors)
12491223

1250-
def _combineFrame(self, other, func, axis='items'):
1224+
def _combine_frame(self, other, func, axis='items'):
12511225
"""
12521226
Arithmetic op
12531227
@@ -1262,10 +1236,10 @@ def _combineFrame(self, other, func, axis='items'):
12621236
y : LongPanel
12631237
"""
12641238
wide = self.to_wide()
1265-
result = wide._combineFrame(other, func, axis=axis)
1239+
result = wide._combine_frame(other, func, axis=axis)
12661240
return result.to_long()
12671241

1268-
def _combinePanel(self, other, func):
1242+
def _combine_panel(self, other, func):
12691243
"""
12701244
Arithmetic operation between panels
12711245
"""

pandas/tests/test_frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,8 @@ def test_constructor_more(self):
782782

783783
# can't cast
784784
mat = np.array(['foo', 'bar'], dtype=object).reshape(2, 1)
785-
df = DataFrame(mat, index=[0, 1], columns=[0], dtype=float)
786-
self.assert_(df.values.dtype == np.object_)
785+
self.assertRaises(ValueError, DataFrame, mat, index=[0, 1],
786+
columns=[0], dtype=float)
787787

788788
dm = self.klass(DataFrame(self.frame._series))
789789
tm.assert_frame_equal(dm, self.frame)

pandas/tests/test_panel.py

+40
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ def test_get_axis(self):
165165
assert(self.panel._get_axis(1) is self.panel.major_axis)
166166
assert(self.panel._get_axis(2) is self.panel.minor_axis)
167167

168+
def test_set_axis(self):
169+
new_items = Index(np.arange(len(self.panel.items)))
170+
new_major = Index(np.arange(len(self.panel.major_axis)))
171+
new_minor = Index(np.arange(len(self.panel.minor_axis)))
172+
173+
self.panel.items = new_items
174+
self.assert_(self.panel.items is new_items)
175+
176+
self.panel.major_axis = new_major
177+
self.assert_(self.panel.major_axis is new_major)
178+
179+
self.panel.minor_axis = new_minor
180+
self.assert_(self.panel.minor_axis is new_minor)
181+
168182
def test_get_axis_number(self):
169183
self.assertEqual(self.panel._get_axis_number('items'), 0)
170184
self.assertEqual(self.panel._get_axis_number('major'), 1)
@@ -311,6 +325,32 @@ def setUp(self):
311325
self.panel = common.makeWidePanel()
312326
common.add_nans(self.panel)
313327

328+
def test_constructor(self):
329+
# with BlockManager
330+
wp = WidePanel(self.panel._data)
331+
self.assert_(wp._data is self.panel._data)
332+
333+
wp = WidePanel(self.panel._data, copy=True)
334+
self.assert_(wp._data is not self.panel._data)
335+
assert_panel_equal(wp, self.panel)
336+
337+
def test_constructor_cast(self):
338+
casted = WidePanel(self.panel._data, dtype=int)
339+
casted2 = WidePanel(self.panel.values, dtype=int)
340+
341+
exp_values = self.panel.values.astype(int)
342+
assert_almost_equal(casted.values, exp_values)
343+
assert_almost_equal(casted2.values, exp_values)
344+
345+
def test_consolidate(self):
346+
self.assert_(self.panel._data.is_consolidated())
347+
348+
self.panel['foo'] = 1.
349+
self.assert_(not self.panel._data.is_consolidated())
350+
351+
panel = self.panel.consolidate()
352+
self.assert_(panel._data.is_consolidated())
353+
314354
def test_values(self):
315355
# nothing to test for the moment
316356
values = self.panel.values

0 commit comments

Comments
 (0)