Skip to content

Commit ea60bfd

Browse files
h-vetinariaeltanawy
authored andcommitted
TST/CLN: series.duplicated; parametrisation; fix warning (pandas-dev#21899)
1 parent 04b6bce commit ea60bfd

File tree

3 files changed

+181
-177
lines changed

3 files changed

+181
-177
lines changed

pandas/conftest.py

+39-11
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,19 @@ def tz_aware_fixture(request):
248248
return request.param
249249

250250

251-
@pytest.fixture(params=[str, 'str', 'U'])
251+
UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
252+
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"]
253+
ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES
254+
255+
FLOAT_DTYPES = [float, "float32", "float64"]
256+
COMPLEX_DTYPES = [complex, "complex64", "complex128"]
257+
STRING_DTYPES = [str, 'str', 'U']
258+
259+
ALL_REAL_DTYPES = FLOAT_DTYPES + ALL_INT_DTYPES
260+
ALL_NUMPY_DTYPES = ALL_REAL_DTYPES + COMPLEX_DTYPES + STRING_DTYPES
261+
262+
263+
@pytest.fixture(params=STRING_DTYPES)
252264
def string_dtype(request):
253265
"""Parametrized fixture for string dtypes.
254266
@@ -259,9 +271,6 @@ def string_dtype(request):
259271
return request.param
260272

261273

262-
FLOAT_DTYPES = [float, "float32", "float64"]
263-
264-
265274
@pytest.fixture(params=FLOAT_DTYPES)
266275
def float_dtype(request):
267276
"""
@@ -274,7 +283,7 @@ def float_dtype(request):
274283
return request.param
275284

276285

277-
@pytest.fixture(params=[complex, "complex64", "complex128"])
286+
@pytest.fixture(params=COMPLEX_DTYPES)
278287
def complex_dtype(request):
279288
"""
280289
Parameterized fixture for complex dtypes.
@@ -286,12 +295,6 @@ def complex_dtype(request):
286295
return request.param
287296

288297

289-
UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
290-
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"]
291-
ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES
292-
ALL_REAL_DTYPES = FLOAT_DTYPES + ALL_INT_DTYPES
293-
294-
295298
@pytest.fixture(params=SIGNED_INT_DTYPES)
296299
def sint_dtype(request):
297300
"""
@@ -358,6 +361,31 @@ def any_real_dtype(request):
358361
return request.param
359362

360363

364+
@pytest.fixture(params=ALL_NUMPY_DTYPES)
365+
def any_numpy_dtype(request):
366+
"""
367+
Parameterized fixture for all numpy dtypes.
368+
369+
* int8
370+
* uint8
371+
* int16
372+
* uint16
373+
* int32
374+
* uint32
375+
* int64
376+
* uint64
377+
* float32
378+
* float64
379+
* complex64
380+
* complex128
381+
* str
382+
* 'str'
383+
* 'U'
384+
"""
385+
386+
return request.param
387+
388+
361389
@pytest.fixture
362390
def mock():
363391
"""

pandas/tests/series/test_analytics.py

+2-166
Original file line numberDiff line numberDiff line change
@@ -907,144 +907,6 @@ def test_matmul(self):
907907
pytest.raises(Exception, a.dot, a.values[:3])
908908
pytest.raises(ValueError, a.dot, b.T)
909909

910-
def test_value_counts_nunique(self):
911-
912-
# basics.rst doc example
913-
series = Series(np.random.randn(500))
914-
series[20:500] = np.nan
915-
series[10:20] = 5000
916-
result = series.nunique()
917-
assert result == 11
918-
919-
# GH 18051
920-
s = pd.Series(pd.Categorical([]))
921-
assert s.nunique() == 0
922-
s = pd.Series(pd.Categorical([np.nan]))
923-
assert s.nunique() == 0
924-
925-
def test_unique(self):
926-
927-
# 714 also, dtype=float
928-
s = Series([1.2345] * 100)
929-
s[::2] = np.nan
930-
result = s.unique()
931-
assert len(result) == 2
932-
933-
s = Series([1.2345] * 100, dtype='f4')
934-
s[::2] = np.nan
935-
result = s.unique()
936-
assert len(result) == 2
937-
938-
# NAs in object arrays #714
939-
s = Series(['foo'] * 100, dtype='O')
940-
s[::2] = np.nan
941-
result = s.unique()
942-
assert len(result) == 2
943-
944-
# decision about None
945-
s = Series([1, 2, 3, None, None, None], dtype=object)
946-
result = s.unique()
947-
expected = np.array([1, 2, 3, None], dtype=object)
948-
tm.assert_numpy_array_equal(result, expected)
949-
950-
# GH 18051
951-
s = pd.Series(pd.Categorical([]))
952-
tm.assert_categorical_equal(s.unique(), pd.Categorical([]),
953-
check_dtype=False)
954-
s = pd.Series(pd.Categorical([np.nan]))
955-
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]),
956-
check_dtype=False)
957-
958-
@pytest.mark.parametrize(
959-
"tc1, tc2",
960-
[
961-
(
962-
Series([1, 2, 3, 3], dtype=np.dtype('int_')),
963-
Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype('int_'))
964-
),
965-
(
966-
Series([1, 2, 3, 3], dtype=np.dtype('uint')),
967-
Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype('uint'))
968-
),
969-
(
970-
Series([1, 2, 3, 3], dtype=np.dtype('float_')),
971-
Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype('float_'))
972-
),
973-
(
974-
Series([1, 2, 3, 3], dtype=np.dtype('unicode_')),
975-
Series([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype('unicode_'))
976-
)
977-
]
978-
)
979-
def test_drop_duplicates_non_bool(self, tc1, tc2):
980-
# Test case 1
981-
expected = Series([False, False, False, True])
982-
assert_series_equal(tc1.duplicated(), expected)
983-
assert_series_equal(tc1.drop_duplicates(), tc1[~expected])
984-
sc = tc1.copy()
985-
sc.drop_duplicates(inplace=True)
986-
assert_series_equal(sc, tc1[~expected])
987-
988-
expected = Series([False, False, True, False])
989-
assert_series_equal(tc1.duplicated(keep='last'), expected)
990-
assert_series_equal(tc1.drop_duplicates(keep='last'), tc1[~expected])
991-
sc = tc1.copy()
992-
sc.drop_duplicates(keep='last', inplace=True)
993-
assert_series_equal(sc, tc1[~expected])
994-
995-
expected = Series([False, False, True, True])
996-
assert_series_equal(tc1.duplicated(keep=False), expected)
997-
assert_series_equal(tc1.drop_duplicates(keep=False), tc1[~expected])
998-
sc = tc1.copy()
999-
sc.drop_duplicates(keep=False, inplace=True)
1000-
assert_series_equal(sc, tc1[~expected])
1001-
1002-
# Test case 2
1003-
expected = Series([False, False, False, False, True, True, False])
1004-
assert_series_equal(tc2.duplicated(), expected)
1005-
assert_series_equal(tc2.drop_duplicates(), tc2[~expected])
1006-
sc = tc2.copy()
1007-
sc.drop_duplicates(inplace=True)
1008-
assert_series_equal(sc, tc2[~expected])
1009-
1010-
expected = Series([False, True, True, False, False, False, False])
1011-
assert_series_equal(tc2.duplicated(keep='last'), expected)
1012-
assert_series_equal(tc2.drop_duplicates(keep='last'), tc2[~expected])
1013-
sc = tc2.copy()
1014-
sc.drop_duplicates(keep='last', inplace=True)
1015-
assert_series_equal(sc, tc2[~expected])
1016-
1017-
expected = Series([False, True, True, False, True, True, False])
1018-
assert_series_equal(tc2.duplicated(keep=False), expected)
1019-
assert_series_equal(tc2.drop_duplicates(keep=False), tc2[~expected])
1020-
sc = tc2.copy()
1021-
sc.drop_duplicates(keep=False, inplace=True)
1022-
assert_series_equal(sc, tc2[~expected])
1023-
1024-
def test_drop_duplicates_bool(self):
1025-
tc = Series([True, False, True, False])
1026-
1027-
expected = Series([False, False, True, True])
1028-
assert_series_equal(tc.duplicated(), expected)
1029-
assert_series_equal(tc.drop_duplicates(), tc[~expected])
1030-
sc = tc.copy()
1031-
sc.drop_duplicates(inplace=True)
1032-
assert_series_equal(sc, tc[~expected])
1033-
1034-
expected = Series([True, True, False, False])
1035-
assert_series_equal(tc.duplicated(keep='last'), expected)
1036-
assert_series_equal(tc.drop_duplicates(keep='last'), tc[~expected])
1037-
sc = tc.copy()
1038-
sc.drop_duplicates(keep='last', inplace=True)
1039-
assert_series_equal(sc, tc[~expected])
1040-
1041-
expected = Series([True, True, True, True])
1042-
assert_series_equal(tc.duplicated(keep=False), expected)
1043-
assert_series_equal(tc.drop_duplicates(keep=False), tc[~expected])
1044-
sc = tc.copy()
1045-
sc.drop_duplicates(keep=False, inplace=True)
1046-
assert_series_equal(sc, tc[~expected])
1047-
1048910
def test_clip(self):
1049911
val = self.ts.median()
1050912

@@ -1416,7 +1278,8 @@ def test_ptp(self):
14161278
N = 1000
14171279
arr = np.random.randn(N)
14181280
ser = Series(arr)
1419-
assert np.ptp(ser) == np.ptp(arr)
1281+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1282+
assert np.ptp(ser) == np.ptp(arr)
14201283

14211284
# GH11163
14221285
s = Series([3, 5, np.nan, -3, 10])
@@ -1457,10 +1320,6 @@ def test_empty_timeseries_redections_return_nat(self):
14571320
assert Series([], dtype=dtype).min() is pd.NaT
14581321
assert Series([], dtype=dtype).max() is pd.NaT
14591322

1460-
def test_unique_data_ownership(self):
1461-
# it works! #1807
1462-
Series(Series(["a", "c", "b"]).unique()).sort_values()
1463-
14641323
def test_repeat(self):
14651324
s = Series(np.random.randn(3), index=['a', 'b', 'c'])
14661325

@@ -1537,29 +1396,6 @@ def test_searchsorted_sorter(self):
15371396
e = np.array([0, 2], dtype=np.intp)
15381397
tm.assert_numpy_array_equal(r, e)
15391398

1540-
def test_is_unique(self):
1541-
# GH11946
1542-
s = Series(np.random.randint(0, 10, size=1000))
1543-
assert not s.is_unique
1544-
s = Series(np.arange(1000))
1545-
assert s.is_unique
1546-
1547-
def test_is_unique_class_ne(self, capsys):
1548-
# GH 20661
1549-
class Foo(object):
1550-
def __init__(self, val):
1551-
self._value = val
1552-
1553-
def __ne__(self, other):
1554-
raise Exception("NEQ not supported")
1555-
1556-
li = [Foo(i) for i in range(5)]
1557-
s = pd.Series(li, index=[i for i in range(5)])
1558-
_, err = capsys.readouterr()
1559-
s.is_unique
1560-
_, err = capsys.readouterr()
1561-
assert len(err) == 0
1562-
15631399
def test_is_monotonic(self):
15641400

15651401
s = Series(np.random.randint(0, 10, size=1000))

0 commit comments

Comments
 (0)