Skip to content

Adding test_map_missing_mixed to test_apply.py in pandas test suite series #20574

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

Merged
merged 9 commits into from
Apr 3, 2018
Merged
10 changes: 10 additions & 0 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,13 @@ def f(x):
result = s.map(f)
exp = pd.Series(['Asia/Tokyo'] * 25, name='XX')
tm.assert_series_equal(result, exp)

@pytest.mark.parametrize("vals,mapping,exp", [
(list('abc'), {np.nan: 'not NaN'}, ['not NaN']),
(list('abc'), {'string': 'another string'}, ['another string']),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your mapping should contain one of the values in the series, so use 'a' instead of 'string'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Riiight

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests did pass

Just not like they should have

(list(range(3)), {42: 'the answer'}, ['the answer'])])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, use 1 instead of 42. Just for consistency make the value numeric as well instead of 'the answer'

def test_map_missing_mixed(self, vals, mapping, exp):
s = pd.Series(vals + [list(mapping.keys())[0]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the mapping keys here. s = pd.Series(vals + [np.nan]) is all you need

result = s.map(mapping)

tm.assert_series_equal(result[-1:].reset_index(drop=True), pd.Series(exp))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think through the exp values you are passing in. They should obviously match the shape of your input but replace with NA values where appropriate.

For your first example, if you did list('abc') as your val {'a': 'foo'} as your mapping then your exp would be ['foo', np.nan, np.nan, np.nan].

I'm not sure what you are trying to do with result[-1:].reset_index(drop=True) but that's getting way too complicated. If you follow all of the above steps you can just do tm.assert_series_equal(result, pd.Series(exp))