Skip to content

Commit 78ce1b4

Browse files
authored
TST: collect tests by method, split large tests (#40133)
1 parent 15b99d8 commit 78ce1b4

14 files changed

+164
-129
lines changed

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,14 @@ def __init__(
589589

590590
elif isinstance(data, (np.ndarray, Series, Index)):
591591
if data.dtype.names:
592+
# i.e. numpy structured array
592593
data_columns = list(data.dtype.names)
593594
data = {k: data[k] for k in data_columns}
594595
if columns is None:
595596
columns = data_columns
596597
mgr = dict_to_mgr(data, index, columns, dtype=dtype)
597598
elif getattr(data, "name", None) is not None:
599+
# i.e. Series/Index with non-None name
598600
mgr = dict_to_mgr({data.name: data}, index, columns, dtype=dtype)
599601
else:
600602
mgr = ndarray_to_mgr(data, index, columns, dtype=dtype, copy=copy)

pandas/tests/frame/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -1722,12 +1722,15 @@ def test_constructor_with_datetimes(self):
17221722
)
17231723
tm.assert_series_equal(result, expected)
17241724

1725+
def test_constructor_with_datetimes1(self):
1726+
17251727
# GH 2809
17261728
ind = date_range(start="2000-01-01", freq="D", periods=10)
17271729
datetimes = [ts.to_pydatetime() for ts in ind]
17281730
datetime_s = Series(datetimes)
17291731
assert datetime_s.dtype == "M8[ns]"
17301732

1733+
def test_constructor_with_datetimes2(self):
17311734
# GH 2810
17321735
ind = date_range(start="2000-01-01", freq="D", periods=10)
17331736
datetimes = [ts.to_pydatetime() for ts in ind]
@@ -1741,6 +1744,7 @@ def test_constructor_with_datetimes(self):
17411744
)
17421745
tm.assert_series_equal(result, expected)
17431746

1747+
def test_constructor_with_datetimes3(self):
17441748
# GH 7594
17451749
# don't coerce tz-aware
17461750
tz = pytz.timezone("US/Eastern")
@@ -1758,6 +1762,7 @@ def test_constructor_with_datetimes(self):
17581762
df.dtypes, Series({"End Date": "datetime64[ns, US/Eastern]"})
17591763
)
17601764

1765+
def test_constructor_with_datetimes4(self):
17611766
# tz-aware (UTC and other tz's)
17621767
# GH 8411
17631768
dr = date_range("20130101", periods=3)
@@ -1770,6 +1775,7 @@ def test_constructor_with_datetimes(self):
17701775
df = DataFrame({"value": dr})
17711776
assert str(df.iat[0, 0].tz) == "US/Eastern"
17721777

1778+
def test_constructor_with_datetimes5(self):
17731779
# GH 7822
17741780
# preserver an index with a tz on dict construction
17751781
i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern")
@@ -1782,7 +1788,9 @@ def test_constructor_with_datetimes(self):
17821788
df = DataFrame({"a": i})
17831789
tm.assert_frame_equal(df, expected)
17841790

