Skip to content

Commit 8662cb9

Browse files
sinhrksjreback
authored andcommitted
TST: assert_dict_equal to check input type
Author: sinhrks <[email protected]> Closes pandas-dev#13264 from sinhrks/test_dict and squashes the following commits: 2a7b9b1 [sinhrks] TST: assert_dict_equal to check input type
1 parent 9a6ce07 commit 8662cb9

11 files changed

+77
-31
lines changed

pandas/tests/frame/test_constructors.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ def test_constructor_dict(self):
220220
frame = DataFrame({'col1': self.ts1,
221221
'col2': self.ts2})
222222

223-
tm.assert_dict_equal(self.ts1, frame['col1'], compare_keys=False)
224-
tm.assert_dict_equal(self.ts2, frame['col2'], compare_keys=False)
223+
# col2 is padded with NaN
224+
self.assertEqual(len(self.ts1), 30)
225+
self.assertEqual(len(self.ts2), 25)
226+
227+
tm.assert_series_equal(self.ts1, frame['col1'], check_names=False)
228+
exp = pd.Series(np.concatenate([[np.nan] * 5, self.ts2.values]),
229+
index=self.ts1.index, name='col2')
230+
tm.assert_series_equal(exp, frame['col2'])
225231

226232
frame = DataFrame({'col1': self.ts1,
227233
'col2': self.ts2},

pandas/tests/frame/test_indexing.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,17 @@ def test_setitem(self):
393393
series = self.frame['A'][::2]
394394
self.frame['col5'] = series
395395
self.assertIn('col5', self.frame)
396-
tm.assert_dict_equal(series, self.frame['col5'],
397-
compare_keys=False)
396+
397+
self.assertEqual(len(series), 15)
398+
self.assertEqual(len(self.frame), 30)
399+
400+
exp = np.ravel(np.column_stack((series.values, [np.nan] * 15)))
401+
exp = Series(exp, index=self.frame.index, name='col5')
402+
tm.assert_series_equal(self.frame['col5'], exp)
398403

399404
series = self.frame['A']
400405
self.frame['col6'] = series
401-
tm.assert_dict_equal(series, self.frame['col6'],
402-
compare_keys=False)
406+
tm.assert_series_equal(series, self.frame['col6'], check_names=False)
403407

404408
with tm.assertRaises(KeyError):
405409
self.frame[randn(len(self.frame) + 1)] = 1

pandas/tests/frame/test_operators.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,14 @@ def test_combineFrame(self):
724724
frame_copy['C'][:5] = nan
725725

726726
added = self.frame + frame_copy
727-
tm.assert_dict_equal(added['A'].valid(),
728-
self.frame['A'] * 2,
729-
compare_keys=False)
727+
728+
indexer = added['A'].valid().index
729+
exp = (self.frame['A'] * 2).copy()
730+
731+
tm.assert_series_equal(added['A'].valid(), exp.loc[indexer])
732+
733+
exp.loc[~exp.index.isin(indexer)] = np.nan
734+
tm.assert_series_equal(added['A'], exp.loc[added['A'].index])
730735

731736
self.assertTrue(
732737
np.isnan(added['C'].reindex(frame_copy.index)[:5]).all())

pandas/tests/frame/test_timeseries.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ def test_pct_change_shift_over_nas(self):
120120
def test_shift(self):
121121
# naive shift
122122
shiftedFrame = self.tsframe.shift(5)
123-
self.assertTrue(shiftedFrame.index.equals(self.tsframe.index))
123+
self.assert_index_equal(shiftedFrame.index, self.tsframe.index)
124124

125125
shiftedSeries = self.tsframe['A'].shift(5)
126126
assert_series_equal(shiftedFrame['A'], shiftedSeries)
127127

128128
shiftedFrame = self.tsframe.shift(-5)
129-
self.assertTrue(shiftedFrame.index.equals(self.tsframe.index))
129+
self.assert_index_equal(shiftedFrame.index, self.tsframe.index)
130130

131131
shiftedSeries = self.tsframe['A'].shift(-5)
132132
assert_series_equal(shiftedFrame['A'], shiftedSeries)
@@ -154,10 +154,10 @@ def test_shift(self):
154154
ps = tm.makePeriodFrame()
155155
shifted = ps.shift(1)
156156
unshifted = shifted.shift(-1)
157-
self.assertTrue(shifted.index.equals(ps.index))
158-
159-
tm.assert_dict_equal(unshifted.ix[:, 0].valid(), ps.ix[:, 0],
160-
compare_keys=False)
157+
self.assert_index_equal(shifted.index, ps.index)
158+
self.assert_index_equal(unshifted.index, ps.index)
159+
tm.assert_numpy_array_equal(unshifted.ix[:, 0].valid().values,
160+
ps.ix[:-1, 0].values)
161161

162162
shifted2 = ps.shift(1, 'B')
163163
shifted3 = ps.shift(1, datetools.bday)

pandas/tests/series/test_analytics.py

+1
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,7 @@ def test_idxmax(self):
12791279
self.assertEqual(result, 1.1)
12801280

12811281
def test_numpy_argmax(self):
1282+
12821283
# argmax is aliased to idxmax
12831284
data = np.random.randint(0, 11, size=10)
12841285
result = np.argmax(Series(data))

pandas/tests/series/test_combine_concat.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def test_combine_first(self):
6565

6666
combined = strings.combine_first(floats)
6767

68-
tm.assert_dict_equal(strings, combined, compare_keys=False)
69-
tm.assert_dict_equal(floats[1::2], combined, compare_keys=False)
68+
tm.assert_series_equal(strings, combined.loc[index[::2]])
69+
tm.assert_series_equal(floats[1::2].astype(object),
70+
combined.loc[index[1::2]])
7071

7172
# corner case
7273
s = Series([1., 2, 3], index=[0, 1, 2])

pandas/tests/series/test_missing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ def test_valid(self):
433433

434434
result = ts.valid()
435435
self.assertEqual(len(result), ts.count())
436-
437-
tm.assert_dict_equal(result, ts, compare_keys=False)
436+
tm.assert_series_equal(result, ts[1::2])
437+
tm.assert_series_equal(result, ts[pd.notnull(ts)])
438438

439439
def test_isnull(self):
440440
ser = Series([0, 5.4, 3, nan, -0.001])

pandas/tests/series/test_timeseries.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def test_shift(self):
2525
shifted = self.ts.shift(1)
2626
unshifted = shifted.shift(-1)
2727

28-
tm.assert_dict_equal(unshifted.valid(), self.ts, compare_keys=False)
28+
tm.assert_index_equal(shifted.index, self.ts.index)
29+
tm.assert_index_equal(unshifted.index, self.ts.index)
30+
tm.assert_numpy_array_equal(unshifted.valid().values,
31+
self.ts.values[:-1])
2932

3033
offset = datetools.bday
3134
shifted = self.ts.shift(1, freq=offset)
@@ -49,7 +52,9 @@ def test_shift(self):
4952
ps = tm.makePeriodSeries()
5053
shifted = ps.shift(1)
5154
unshifted = shifted.shift(-1)
52-
tm.assert_dict_equal(unshifted.valid(), ps, compare_keys=False)
55+
tm.assert_index_equal(shifted.index, ps.index)
56+
tm.assert_index_equal(unshifted.index, ps.index)
57+
tm.assert_numpy_array_equal(unshifted.valid().values, ps.values[:-1])
5358

5459
shifted2 = ps.shift(1, 'B')
5560
shifted3 = ps.shift(1, datetools.bday)
@@ -77,16 +82,16 @@ def test_shift(self):
7782

7883
# xref 8260
7984
# with tz
80-
s = Series(
81-
date_range('2000-01-01 09:00:00', periods=5,
82-
tz='US/Eastern'), name='foo')
85+
s = Series(date_range('2000-01-01 09:00:00', periods=5,
86+
tz='US/Eastern'), name='foo')
8387
result = s - s.shift()
84-
assert_series_equal(result, Series(
85-
TimedeltaIndex(['NaT'] + ['1 days'] * 4), name='foo'))
88+
89+
exp = Series(TimedeltaIndex(['NaT'] + ['1 days'] * 4), name='foo')
90+
assert_series_equal(result, exp)
8691

8792
# incompat tz
88-
s2 = Series(
89-
date_range('2000-01-01 09:00:00', periods=5, tz='CET'), name='foo')
93+
s2 = Series(date_range('2000-01-01 09:00:00', periods=5,
94+
tz='CET'), name='foo')
9095
self.assertRaises(ValueError, lambda: s - s2)
9196

9297
def test_tshift(self):

pandas/tseries/base.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,17 @@ def __contains__(self, key):
189189
return False
190190

191191
def __getitem__(self, key):
192+
"""
193+
This getitem defers to the underlying array, which by-definition can
194+
only handle list-likes, slices, and integer scalars
195+
"""
196+
197+
is_int = is_integer(key)
198+
if lib.isscalar(key) and not is_int:
199+
raise ValueError
200+
192201
getitem = self._data.__getitem__
193-
if lib.isscalar(key):
202+
if is_int:
194203
val = getitem(key)
195204
return self._box_func(val)
196205
else:

pandas/util/testing.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ def assert_almost_equal(left, right, check_exact=False, **kwargs):
131131
return _testing.assert_almost_equal(left, right, **kwargs)
132132

133133

134-
assert_dict_equal = _testing.assert_dict_equal
134+
def assert_dict_equal(left, right, compare_keys=True):
135+
136+
# instance validation
137+
assertIsInstance(left, dict, '[dict] ')
138+
assertIsInstance(right, dict, '[dict] ')
139+
140+
return _testing.assert_dict_equal(left, right, compare_keys=compare_keys)
135141

136142

137143
def randbool(size=(), p=0.5):

pandas/util/validators.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ def _check_for_default_values(fname, arg_val_dict, compat_args):
4242
# as comparison may have been overriden for the left
4343
# hand object
4444
try:
45-
match = (arg_val_dict[key] == compat_args[key])
45+
v1 = arg_val_dict[key]
46+
v2 = compat_args[key]
47+
48+
# check for None-ness otherwise we could end up
49+
# comparing a numpy array vs None
50+
if (v1 is not None and v2 is None) or \
51+
(v1 is None and v2 is not None):
52+
match = False
53+
else:
54+
match = (v1 == v2)
4655

4756
if not is_bool(match):
4857
raise ValueError("'match' is not a boolean")

0 commit comments

Comments
 (0)