Skip to content

TST: assert_dict_equal to check input type #13264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ def test_constructor_dict(self):
frame = DataFrame({'col1': self.ts1,
'col2': self.ts2})

tm.assert_dict_equal(self.ts1, frame['col1'], compare_keys=False)
tm.assert_dict_equal(self.ts2, frame['col2'], compare_keys=False)
# col2 is padded with NaN
self.assertEqual(len(self.ts1), 30)
self.assertEqual(len(self.ts2), 25)

tm.assert_series_equal(self.ts1, frame['col1'], check_names=False)
exp = pd.Series(np.concatenate([[np.nan] * 5, self.ts2.values]),
index=self.ts1.index, name='col2')
tm.assert_series_equal(exp, frame['col2'])

frame = DataFrame({'col1': self.ts1,
'col2': self.ts2},
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,17 @@ def test_setitem(self):
series = self.frame['A'][::2]
self.frame['col5'] = series
self.assertIn('col5', self.frame)
tm.assert_dict_equal(series, self.frame['col5'],
compare_keys=False)

self.assertEqual(len(series), 15)
self.assertEqual(len(self.frame), 30)

exp = np.ravel(np.column_stack((series.values, [np.nan] * 15)))
exp = Series(exp, index=self.frame.index, name='col5')
tm.assert_series_equal(self.frame['col5'], exp)

series = self.frame['A']
self.frame['col6'] = series
tm.assert_dict_equal(series, self.frame['col6'],
compare_keys=False)
tm.assert_series_equal(series, self.frame['col6'], check_names=False)

with tm.assertRaises(KeyError):
self.frame[randn(len(self.frame) + 1)] = 1
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,14 @@ def test_combineFrame(self):
frame_copy['C'][:5] = nan

added = self.frame + frame_copy
tm.assert_dict_equal(added['A'].valid(),
self.frame['A'] * 2,
compare_keys=False)

indexer = added['A'].valid().index
exp = (self.frame['A'] * 2).copy()

tm.assert_series_equal(added['A'].valid(), exp.loc[indexer])

exp.loc[~exp.index.isin(indexer)] = np.nan
tm.assert_series_equal(added['A'], exp.loc[added['A'].index])

self.assertTrue(
np.isnan(added['C'].reindex(frame_copy.index)[:5]).all())
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ def test_pct_change_shift_over_nas(self):
def test_shift(self):
# naive shift
shiftedFrame = self.tsframe.shift(5)
self.assertTrue(shiftedFrame.index.equals(self.tsframe.index))
self.assert_index_equal(shiftedFrame.index, self.tsframe.index)

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

shiftedFrame = self.tsframe.shift(-5)
self.assertTrue(shiftedFrame.index.equals(self.tsframe.index))
self.assert_index_equal(shiftedFrame.index, self.tsframe.index)

shiftedSeries = self.tsframe['A'].shift(-5)
assert_series_equal(shiftedFrame['A'], shiftedSeries)
Expand Down Expand Up @@ -154,10 +154,10 @@ def test_shift(self):
ps = tm.makePeriodFrame()
shifted = ps.shift(1)
unshifted = shifted.shift(-1)
self.assertTrue(shifted.index.equals(ps.index))

tm.assert_dict_equal(unshifted.ix[:, 0].valid(), ps.ix[:, 0],
compare_keys=False)
self.assert_index_equal(shifted.index, ps.index)
self.assert_index_equal(unshifted.index, ps.index)
tm.assert_numpy_array_equal(unshifted.ix[:, 0].valid().values,
ps.ix[:-1, 0].values)

shifted2 = ps.shift(1, 'B')
shifted3 = ps.shift(1, datetools.bday)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/series/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def test_combine_first(self):

combined = strings.combine_first(floats)

tm.assert_dict_equal(strings, combined, compare_keys=False)
tm.assert_dict_equal(floats[1::2], combined, compare_keys=False)
tm.assert_series_equal(strings, combined.loc[index[::2]])
tm.assert_series_equal(floats[1::2].astype(object),
combined.loc[index[1::2]])

# corner case
s = Series([1., 2, 3], index=[0, 1, 2])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ def test_valid(self):

result = ts.valid()
self.assertEqual(len(result), ts.count())

tm.assert_dict_equal(result, ts, compare_keys=False)
tm.assert_series_equal(result, ts[1::2])
tm.assert_series_equal(result, ts[pd.notnull(ts)])

def test_isnull(self):
ser = Series([0, 5.4, 3, nan, -0.001])
Expand Down
23 changes: 14 additions & 9 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def test_shift(self):
shifted = self.ts.shift(1)
unshifted = shifted.shift(-1)

tm.assert_dict_equal(unshifted.valid(), self.ts, compare_keys=False)
tm.assert_index_equal(shifted.index, self.ts.index)
tm.assert_index_equal(unshifted.index, self.ts.index)
tm.assert_numpy_array_equal(unshifted.valid().values,
self.ts.values[:-1])

offset = datetools.bday
shifted = self.ts.shift(1, freq=offset)
Expand All @@ -49,7 +52,9 @@ def test_shift(self):
ps = tm.makePeriodSeries()
shifted = ps.shift(1)
unshifted = shifted.shift(-1)
tm.assert_dict_equal(unshifted.valid(), ps, compare_keys=False)
tm.assert_index_equal(shifted.index, ps.index)
tm.assert_index_equal(unshifted.index, ps.index)
tm.assert_numpy_array_equal(unshifted.valid().values, ps.values[:-1])

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

# xref 8260
# with tz
s = Series(
date_range('2000-01-01 09:00:00', periods=5,
tz='US/Eastern'), name='foo')
s = Series(date_range('2000-01-01 09:00:00', periods=5,
tz='US/Eastern'), name='foo')
result = s - s.shift()
assert_series_equal(result, Series(
TimedeltaIndex(['NaT'] + ['1 days'] * 4), name='foo'))

exp = Series(TimedeltaIndex(['NaT'] + ['1 days'] * 4), name='foo')
assert_series_equal(result, exp)

# incompat tz
s2 = Series(
date_range('2000-01-01 09:00:00', periods=5, tz='CET'), name='foo')
s2 = Series(date_range('2000-01-01 09:00:00', periods=5,
tz='CET'), name='foo')
self.assertRaises(ValueError, lambda: s - s2)

def test_tshift(self):
Expand Down
8 changes: 7 additions & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ def assert_almost_equal(left, right, check_exact=False, **kwargs):
return _testing.assert_almost_equal(left, right, **kwargs)


assert_dict_equal = _testing.assert_dict_equal
def assert_dict_equal(left, right, compare_keys=True):

# instance validation
assertIsInstance(left, dict, '[dict] ')
assertIsInstance(right, dict, '[dict] ')

return _testing.assert_dict_equal(left, right, compare_keys=compare_keys)


def randbool(size=(), p=0.5):
Expand Down