1791+
def test_constructor_with_datetimes6(self):
17851792
# multiples
1793+
i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern")
17861794
i_no_tz = date_range("1/1/2011", periods=5, freq="10s")
17871795
df = DataFrame({"a": i, "b": i_no_tz})
17881796
expected = DataFrame({"a": i.to_series().reset_index(drop=True), "b": i_no_tz})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import pytest
2+
3+
from pandas import (
4+
CategoricalIndex,
5+
Index,
6+
)
7+
import pandas._testing as tm
8+
9+
10+
class TestAppend:
11+
@pytest.fixture
12+
def ci(self):
13+
categories = list("cab")
14+
return CategoricalIndex(list("aabbca"), categories=categories, ordered=False)
15+
16+
def test_append(self, ci):
17+
# append cats with the same categories
18+
result = ci[:3].append(ci[3:])
19+
tm.assert_index_equal(result, ci, exact=True)
20+
21+
foos = [ci[:1], ci[1:3], ci[3:]]
22+
result = foos[0].append(foos[1:])
23+
tm.assert_index_equal(result, ci, exact=True)
24+
25+
def test_append_empty(self, ci):
26+
# empty
27+
result = ci.append([])
28+
tm.assert_index_equal(result, ci, exact=True)
29+
30+
def test_append_mismatched_categories(self, ci):
31+
# appending with different categories or reordered is not ok
32+
msg = "all inputs must be Index"
33+
with pytest.raises(TypeError, match=msg):
34+
ci.append(ci.values.set_categories(list("abcd")))
35+
with pytest.raises(TypeError, match=msg):
36+
ci.append(ci.values.reorder_categories(list("abc")))
37+
38+
def test_append_category_objects(self, ci):
39+
# with objects
40+
result = ci.append(Index(["c", "a"]))
41+
expected = CategoricalIndex(list("aabbcaca"), categories=ci.categories)
42+
tm.assert_index_equal(result, expected, exact=True)
43+
44+
def test_append_non_categories(self, ci):
45+
# invalid objects -> cast to object via concat_compat
46+
result = ci.append(Index(["a", "d"]))
47+
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
48+
tm.assert_index_equal(result, expected, exact=True)
49+
50+
def test_append_object(self, ci):
51+
# GH#14298 - if base object is not categorical -> coerce to object
52+
result = Index(["c", "a"]).append(ci)
53+
expected = Index(list("caaabbca"))
54+
tm.assert_index_equal(result, expected, exact=True)
55+
56+
def test_append_to_another(self):
57+
# hits Index._concat
58+
fst = Index(["a", "b"])
59+
snd = CategoricalIndex(["d", "e"])
60+
result = fst.append(snd)
61+
expected = Index(["a", "b", "d", "e"])
62+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/categorical/test_category.py

-53
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,6 @@ def test_can_hold_identifiers(self):
3030
key = idx[0]
3131
assert idx._can_hold_identifiers_and_holds_name(key) is True
3232

33-
def test_append(self):
34-
35-
ci = self.create_index()
36-
categories = ci.categories
37-
38-
# append cats with the same categories
39-
result = ci[:3].append(ci[3:])
40-
tm.assert_index_equal(result, ci, exact=True)
41-
42-
foos = [ci[:1], ci[1:3], ci[3:]]
43-
result = foos[0].append(foos[1:])
44-
tm.assert_index_equal(result, ci, exact=True)
45-
46-
# empty
47-
result = ci.append([])
48-
tm.assert_index_equal(result, ci, exact=True)
49-
50-
# appending with different categories or reordered is not ok
51-
msg = "all inputs must be Index"
52-
with pytest.raises(TypeError, match=msg):
53-
ci.append(ci.values.set_categories(list("abcd")))
54-
with pytest.raises(TypeError, match=msg):
55-
ci.append(ci.values.reorder_categories(list("abc")))
56-
57-
# with objects
58-
result = ci.append(Index(["c", "a"]))
59-
expected = CategoricalIndex(list("aabbcaca"), categories=categories)
60-
tm.assert_index_equal(result, expected, exact=True)
61-
62-
# invalid objects -> cast to object via concat_compat
63-
result = ci.append(Index(["a", "d"]))
64-
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
65-
tm.assert_index_equal(result, expected, exact=True)
66-
67-
# GH14298 - if base object is not categorical -> coerce to object
68-
result = Index(["c", "a"]).append(ci)
69-
expected = Index(list("caaabbca"))
70-
tm.assert_index_equal(result, expected, exact=True)
71-
72-
def test_append_to_another(self):
73-
# hits Index._concat
74-
fst = Index(["a", "b"])
75-
snd = CategoricalIndex(["d", "e"])
76-
result = fst.append(snd)
77-
expected = Index(["a", "b", "d", "e"])
78-
tm.assert_index_equal(result, expected)
79-
8033
def test_insert(self):
8134

8235
ci = self.create_index()
@@ -326,12 +279,6 @@ def test_map_str(self):
326279
class TestCategoricalIndex2:
327280
# Tests that are not overriding a test in Base
328281

