8000 TST: Add tests for duplicated and drop_duplicates by mproszewska · Pull Request #32575 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

TST: Add tests for duplicated and drop_duplicates #32575

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 14 commits into from
Apr 6, 2020
Merged
Prev Previous commit
Next Next commit
TST: Apply requested changes
  • Loading branch information
mproszewska committed Mar 14, 2020
commit 5931a48dd243d24b6c2b6cf7ab9f460ff5dcfdb5
104 changes: 48 additions & 56 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,66 +365,58 @@ def test_has_duplicates(self):
assert idx.is_unique is True
assert idx.has_duplicates is False

def _test_drop_duplicates(self, idx, keep, expected, index):
for k, e, i in zip(keep, expected, index):
tm.assert_numpy_array_equal(idx.duplicated(keep=k), e)
e = idx[~e]

result = idx.drop_duplicates(keep=k)
tm.assert_index_equal(result, e)

result = Series(idx).drop_duplicates(keep=k)
tm.assert_series_equal(result, Series(e, i))

def test_drop_duplicates(self):
keep = ["first", "last", False]

categories = [[1, 2, 3], list("abc")]
expected = [
np.array([False, True, True]),
np.array([True, True, False]),
np.array([True, True, True]),
]
index = [[0], [2], np.empty(shape=(0), dtype=int)]
for c in categories:
idx = pd.CategoricalIndex([1, 1, 1], categories=c, name="foo")
self._test_drop_duplicates(idx, keep, expected, index)

categories = ["a", "b", "c"]
idx = CategoricalIndex([2, "a", "b"], categories=categories, name="foo")
expected = np.zeros(shape=(3, 3), dtype=np.bool)
index = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
self._test_drop_duplicates(idx, keep, expected, index)

idx = CategoricalIndex(list("abb"), categories=categories, name="foo")
expected = [
np.array([False, False, True]),
np.array([False, True, False]),
np.array([False, True, True]),
]
index = [[0, 1], [0, 2], [0]]
self._test_drop_duplicates(idx, keep, expected, index)

def test_unique(self):

categories = [1, 2, 3]
idx = CategoricalIndex([1, 1, 1], categories=categories)
expected = CategoricalIndex([1], categories=[1])
tm.assert_index_equal(idx.unique(), expected)
@pytest.mark.parametrize(
"data, categories, expected",
[
([1, 1, 1], [1, 2, 3],
{
"first" : np.array([False, True, True]),
"last" : np.array([True, True, False]),
False : np.array([True, True, True]),
}),
([1, 1, 1], list("abc"),
{
"first" : np.array([False, True, True]),
"last" : np.array([True, True, False]),
False : np.array([True, True, True]),
}),
([2, "a", "b"], list('abc'),
{
"first" : np.zeros(shape=(3), dtype=np.bool),
"last" : np.zeros(shape=(3), dtype=np.bool),
False : np.zeros(shape=(3), dtype=np.bool),
}),
(list("abb"), list('abc'),
{
"first" : np.array([False, False, True]),
"last" : np.array([False, True, False]),
False : np.array([False, True, True]),
}),
],
)
def test_drop_duplicates(self, data, categories, expected):

categories = list("abc")
idx = CategoricalIndex([1, 1, 1], categories=categories)
expected = CategoricalIndex([np.nan], categories=[])
tm.assert_index_equal(idx.unique(), expected)
idx = CategoricalIndex(data, categories=categories, name="foo")
for keep, e in expected.items():
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), e)
e = idx[~e]
result = idx.drop_duplicates(keep=keep)
tm.assert_index_equal(result, e)

categories = [1, 2, 3]
idx = CategoricalIndex([1, 2, "a"], categories=categories)
expected = CategoricalIndex([1, 2, np.nan], categories=[1, 2])
tm.assert_index_equal(idx.unique(), expected)
@pytest.mark.parametrize(
"data, categories, expected_data, expected_categories",
[
([1, 1, 1], [1, 2, 3], [1], [1]),
([1, 1, 1], list('abc'), [np.nan], []),
([1, 2, "a"], [1, 2, 3], [1, 2, np.nan], [1, 2]),
([2, "a", "b"], list("abc"), [np.nan, "a", "b"], ["a", "b"]),
],
)
def test_unique(self, data, categories, expected_data, expected_categories):

categories = list("abc")
idx = CategoricalIndex([2, "a", "b"], categories=categories)
expected = CategoricalIndex([np.nan, "a", "b"], categories=["a", "b"])
idx = CategoricalIndex(data, categories=categories)
expected = CategoricalIndex(expected_data, categories=expected_categories)
tm.assert_index_equal(idx.unique(), expected)

