-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
TST/CLN: series.duplicated; parametrisation; fix warning #21899
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ca9dee
TST/CLN: series.duplicated; parametrisation; fix warning
h-vetinari c06442b
Incorporate review (jreback)
h-vetinari 92d708c
Split off duplicates/unique tests into separate file
h-vetinari 12d2888
Change tests away from classes; reorder conftest.py
h-vetinari c9a3f71
Typos
h-vetinari d34b7d1
Further cleanup
h-vetinari e640040
Remove space
h-vetinari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Change tests away from classes; reorder conftest.py
- Loading branch information
commit 12d288835e8660f8a2981f59ada78baea16c6bc1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,133 +10,136 @@ | |
|
||
from pandas.util.testing import assert_series_equal | ||
import pandas.util.testing as tm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use tm; don’t import assert_series_equal |
||
from .common import TestData | ||
|
||
|
||
class TestSeriesDuplicates(TestData): | ||
|
||
def test_value_counts_nunique(self): | ||
|
||
# basics.rst doc example | ||
series = Series(np.random.randn(500)) | ||
series[20:500] = np.nan | ||
series[10:20] = 5000 | ||
result = series.nunique() | ||
assert result == 11 | ||
|
||
# GH 18051 | ||
s = pd.Series(pd.Categorical([])) | ||
assert s.nunique() == 0 | ||
s = pd.Series(pd.Categorical([np.nan])) | ||
assert s.nunique() == 0 | ||
|
||
def test_unique(self): | ||
|
||
# 714 also, dtype=float | ||
s = Series([1.2345] * 100) | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
s = Series([1.2345] * 100, dtype='f4') | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
# NAs in object arrays #714 | ||
s = Series(['foo'] * 100, dtype='O') | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
# decision about None | ||
s = Series([1, 2, 3, None, None, None], dtype=object) | ||
result = s.unique() | ||
expected = np.array([1, 2, 3, None], dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# GH 18051 | ||
s = pd.Series(pd.Categorical([])) | ||
tm.assert_categorical_equal(s.unique(), pd.Categorical([]), | ||
check_dtype=False) | ||
s = pd.Series(pd.Categorical([np.nan])) | ||
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]), | ||
check_dtype=False) | ||
|
||
def test_unique_data_ownership(self): | ||
# it works! #1807 | ||
Series(Series(["a", "c", "b"]).unique()).sort_values() | ||
|
||
def test_is_unique(self): | ||
# GH11946 | ||
s = Series(np.random.randint(0, 10, size=1000)) | ||
assert not s.is_unique | ||
s = Series(np.arange(1000)) | ||
assert s.is_unique | ||
|
||
def test_is_unique_class_ne(self, capsys): | ||
# GH 20661 | ||
class Foo(object): | ||
def __init__(self, val): | ||
self._value = val | ||
|
||
def __ne__(self, other): | ||
raise Exception("NEQ not supported") | ||
|
||
li = [Foo(i) for i in range(5)] | ||
s = pd.Series(li, index=[i for i in range(5)]) | ||
_, err = capsys.readouterr() | ||
s.is_unique | ||
_, err = capsys.readouterr() | ||
assert len(err) == 0 | ||
|
||
@pytest.mark.parametrize( | ||
'keep, expected', | ||
[ | ||
('first', Series([False, False, False, False, True, True, False])), | ||
('last', Series([False, True, True, False, False, False, False])), | ||
(False, Series([False, True, True, False, True, True, False])) | ||
]) | ||
def test_drop_duplicates_non_bool(self, any_numpy_dtype, keep, expected): | ||
tc = Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype(any_numpy_dtype)) | ||
|
||
assert_series_equal(tc.duplicated(keep=keep), expected) | ||
assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected]) | ||
sc = tc.copy() | ||
sc.drop_duplicates(keep=keep, inplace=True) | ||
assert_series_equal(sc, tc[~expected]) | ||
|
||
@pytest.mark.parametrize('keep, expected', | ||
[('first', Series([False, False, True, True])), | ||
('last', Series([True, True, False, False])), | ||
(False, Series([True, True, True, True]))]) | ||
def test_drop_duplicates_bool(self, keep, expected): | ||
tc = Series([True, False, True, False]) | ||
|
||
assert_series_equal(tc.duplicated(keep=keep), expected) | ||
assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected]) | ||
sc = tc.copy() | ||
sc.drop_duplicates(keep=keep, inplace=True) | ||
assert_series_equal(sc, tc[~expected]) | ||
|
||
@pytest.mark.parametrize('keep, expected', [ | ||
('first', Series([False, False, True, False, True], name='name')), | ||
('last', Series([True, True, False, False, False], name='name')), | ||
(False, Series([True, True, True, False, True], name='name')) | ||
]) | ||
def test_duplicated_keep(self, keep, expected): | ||
s = Series(['a', 'b', 'b', 'c', 'a'], name='name') | ||
|
||
result = s.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('keep, expected', [ | ||
('first', Series([False, False, True, False, True])), | ||
('last', Series([True, True, False, False, False])), | ||
(False, Series([True, True, True, False, True])) | ||
def test_value_counts_nunique(): | ||
# basics.rst doc example | ||
series = Series(np.random.randn(500)) | ||
series[20:500] = np.nan | ||
series[10:20] = 5000 | ||
result = series.nunique() | ||
assert result == 11 | ||
|
||
# GH 18051 | ||
s = pd.Series(pd.Categorical([])) | ||
assert s.nunique() == 0 | ||
s = pd.Series(pd.Categorical([np.nan])) | ||
assert s.nunique() == 0 | ||
|
||
|
||
def test_unique(): | ||
# GH714 also, dtype=float | ||
s = Series([1.2345] * 100) | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
s = Series([1.2345] * 100, dtype='f4') | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
# NAs in object arrays #714 | ||
s = Series(['foo'] * 100, dtype='O') | ||
s[::2] = np.nan | ||
result = s.unique() | ||
assert len(result) == 2 | ||
|
||
# decision about None | ||
s = Series([1, 2, 3, None, None, None], dtype=object) | ||
result = s.unique() | ||
expected = np.array([1, 2, 3, None], dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# GH 18051 | ||
s = pd.Series(pd.Categorical([])) | ||
tm.assert_categorical_equal(s.unique(), pd.Categorical([]), | ||
check_dtype=False) | ||
s = pd.Series(pd.Categorical([np.nan])) | ||
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]), | ||
check_dtype=False) | ||
|
||
|
||
def test_unique_data_ownership(): | ||
# it works! #1807 | ||
Series(Series(["a", "c", "b"]).unique()).sort_values() | ||
|
||
|
||
def test_is_unique(): | ||
# GH11946 | ||
s = Series(np.random.randint(0, 10, size=1000)) | ||
assert not s.is_unique | ||
s = Series(np.arange(1000)) | ||
assert s.is_unique | ||
|
||
|
||
def test_is_unique_class_ne(capsys): | ||
# GH 20661 | ||
class Foo(object): | ||
def __init__(self, val): | ||
self._value = val | ||
|
||
def __ne__(self, other): | ||
raise Exception("NEQ not supported") | ||
|
||
li = [Foo(i) for i in range(5)] | ||
s = pd.Series(li, index=[i for i in range(5)]) | ||
_, err = capsys.readouterr() | ||
s.is_unique | ||
_, err = capsys.readouterr() | ||
assert len(err) == 0 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'keep, expected', | ||
[ | ||
('first', Series([False, False, False, False, True, True, False])), | ||
('last', Series([False, True, True, False, False, False, False])), | ||
(False, Series([False, True, True, False, True, True, False])) | ||
]) | ||
def test_duplicated_nan_none(self, keep, expected): | ||
s = Series([np.nan, 3, 3, None, np.nan], dtype=object) | ||
|
||
result = s.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) | ||
def test_drop_duplicates_non_bool(any_numpy_dtype, keep, expected): | ||
tc = Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype(any_numpy_dtype)) | ||
|
||
assert_series_equal(tc.duplicated(keep=keep), expected) | ||
assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected]) | ||
sc = tc.copy() | ||
sc.drop_duplicates(keep=keep, inplace=True) | ||
assert_series_equal(sc, tc[~expected]) | ||
|
||
|
||
@pytest.mark.parametrize('keep, expected', | ||
[('first', Series([False, False, True, True])), | ||
('last', Series([True, True, False, False])), | ||
(False, Series([True, True, True, True]))]) | ||
def test_drop_duplicates_bool(keep, expected): | ||
tc = Series([True, False, True, False]) | ||
|
||
assert_series_equal(tc.duplicated(keep=keep), expected) | ||
assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected]) | ||
sc = tc.copy() | ||
sc.drop_duplicates(keep=keep, inplace=True) | ||
assert_series_equal(sc, tc[~expected]) | ||
|
||
|
||
@pytest.mark.parametrize('keep, expected', [ | ||
('first', Series([False, False, True, False, True], name='name')), | ||
('last', Series([True, True, False, False, False], name='name')), | ||
(False, Series([True, True, True, False, True], name='name')) | ||
]) | ||
def test_duplicated_keep(keep, expected): | ||
s = Series(['a', 'b', 'b', 'c', 'a'], name='name') | ||
|
||
result = s.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize('keep, expected', [ | ||
('first', Series([False, False, True, False, True])), | ||
('last', Series([True, True, False, False, False])), | ||
(False, Series([True, True, True, False, True])) | ||
]) | ||
def test_duplicated_nan_none(keep, expected): | ||
s = Series([np.nan, 3, 3, None, np.nan], dtype=object) | ||
|
||
result = s.duplicated(keep=keep) | ||
tm.assert_series_equal(result, expected) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gfyoung this is ok? it follows our existing pattern?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback @gfyoung I only added
COMPLEX_DTYPES
,STRING_DTYPES
(which was just giving names to existing collections) andALL_NUMPY_DTYPES
- the rest was just reordering.