329-
def test_format_different_scalar_lengths(self):
330-
# GH35439
331-
idx = CategoricalIndex(["aaaaaaaaa", "b"])
332-
expected = ["aaaaaaaaa", "b"]
333-
assert idx.format() == expected
334-
335282
@pytest.mark.parametrize(
336283
"dtype, engine_type",
337284
[

pandas/tests/indexes/categorical/test_formats.py

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88

99
class TestCategoricalIndexRepr:
10+
def test_format_different_scalar_lengths(self):
11+
# GH#35439
12+
idx = CategoricalIndex(["aaaaaaaaa", "b"])
13+
expected = ["aaaaaaaaa", "b"]
14+
assert idx.format() == expected
15+
1016
def test_string_categorical_index_repr(self):
1117
# short
1218
idx = CategoricalIndex(["a", "bb", "ccc"])

pandas/tests/indexes/datetimes/test_indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,13 @@ def test_get_loc_reasonable_key_error(self):
551551
with pytest.raises(KeyError, match="2000"):
552552
index.get_loc("1/1/2000")
553553

554+
def test_get_loc_year_str(self):
555+
rng = date_range("1/1/2000", "1/1/2010")
556+
557+
result = rng.get_loc("2009")
558+
expected = slice(3288, 3653)
559+
assert result == expected
560+
554561

555562
class TestContains:
556563
def test_dti_contains_with_duplicates(self):

pandas/tests/indexes/datetimes/test_misc.py

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def test_range_edges(self):
3737
)
3838
tm.assert_index_equal(idx, exp)
3939

40+
def test_range_edges2(self):
41+
4042
idx = date_range(
4143
start=Timestamp("1970-01-01 00:00:00.000000004"),
4244
end=Timestamp("1970-01-01 00:00:00.000000001"),
@@ -45,6 +47,8 @@ def test_range_edges(self):
4547
exp = DatetimeIndex([], freq="N")
4648
tm.assert_index_equal(idx, exp)
4749

50+
def test_range_edges3(self):
51+
4852
idx = date_range(
4953
start=Timestamp("1970-01-01 00:00:00.000000001"),
5054
end=Timestamp("1970-01-01 00:00:00.000000001"),
@@ -53,6 +57,8 @@ def test_range_edges(self):
5357
exp = DatetimeIndex(["1970-01-01 00:00:00.000000001"], freq="N")
5458
tm.assert_index_equal(idx, exp)
5559

60+
def test_range_edges4(self):
61+
5662
idx = date_range(
5763
start=Timestamp("1970-01-01 00:00:00.000001"),
5864
end=Timestamp("1970-01-01 00:00:00.000004"),
@@ -69,6 +75,8 @@ def test_range_edges(self):
6975
)
7076
tm.assert_index_equal(idx, exp)
7177

78+
def test_range_edges5(self):
79+
7280
idx = date_range(
7381
start=Timestamp("1970-01-01 00:00:00.001"),
7482
end=Timestamp("1970-01-01 00:00:00.004"),
@@ -85,6 +93,7 @@ def test_range_edges(self):
8593
)
8694
tm.assert_index_equal(idx, exp)
8795

96+
def test_range_edges6(self):
8897
idx = date_range(
8998
start=Timestamp("1970-01-01 00:00:01"),
9099
end=Timestamp("1970-01-01 00:00:04"),
@@ -101,6 +110,7 @@ def test_range_edges(self):
101110
)
102111
tm.assert_index_equal(idx, exp)
103112

113+
def test_range_edges7(self):
104114
idx = date_range(
105115
start=Timestamp("1970-01-01 00:01"),
106116
end=Timestamp("1970-01-01 00:04"),
@@ -117,6 +127,7 @@ def test_range_edges(self):
117127
)
118128
tm.assert_index_equal(idx, exp)
119129

130+
def test_range_edges8(self):
120131
idx = date_range(
121132
start=Timestamp("1970-01-01 01:00"),
122133
end=Timestamp("1970-01-01 04:00"),
@@ -133,6 +144,7 @@ def test_range_edges(self):
133144
)
134145
tm.assert_index_equal(idx, exp)
135146

147+
def test_range_edges9(self):
136148
idx = date_range(
137149
start=Timestamp("1970-01-01"), end=Timestamp("1970-01-04"), freq="D"
138150
)
@@ -234,24 +246,28 @@ def test_datetimeindex_accessors(self):
234246
exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name")
235247
tm.assert_index_equal(res, exp)
236248

249+
def test_datetimeindex_accessors2(self):
237250
dti = date_range(freq="BQ-FEB", start=datetime(1998, 1, 1), periods=4)
238251

239252
assert sum(dti.is_quarter_start) == 0
240253
assert sum(dti.is_quarter_end) == 4
241254
assert sum(dti.is_year_start) == 0
242255
assert sum(dti.is_year_end) == 1
243256

257+
def test_datetimeindex_accessors3(self):
244258
# Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
245259
bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu")
246260
dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
247261
msg = "Custom business days is not supported by is_month_start"
248262
with pytest.raises(ValueError, match=msg):
249263
dti.is_month_start
250264

265+
def test_datetimeindex_accessors4(self):
251266
dti = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
252267

253268
assert dti.is_month_start[0] == 1
254269

270+
def test_datetimeindex_accessors5(self):
255271
tests = [
256272
(Timestamp("2013-06-01", freq="M").is_month_start, 1),
257273
(Timestamp("2013-06-01", freq="BM").is_month_start, 0),
@@ -290,6 +306,7 @@ def test_datetimeindex_accessors(self):
290306
for ts, value in tests:
291307
assert ts == value
292308

309+
def test_datetimeindex_accessors6(self):
293310
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
294311
# return the same weekofyear accessor close to new year w/ tz
295312
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]

pandas/tests/indexes/datetimes/test_partial_slicing.py

-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ def test_slice_year(self):
5555
expected = df[df.index.year == 2005]
5656
tm.assert_frame_equal(result, expected)
5757

58-
rng = date_range("1/1/2000", "1/1/2010")
59-
60-
result = rng.get_loc("2009")
61-
expected = slice(3288, 3653)
62-
assert result == expected
63-
6458
@pytest.mark.parametrize(
6559
"partial_dtime",
6660
[

pandas/tests/indexes/period/test_period.py

-15
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,6 @@ def test_is_(self):
253253
assert not index.is_(index - 2)
254254
assert not index.is_(index - 0)
255255

256-
def test_periods_number_check(self):
257-
msg = (
258-
"Of the three parameters: start, end, and periods, exactly two "
259-
"must be specified"
260-
)
261-
with pytest.raises(ValueError, match=msg):
262-
period_range("2011-1-1", "2012-1-1", "B")
263-
264256
def test_index_duplicate_periods(self):
265257
# monotonic
266258
idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq="A-JUN")
@@ -348,13 +340,6 @@ def test_with_multi_index(self):
348340

349341
assert isinstance(s.index.values[0][0], Period)
350342

351-
def test_convert_array_of_periods(self):
352-
rng = period_range("1/1/2000", periods=20, freq="D")
353-
periods = list(rng)
354-
355-
result = Index(periods)
356-
assert isinstance(result, PeriodIndex)
357-
358343
def test_pickle_freq(self):
359344
# GH2891
360345
prng = period_range("1/1/2011", "1/1/2012", freq="M")

pandas/tests/indexes/period/test_period_range.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313

1414
class TestPeriodRange:
15+
def test_required_arguments(self):
16+
msg = (
17+
"Of the three parameters: start, end, and periods, exactly two "
18+
"must be specified"
19+
)
20+
with pytest.raises(ValueError, match=msg):
21+
period_range("2011-1-1", "2012-1-1", "B")
22+
1523
@pytest.mark.parametrize("freq", ["D", "W", "M", "Q", "A"])
1624
def test_construction_from_string(self, freq):
1725
# non-empty

pandas/tests/indexes/test_index_new.py

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ def test_constructor_infer_periodindex(self):
8080
tm.assert_index_equal(rs, xp)
8181
assert isinstance(rs, PeriodIndex)
8282

83+
def test_from_list_of_periods(self):
84+
rng = period_range("1/1/2000", periods=20, freq="D")
85+
periods = list(rng)
86+
87+
result = Index(periods)
88+
assert isinstance(result, PeriodIndex)
89+
8390
@pytest.mark.parametrize("pos", [0, 1])
8491
@pytest.mark.parametrize(
8592
"klass,dtype,ctor",

0 commit comments

Comments
 (0)