/
test_generic.py
222 lines (170 loc) · 6.99 KB
/
test_generic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# pylint: disable-msg=E1101,W0612
from datetime import datetime, timedelta
import operator
import unittest
import nose
import numpy as np
import pandas as pd
from pandas import (Index, Series, DataFrame, Panel,
isnull, notnull,date_range)
from pandas.core.index import Index, MultiIndex
from pandas.tseries.index import Timestamp, DatetimeIndex
import pandas.core.common as com
from pandas.compat import StringIO, lrange, range, zip, u, OrderedDict, long
from pandas import compat
from pandas.util.testing import (assert_series_equal,
assert_frame_equal,
assert_panel_equal,
assert_almost_equal,
ensure_clean)
import pandas.util.testing as tm
#------------------------------------------------------------------------------
# Generic types test cases
class Generic(object):
_multiprocess_can_split_ = True
def setUp(self):
import warnings
warnings.filterwarnings(action='ignore', category=FutureWarning)
@property
def _ndim(self):
return self._typ._AXIS_LEN
def _axes(self):
""" return the axes for my object typ """
return self._typ._AXIS_ORDERS
def _construct(self, shape, value=None, **kwargs):
""" construct an object for the given shape
if value is specified use that if its a scalar
if value is an array, repeat it as needed """
if isinstance(shape,int):
shape = tuple([shape] * self._ndim)
if value is not None:
if np.isscalar(value):
if value == 'empty':
arr = None
# remove the info axis
kwargs.pop(self._typ._info_axis_name,None)
else:
arr = np.empty(shape)
arr.fill(value)
else:
fshape = np.prod(shape)
arr = value.ravel()
new_shape = fshape/arr.shape[0]
if fshape % arr.shape[0] != 0:
raise Exception("invalid value passed in _construct")
arr = np.repeat(arr,new_shape).reshape(shape)
else:
arr = np.random.randn(*shape)
return self._typ(arr,**kwargs)
def _compare(self, result, expected):
self._comparator(result,expected)
def test_rename(self):
# single axis
for axis in self._axes():
kwargs = { axis : list('ABCD') }
obj = self._construct(4,**kwargs)
# no values passed
#self.assertRaises(Exception, o.rename(str.lower))
# rename a single axis
result = obj.rename(**{ axis : str.lower })
expected = obj.copy()
setattr(expected,axis,list('abcd'))
self._compare(result, expected)
# multiple axes at once
def test_get_numeric_data(self):
n = 4
kwargs = { }
for i in range(self._ndim):
kwargs[self._typ._AXIS_NAMES[i]] = list(range(n))
# get the numeric data
o = self._construct(n,**kwargs)
result = o._get_numeric_data()
self._compare(result, o)
# non-inclusion
result = o._get_bool_data()
expected = self._construct(n,value='empty',**kwargs)
self._compare(result,expected)
# get the bool data
arr = np.array([True,True,False,True])
o = self._construct(n,value=arr,**kwargs)
result = o._get_numeric_data()
self._compare(result, o)
# _get_numeric_data is includes _get_bool_data, so can't test for non-inclusion
def test_nonzero(self):
# GH 4633
# look at the boolean/nonzero behavior for objects
obj = self._construct(shape=4)
self.assertRaises(ValueError, lambda : bool(obj == 0))
self.assertRaises(ValueError, lambda : bool(obj == 1))
self.assertRaises(ValueError, lambda : bool(obj))
obj = self._construct(shape=4,value=1)
self.assertRaises(ValueError, lambda : bool(obj == 0))
self.assertRaises(ValueError, lambda : bool(obj == 1))
self.assertRaises(ValueError, lambda : bool(obj))
obj = self._construct(shape=4,value=np.nan)
self.assertRaises(ValueError, lambda : bool(obj == 0))
self.assertRaises(ValueError, lambda : bool(obj == 1))
self.assertRaises(ValueError, lambda : bool(obj))
# empty
obj = self._construct(shape=0)
self.assertRaises(ValueError, lambda : bool(obj))
# invalid behaviors
obj1 = self._construct(shape=4,value=1)
obj2 = self._construct(shape=4,value=1)
def f():
if obj1:
print("this works and shouldn't")
self.assertRaises(ValueError, f)
self.assertRaises(ValueError, lambda : obj1 and obj2)
self.assertRaises(ValueError, lambda : obj1 or obj2)
self.assertRaises(ValueError, lambda : not obj1)
class TestSeries(unittest.TestCase, Generic):
_typ = Series
_comparator = lambda self, x, y: assert_series_equal(x,y)
def test_rename_mi(self):
s = Series([11,21,31],
index=MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]]))
result = s.rename(str.lower)
def test_get_numeric_data_preserve_dtype(self):
# get the numeric data
o = Series([1,2,3])
result = o._get_numeric_data()
self._compare(result, o)
o = Series([1,'2',3.])
result = o._get_numeric_data()
expected = Series([],dtype=object)
self._compare(result, expected)
o = Series([True,False,True])
result = o._get_numeric_data()
self._compare(result, o)
o = Series([True,False,True])
result = o._get_bool_data()
self._compare(result, o)
o = Series(date_range('20130101',periods=3))
result = o._get_numeric_data()
expected = Series([],dtype='M8[ns]')
self._compare(result, expected)
def test_nonzero_single_element(self):
s = Series([True])
self.assertRaises(ValueError, lambda : bool(s))
s = Series([False])
self.assertRaises(ValueError, lambda : bool(s))
class TestDataFrame(unittest.TestCase, Generic):
_typ = DataFrame
_comparator = lambda self, x, y: assert_frame_equal(x,y)
def test_rename_mi(self):
df = DataFrame([11,21,31],
index=MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]]))
result = df.rename(str.lower)
def test_get_numeric_data_preserve_dtype(self):
# get the numeric data
o = DataFrame({'A' : [1,'2',3.] })
result = o._get_numeric_data()
expected = DataFrame(index=[0,1,2],dtype=object)
self._compare(result, expected)
class TestPanel(unittest.TestCase, Generic):
_typ = Panel
_comparator = lambda self, x, y: assert_panel_equal(x,y)
if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)