Skip to content

Commit bc46620

Browse files
authored
TST: collect Index tests by method (#40070)
1 parent 64859c1 commit bc46620

File tree

19 files changed

+699
-714
lines changed

19 files changed

+699
-714
lines changed

pandas/core/groupby/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):
774774
counts[label] = group.shape[0]
775775
result[label] = res
776776

777-
result = lib.maybe_convert_objects(result, try_float=0)
777+
result = lib.maybe_convert_objects(result, try_float=False)
778778
result = maybe_cast_result(result, obj, numeric_only=True)
779779

780780
return result, counts

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ def _format_with_header(
11521152
values = self._values
11531153

11541154
if is_object_dtype(values.dtype):
1155-
values = lib.maybe_convert_objects(values, safe=1)
1155+
values = lib.maybe_convert_objects(values, safe=True)
11561156

11571157
result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]
11581158

pandas/tests/dtypes/test_inference.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,23 @@ def test_maybe_convert_objects_datetime(self):
564564
[np.datetime64("2000-01-01"), np.timedelta64(1, "s")], dtype=object
565565
)
566566
exp = arr.copy()
567-
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
567+
out = lib.maybe_convert_objects(
568+
arr, convert_datetime=True, convert_timedelta=True
569+
)
568570
tm.assert_numpy_array_equal(out, exp)
569571

570572
arr = np.array([pd.NaT, np.timedelta64(1, "s")], dtype=object)
571573
exp = np.array([np.timedelta64("NaT"), np.timedelta64(1, "s")], dtype="m8[ns]")
572-
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
574+
out = lib.maybe_convert_objects(
575+
arr, convert_datetime=True, convert_timedelta=True
576+
)
573577
tm.assert_numpy_array_equal(out, exp)
574578

575579
arr = np.array([np.timedelta64(1, "s"), np.nan], dtype=object)
576580
exp = arr.copy()
577-
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
581+
out = lib.maybe_convert_objects(
582+
arr, convert_datetime=True, convert_timedelta=True
583+
)
578584
tm.assert_numpy_array_equal(out, exp)
579585

580586
@pytest.mark.parametrize(
@@ -587,7 +593,7 @@ def test_maybe_convert_objects_datetime(self):
587593
def test_maybe_convert_objects_nullable_integer(self, exp):
588594
# GH27335
589595
arr = np.array([2, np.NaN], dtype=object)
590-
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=1)
596+
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=True)
591597

592598
tm.assert_extension_array_equal(result, exp)
593599

@@ -601,7 +607,7 @@ def test_maybe_convert_objects_bool_nan(self):
601607
def test_mixed_dtypes_remain_object_array(self):
602608
# GH14956
603609
array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object)
604-
result = lib.maybe_convert_objects(array, convert_datetime=1)
610+
result = lib.maybe_convert_objects(array, convert_datetime=True)
605611
tm.assert_numpy_array_equal(result, array)
606612

607613

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import (
5+
PeriodIndex,
6+
Series,
7+
date_range,
8+
period_range,
9+
timedelta_range,
10+
)
11+
import pandas._testing as tm
12+
13+
14+
class DropDuplicates:
15+
def test_drop_duplicates_metadata(self, idx):
16+
# GH#10115
17+
result = idx.drop_duplicates()
18+
tm.assert_index_equal(idx, result)
19+
assert idx.freq == result.freq
20+
21+
idx_dup = idx.append(idx)
22+
result = idx_dup.drop_duplicates()
23+
24+
expected = idx
25+
if not isinstance(idx, PeriodIndex):
26+
# freq is reset except for PeriodIndex
27+
assert idx_dup.freq is None
28+
assert result.freq is None
29+
expected = idx._with_freq(None)
30+
else:
31+
assert result.freq == expected.freq
32+
33+
tm.assert_index_equal(result, expected)
34+
35+
@pytest.mark.parametrize(
36+
"keep, expected, index",
37+
[
38+
("first", np.concatenate(([False] * 10, [True] * 5)), np.arange(0, 10)),
39+
("last", np.concatenate(([True] * 5, [False] * 10)), np.arange(5, 15)),
40+
(
41+
False,
42+
np.concatenate(([True] * 5, [False] * 5, [True] * 5)),
43+
np.arange(5, 10),
44+
),
45+
],
46+
)
47+
def test_drop_duplicates(self, keep, expected, index, idx):
48+
# to check Index/Series compat
49+
idx = idx.append(idx[:5])
50+
51+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
52+
expected = idx[~expected]
53+
54+
result = idx.drop_duplicates(keep=keep)
55+
tm.assert_index_equal(result, expected)
56+
57+
result = Series(idx).drop_duplicates(keep=keep)
58+
tm.assert_series_equal(result, Series(expected, index=index))
59+
60+
61+
class TestDropDuplicatesPeriodIndex(DropDuplicates):
62+
@pytest.fixture(params=["D", "3D", "H", "2H", "T", "2T", "S", "3S"])
63+
def freq(self, request):
64+
return request.param
65+
66+
@pytest.fixture
67+
def idx(self, freq):
68+
return period_range("2011-01-01", periods=10, freq=freq, name="idx")
69+
70+
71+
class TestDropDuplicatesDatetimeIndex(DropDuplicates):
72+
@pytest.fixture
73+
def idx(self, freq_sample):
74+
return date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
75+
76+
77+
class TestDropDuplicatesTimedeltaIndex(DropDuplicates):
78+
@pytest.fixture
79+
def idx(self, freq_sample):
80+
return timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import (
5+
DatetimeIndex,
6+
NaT,
7+
PeriodIndex,
8+
TimedeltaIndex,
9+
)
10+
import pandas._testing as tm
11+
12+
13+
class NATests:
14+
def test_nat(self, index_without_na):
15+
empty_index = index_without_na[:0]
16+
17+
index_with_na = index_without_na.copy(deep=True)
18+
index_with_na._data[1] = NaT
19+
20+
assert type(index_without_na)._na_value is NaT
21+
assert empty_index._na_value is NaT
22+
assert index_with_na._na_value is NaT
23+
assert index_without_na._na_value is NaT
24+
25+
idx = index_without_na
26+
assert idx._can_hold_na
27+
28+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
29+
assert idx.hasnans is False
30+
31+
idx = index_with_na
32+
assert idx._can_hold_na
33+
34+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
35+
assert idx.hasnans is True
36+
37+
38+
class TestDatetimeIndexNA(NATests):
39+
@pytest.fixture
40+
def index_without_na(self, tz_naive_fixture):
41+
tz = tz_naive_fixture
42+
return DatetimeIndex(["2011-01-01", "2011-01-02"], tz=tz)
43+
44+
45+
class TestTimedeltaIndexNA(NATests):
46+
@pytest.fixture
47+
def index_without_na(self):
48+
return TimedeltaIndex(["1 days", "2 days"])
49+
50+
51+
class TestPeriodIndexNA(NATests):
52+
@pytest.fixture
53+
def index_without_na(self):
54+
return PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")

0 commit comments

Comments
 (0)