Skip to content

Commit 2baddc8

Browse files
jbrockmendelukarroum
authored andcommitted
TST/REF: collect tests by method (#37573)
* TST/REF: collect tests by method * use round_trip_pickle
1 parent 7113926 commit 2baddc8

File tree

7 files changed

+241
-227
lines changed

7 files changed

+241
-227
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame
5+
import pandas._testing as tm
6+
7+
8+
class TestSwapAxes:
9+
def test_swapaxes(self):
10+
df = DataFrame(np.random.randn(10, 5))
11+
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
12+
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
13+
14+
def test_swapaxes_noop(self):
15+
df = DataFrame(np.random.randn(10, 5))
16+
tm.assert_frame_equal(df, df.swapaxes(0, 0))
17+
18+
def test_swapaxes_invalid_axis(self):
19+
df = DataFrame(np.random.randn(10, 5))
20+
msg = "No axis named 2 for object type DataFrame"
21+
with pytest.raises(ValueError, match=msg):
22+
df.swapaxes(2, 5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pandas import Index
2+
import pandas._testing as tm
3+
4+
5+
def test_add_prefix_suffix(float_frame):
6+
with_prefix = float_frame.add_prefix("foo#")
7+
expected = Index([f"foo#{c}" for c in float_frame.columns])
8+
tm.assert_index_equal(with_prefix.columns, expected)
9+
10+
with_suffix = float_frame.add_suffix("#foo")
11+
expected = Index([f"{c}#foo" for c in float_frame.columns])
12+
tm.assert_index_equal(with_suffix.columns, expected)
13+
14+
with_pct_prefix = float_frame.add_prefix("%")
15+
expected = Index([f"%{c}" for c in float_frame.columns])
16+
tm.assert_index_equal(with_pct_prefix.columns, expected)
17+
18+
with_pct_suffix = float_frame.add_suffix("%")
19+
expected = Index([f"{c}%" for c in float_frame.columns])
20+
tm.assert_index_equal(with_pct_suffix.columns, expected)

pandas/tests/frame/test_api.py

+1-173
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
from copy import deepcopy
2-
import datetime
32
import inspect
43
import pydoc
54
import warnings
65

76
import numpy as np
87
import pytest
98

10-
from pandas.compat import IS64, is_platform_windows
119
import pandas.util._test_decorators as td
1210
from pandas.util._test_decorators import async_mark, skip_if_no
1311

1412
import pandas as pd
15-
from pandas import Categorical, DataFrame, Series, date_range, timedelta_range
13+
from pandas import DataFrame, Series, date_range, timedelta_range
1614
import pandas._testing as tm
1715

1816

@@ -30,23 +28,6 @@ def test_getitem_pop_assign_name(self, float_frame):
3028
s2 = s.loc[:]
3129
assert s2.name == "B"
3230

33-
def test_add_prefix_suffix(self, float_frame):
34-
with_prefix = float_frame.add_prefix("foo#")
35-
expected = pd.Index([f"foo#{c}" for c in float_frame.columns])
36-
tm.assert_index_equal(with_prefix.columns, expected)
37-
38-
with_suffix = float_frame.add_suffix("#foo")
39-
expected = pd.Index([f"{c}#foo" for c in float_frame.columns])
40-
tm.assert_index_equal(with_suffix.columns, expected)
41-
42-
with_pct_prefix = float_frame.add_prefix("%")
43-
expected = pd.Index([f"%{c}" for c in float_frame.columns])
44-
tm.assert_index_equal(with_pct_prefix.columns, expected)
45-
46-
with_pct_suffix = float_frame.add_suffix("%")
47-
expected = pd.Index([f"{c}%" for c in float_frame.columns])
48-
tm.assert_index_equal(with_pct_suffix.columns, expected)
49-
5031
def test_get_axis(self, float_frame):
5132
f = float_frame
5233
assert f._get_axis_number(0) == 0
@@ -76,9 +57,6 @@ def test_get_axis(self, float_frame):
7657
with pytest.raises(ValueError, match="No axis named"):
7758
f._get_axis_number(None)
7859

79-
def test_keys(self, float_frame):
80-
assert float_frame.keys() is float_frame.columns
81-
8260
def test_column_contains_raises(self, float_frame):
8361
with pytest.raises(TypeError, match="unhashable type: 'Index'"):
8462
float_frame.columns in float_frame
@@ -149,143 +127,6 @@ def test_empty(self, float_frame, float_string_frame):
149127
del df["A"]
150128
assert not df.empty
151129

152-
def test_iteritems(self):
153-
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
154-
for k, v in df.items():
155-
assert isinstance(v, DataFrame._constructor_sliced)
156-
157-
def test_items(self):
158-
# GH 17213, GH 13918
159-
cols = ["a", "b", "c"]
160-
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=cols)
161-
for c, (k, v) in zip(cols, df.items()):
162-
assert c == k
163-
assert isinstance(v, Series)
164-
assert (df[k] == v).all()
165-
166-
def test_iter(self, float_frame):
167-
assert tm.equalContents(list(float_frame), float_frame.columns)
168-
169-
def test_iterrows(self, float_frame, float_string_frame):
170-
for k, v in float_frame.iterrows():
171-
exp = float_frame.loc[k]
172-
tm.assert_series_equal(v, exp)
173-
174-
for k, v in float_string_frame.iterrows():
175-
exp = float_string_frame.loc[k]
176-
tm.assert_series_equal(v, exp)
177-
178-
def test_iterrows_iso8601(self):
179-
# GH 19671
180-
s = DataFrame(
181-
{
182-
"non_iso8601": ["M1701", "M1802", "M1903", "M2004"],
183-
"iso8601": date_range("2000-01-01", periods=4, freq="M"),
184-
}
185-
)
186-
for k, v in s.iterrows():
187-
exp = s.loc[k]
188-
tm.assert_series_equal(v, exp)
189-
190-
def test_iterrows_corner(self):
191-
# gh-12222
192-
df = DataFrame(
193-
{
194-
"a": [datetime.datetime(2015, 1, 1)],
195-
"b": [None],
196-
"c": [None],
197-
"d": [""],
198-
"e": [[]],
199-
"f": [set()],
200-
"g": [{}],
201-
}
202-
)
203-
expected = Series(
204-
[datetime.datetime(2015, 1, 1), None, None, "", [], set(), {}],
205-
index=list("abcdefg"),
206-
name=0,
207-
dtype="object",
208-
)
209-
_, result = next(df.iterrows())
210-
tm.assert_series_equal(result, expected)
211-
212-
def test_itertuples(self, float_frame):
213-
for i, tup in enumerate(float_frame.itertuples()):
214-
s = DataFrame._constructor_sliced(tup[1:])
215-
s.name = tup[0]
216-
expected = float_frame.iloc[i, :].reset_index(drop=True)
217-
tm.assert_series_equal(s, expected)
218-
219-
df = DataFrame(
220-
{"floats": np.random.randn(5), "ints": range(5)}, columns=["floats", "ints"]
221-
)
222-
223-
for tup in df.itertuples(index=False):
224-
assert isinstance(tup[1], int)
225-
226-
df = DataFrame(data={"a": [1, 2, 3], "b": [4, 5, 6]})
227-
dfaa = df[["a", "a"]]
228-
229-
assert list(dfaa.itertuples()) == [(0, 1, 1), (1, 2, 2), (2, 3, 3)]
230-
231-
# repr with int on 32-bit/windows
232-
if not (is_platform_windows() or not IS64):
233-
assert (
234-
repr(list(df.itertuples(name=None)))
235-
== "[(0, 1, 4), (1, 2, 5), (2, 3, 6)]"
236-
)
237-
238-
tup = next(df.itertuples(name="TestName"))
239-
assert tup._fields == ("Index", "a", "b")
240-
assert (tup.Index, tup.a, tup.b) == tup
241-
assert type(tup).__name__ == "TestName"
242-
243-
df.columns = ["def", "return"]
244-
tup2 = next(df.itertuples(name="TestName"))
245-
assert tup2 == (0, 1, 4)
246-
assert tup2._fields == ("Index", "_1", "_2")
247-
248-
df3 = DataFrame({"f" + str(i): [i] for i in range(1024)})
249-
# will raise SyntaxError if trying to create namedtuple
250-
tup3 = next(df3.itertuples())
251-
assert isinstance(tup3, tuple)
252-
assert hasattr(tup3, "_fields")
253-
254-
# GH 28282
255-
df_254_columns = DataFrame([{f"foo_{i}": f"bar_{i}" for i in range(254)}])
256-
result_254_columns = next(df_254_columns.itertuples(index=False))
257-
assert isinstance(result_254_columns, tuple)
258-
assert hasattr(result_254_columns, "_fields")
259-
260-
df_255_columns = DataFrame([{f"foo_{i}": f"bar_{i}" for i in range(255)}])
261-
result_255_columns = next(df_255_columns.itertuples(index=False))
262-
assert isinstance(result_255_columns, tuple)
263-
assert hasattr(result_255_columns, "_fields")
264-
265-
def test_sequence_like_with_categorical(self):
266-
267-
# GH 7839
268-
# make sure can iterate
269-
df = DataFrame(
270-
{"id": [1, 2, 3, 4, 5, 6], "raw_grade": ["a", "b", "b", "a", "a", "e"]}
271-
)
272-
df["grade"] = Categorical(df["raw_grade"])
273-
274-
# basic sequencing testing
275-
result = list(df.grade.values)
276-
expected = np.array(df.grade.values).tolist()
277-
tm.assert_almost_equal(result, expected)
278-
279-
# iteration
280-
for t in df.itertuples(index=False):
281-
str(t)
282-
283-
for row, s in df.iterrows():
284-
str(s)
285-
286-
for c, col in df.items():
287-
str(s)
288-
289130
def test_len(self, float_frame):
290131
assert len(float_frame) == len(float_frame.index)
291132

@@ -294,15 +135,6 @@ def test_len(self, float_frame):
294135
expected = float_frame.reindex(columns=["A", "B"]).values
295136
tm.assert_almost_equal(arr, expected)
296137

297-
def test_swapaxes(self):
298-
df = DataFrame(np.random.randn(10, 5))
299-
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
300-
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
301-
tm.assert_frame_equal(df, df.swapaxes(0, 0))
302-
msg = "No axis named 2 for object type DataFrame"
303-
with pytest.raises(ValueError, match=msg):
304-
df.swapaxes(2, 5)
305-
306138
def test_axis_aliases(self, float_frame):
307139
f = float_frame
308140

@@ -321,10 +153,6 @@ def test_class_axis(self):
321153
assert pydoc.getdoc(DataFrame.index)
322154
assert pydoc.getdoc(DataFrame.columns)
323155

324-
def test_items_names(self, float_string_frame):
325-
for k, v in float_string_frame.items():
326-
assert v.name == k
327-
328156
def test_series_put_names(self, float_string_frame):
329157
series = float_string_frame._series
330158
for k, v in series.items():

0 commit comments

Comments
 (0)