def test_repr_roundtrip(self):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ def sort(request):
in in the Index setops methods.
"""
return request.param

@pytest.fixture(
params=["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
def freq_sample(request):
"""
Valid values for 'freq' parameter used to create date_range and
timedelta_range..
"""
return request.param
44 changes: 7 additions & 37 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,9 @@ def test_order_without_freq(self, index_dates, expected_dates, tz_naive_fixture)
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
assert ordered.freq is None

@pytest.mark.parametrize(
"freq", ["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
def test_drop_duplicates_metadata(self, freq):
def test_drop_duplicates_metadata(self, freq_sample):
# GH 10115
idx = pd.date_range("2011-01-01", freq=freq, periods=10, name="idx")
idx = pd.date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
result = idx.drop_duplicates()
tm.assert_index_equal(idx, result)
assert idx.freq == result.freq
Expand All @@ -275,9 +272,6 @@ def test_drop_duplicates_metadata(self, freq):
tm.assert_index_equal(idx, result)
assert result.freq is None

@pytest.mark.parametrize(
"freq", ["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
@pytest.mark.parametrize(
"keep, expected, index",
[
Expand All @@ -290,9 +284,9 @@ def test_drop_duplicates_metadata(self, freq):
),
],
)
def test_drop_duplicates(self, freq, keep, expected, index):
def test_drop_duplicates(self, freq_sample, keep, expected, index):
# to check Index/Series compat
idx = pd.date_range("2011-01-01", freq=freq, periods=10, name="idx")
idx = pd.date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
idx = idx.append(idx[:5])

tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
Expand All @@ -304,36 +298,12 @@ def test_drop_duplicates(self, freq, keep, expected, index):
result = Series(idx).drop_duplicates(keep=keep)
tm.assert_series_equal(result, Series(expected, index=index))

@pytest.mark.parametrize(
"freq",
[
"A",
"2A",
"-2A",
"Q",
"-1Q",
"M",
"-1M",
"D",
"3D",
"-3D",
"W",
"-1W",
"H",
"2H",
"-2H",
"T",
"2T",
"S",
"-3S",
],
)
def test_infer_freq(self, freq):
def test_infer_freq(self, freq_sample):
# GH 11018
idx = pd.date_range("2011-01-01 09:00:00", freq=freq, periods=10)
idx = pd.date_range("2011-01-01 09:00:00", freq=freq_sample, periods=10)
result = pd.DatetimeIndex(idx.asi8, freq="infer")
tm.assert_index_equal(idx, result)
assert result.freq == freq
assert result.freq == freq_sample

def test_nat(self, tz_naive_fixture):
tz = tz_naive_fixture
Expand Down
23 changes: 7 additions & 16 deletions pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,9 @@ def test_order(self):
tm.assert_numpy_array_equal(indexer, exp, check_dtype=False)
assert ordered.freq is None

@pytest.mark.parametrize(
"freq", ["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
def test_drop_duplicates_metadata(self, freq):
def test_drop_duplicates_metadata(self, freq_sample):
# GH 10115
idx = pd.timedelta_range("1 day", periods=10, freq=freq, name="idx")
idx = pd.timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
result = idx.drop_duplicates()
tm.assert_index_equal(idx, result)
assert idx.freq == result.freq
Expand All @@ -152,9 +149,6 @@ def test_drop_duplicates_metadata(self, freq):
tm.assert_index_equal(idx, result)
assert result.freq is None

@pytest.mark.parametrize(
"freq", ["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
@pytest.mark.parametrize(
"keep, expected, index",
[
Expand All @@ -167,9 +161,9 @@ def test_drop_duplicates_metadata(self, freq):
),
],
)
def test_drop_duplicates(self, freq, keep, expected, index):
def test_drop_duplicates(self, freq_sample, keep, expected, index):
# to check Index/Series compat
idx = pd.timedelta_range("1 day", periods=10, freq=freq, name="idx")
idx = pd.timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
idx = idx.append(idx[:5])

tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
Expand All @@ -181,15 +175,12 @@ def test_drop_duplicates(self, freq, keep, expected, index):
result = Series(idx).drop_duplicates(keep=keep)
tm.assert_series_equal(result, Series(expected, index=index))

@pytest.mark.parametrize(
"freq", ["D", "3D", "-3D", "H", "2H", "-2H", "T", "2T", "S", "-3S"]
)
def test_infer_freq(self, freq):
def test_infer_freq(self, freq_sample):
# GH#11018
idx = pd.timedelta_range("1", freq=freq, periods=10)
idx = pd.timedelta_range("1", freq=freq_sample, periods=10)
result = pd.TimedeltaIndex(idx.asi8, freq="infer")
tm.assert_index_equal(idx, result)
assert result.freq == freq
assert result.freq == freq_sample

def test_repeat(self):
index = pd.timedelta_range("1 days", periods=2, freq="D")
Expand Down
0