Skip to content

Commit d391e0b

Browse files
authored
TST/REF: collect tests by method (#37449)
1 parent 31e12a7 commit d391e0b

10 files changed

+119
-139
lines changed

pandas/tests/base/test_conversion.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pandas.core.dtypes.dtypes import DatetimeTZDtype
66

77
import pandas as pd
8-
from pandas import CategoricalIndex, Series, Timedelta, Timestamp
8+
from pandas import CategoricalIndex, Series, Timedelta, Timestamp, date_range
99
import pandas._testing as tm
1010
from pandas.core.arrays import (
1111
DatetimeArray,
@@ -449,3 +449,39 @@ def test_to_numpy_dataframe_single_block_no_mutate():
449449
expected = pd.DataFrame(np.array([1.0, 2.0, np.nan]))
450450
result.to_numpy(na_value=0.0)
451451
tm.assert_frame_equal(result, expected)
452+
453+
454+
class TestAsArray:
455+
@pytest.mark.parametrize("tz", [None, "US/Central"])
456+
def test_asarray_object_dt64(self, tz):
457+
ser = Series(date_range("2000", periods=2, tz=tz))
458+
459+
with tm.assert_produces_warning(None):
460+
# Future behavior (for tzaware case) with no warning
461+
result = np.asarray(ser, dtype=object)
462+
463+
expected = np.array(
464+
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)]
465+
)
466+
tm.assert_numpy_array_equal(result, expected)
467+
468+
def test_asarray_tz_naive(self):
469+
# This shouldn't produce a warning.
470+
ser = Series(date_range("2000", periods=2))
471+
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
472+
result = np.asarray(ser)
473+
474+
tm.assert_numpy_array_equal(result, expected)
475+
476+
def test_asarray_tz_aware(self):
477+
tz = "US/Central"
478+
ser = Series(date_range("2000", periods=2, tz=tz))
479+
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
480+
result = np.asarray(ser, dtype="datetime64[ns]")
481+
482+
tm.assert_numpy_array_equal(result, expected)
483+
484+
# Old behavior with no warning
485+
result = np.asarray(ser, dtype="M8[ns]")
486+
487+
tm.assert_numpy_array_equal(result, expected)

pandas/tests/frame/indexing/test_getitem.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import numpy as np
12
import pytest
23

3-
from pandas import DataFrame, MultiIndex
4+
from pandas import DataFrame, MultiIndex, period_range
5+
import pandas._testing as tm
46

57

68
class TestGetitem:
@@ -14,3 +16,16 @@ def test_getitem_unused_level_raises(self):
1416

1517
with pytest.raises(KeyError, match="notevenone"):
1618
df["notevenone"]
19+
20+
def test_getitem_periodindex(self):
21+
rng = period_range("1/1/2000", periods=5)
22+
df = DataFrame(np.random.randn(10, 5), columns=rng)
23+
24+
ts = df[rng[0]]
25+
tm.assert_series_equal(ts, df.iloc[:, 0])
26+
27+
# GH#1211; smoketest unrelated to the rest of this test
28+
repr(df)
29+
30+
ts = df["1/1/2000"]
31+
tm.assert_series_equal(ts, df.iloc[:, 0])

pandas/tests/frame/methods/test_reindex.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import inspect
23
from itertools import permutations
34

45
import numpy as np
@@ -846,3 +847,20 @@ def test_reindex_with_categoricalindex(self):
846847
df.reindex(["a"], level=1)
847848
with pytest.raises(NotImplementedError, match=msg.format("limit")):
848849
df.reindex(["a"], limit=2)
850+
851+
def test_reindex_signature(self):
852+
sig = inspect.signature(DataFrame.reindex)
853+
parameters = set(sig.parameters)
854+
assert parameters == {
855+
"self",
856+
"labels",
857+
"index",
858+
"columns",
859+
"axis",
860+
"limit",
861+
"copy",
862+
"level",
863+
"method",
864+
"fill_value",
865+
"tolerance",
866+
}

pandas/tests/frame/methods/test_rename.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import ChainMap
2+
import inspect
23

34
import numpy as np
45
import pytest
@@ -8,6 +9,21 @@
89

910

1011
class TestRename:
12+
def test_rename_signature(self):
13+
sig = inspect.signature(DataFrame.rename)
14+
parameters = set(sig.parameters)
15+
assert parameters == {
16+
"self",
17+
"mapper",
18+
"index",
19+
"columns",
20+
"axis",
21+
"inplace",
22+
"copy",
23+
"level",
24+
"errors",
25+
}
26+
1127
@pytest.mark.parametrize("klass", [Series, DataFrame])
1228
def test_rename_mi(self, klass):
1329
obj = klass(

pandas/tests/frame/methods/test_set_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from pandas import (
7+
Categorical,
78
DataFrame,
89
DatetimeIndex,
910
Index,
@@ -372,6 +373,21 @@ def test_construction_with_categorical_index(self):
372373
idf = idf.reset_index().set_index("B")
373374
tm.assert_index_equal(idf.index, ci)
374375

376+
def test_set_index_preserve_categorical_dtype(self):
377+
# GH#13743, GH#13854
378+
df = DataFrame(
379+
{
380+
"A": [1, 2, 1, 1, 2],
381+
"B": [10, 16, 22, 28, 34],
382+
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
383+
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
384+
}
385+
)
386+
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
387+
result = df.set_index(cols).reset_index()
388+
result = result.reindex(columns=df.columns)
389+
tm.assert_frame_equal(result, df)
390+
375391
def test_set_index_datetime(self):
376392
# GH#3950
377393
df = DataFrame(

pandas/tests/frame/test_alter_axes.py

-76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
import inspect
32

43
import numpy as np
54
import pytest
@@ -137,34 +136,6 @@ def test_dti_set_index_reindex(self):
137136

138137
# Renaming
139138

140-
def test_reindex_api_equivalence(self):
141-
# equivalence of the labels/axis and index/columns API's
142-
df = DataFrame(
143-
[[1, 2, 3], [3, 4, 5], [5, 6, 7]],
144-
index=["a", "b", "c"],
145-
columns=["d", "e", "f"],
146-
)
147-
148-
res1 = df.reindex(["b", "a"])
149-
res2 = df.reindex(index=["b", "a"])
150-
res3 = df.reindex(labels=["b", "a"])
151-
res4 = df.reindex(labels=["b", "a"], axis=0)
152-
res5 = df.reindex(["b", "a"], axis=0)
153-
for res in [res2, res3, res4, res5]:
154-
tm.assert_frame_equal(res1, res)
155-
156-
res1 = df.reindex(columns=["e", "d"])
157-
res2 = df.reindex(["e", "d"], axis=1)
158-
res3 = df.reindex(labels=["e", "d"], axis=1)
159-
for res in [res2, res3]:
160-
tm.assert_frame_equal(res1, res)
161-
162-
res1 = df.reindex(index=["b", "a"], columns=["e", "d"])
163-
res2 = df.reindex(columns=["e", "d"], index=["b", "a"])
164-
res3 = df.reindex(labels=["b", "a"], axis=0).reindex(labels=["e", "d"], axis=1)
165-
for res in [res2, res3]:
166-
tm.assert_frame_equal(res1, res)
167-
168139
def test_assign_columns(self, float_frame):
169140
float_frame["hi"] = "there"
170141

@@ -173,38 +144,6 @@ def test_assign_columns(self, float_frame):
173144
tm.assert_series_equal(float_frame["C"], df["baz"], check_names=False)
174145
tm.assert_series_equal(float_frame["hi"], df["foo2"], check_names=False)
175146

176-
def test_rename_signature(self):
177-
sig = inspect.signature(DataFrame.rename)
178-
parameters = set(sig.parameters)
179-
assert parameters == {
180-
"self",
181-
"mapper",
182-
"index",
183-
"columns",
184-
"axis",
185-
"inplace",
186-
"copy",
187-
"level",
188-
"errors",
189-
}
190-
191-
def test_reindex_signature(self):
192-
sig = inspect.signature(DataFrame.reindex)
193-
parameters = set(sig.parameters)
194-
assert parameters == {
195-
"self",
196-
"labels",
197-
"index",
198-
"columns",
199-
"axis",
200-
"limit",
201-
"copy",
202-
"level",
203-
"method",
204-
"fill_value",
205-
"tolerance",
206-
}
207-
208147

209148
class TestIntervalIndex:
210149
def test_setitem(self):
@@ -256,21 +195,6 @@ def test_set_reset_index(self):
256195

257196

258197
class TestCategoricalIndex:
259-
def test_set_index_preserve_categorical_dtype(self):
260-
# GH13743, GH13854
261-
df = DataFrame(
262-
{
263-
"A": [1, 2, 1, 1, 2],
264-
"B": [10, 16, 22, 28, 34],
265-
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
266-
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
267-
}
268-
)
269-
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
270-
result = df.set_index(cols).reset_index()
271-
result = result.reindex(columns=df.columns)
272-
tm.assert_frame_equal(result, df)
273-
274198
@pytest.mark.parametrize(
275199
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
276200
)

pandas/tests/frame/test_period.py

-19
This file was deleted.

pandas/tests/series/test_analytics.py

-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66

77
class TestSeriesAnalytics:
8-
def test_ptp(self):
9-
# GH21614
10-
N = 1000
11-
arr = np.random.randn(N)
12-
ser = Series(arr)
13-
assert np.ptp(ser) == np.ptp(arr)
14-
158
def test_is_monotonic(self):
169

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

pandas/tests/series/test_npfuncs.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Tests for np.foo applied to Series, not necessarily ufuncs.
3+
"""
4+
5+
import numpy as np
6+
7+
from pandas import Series
8+
9+
10+
class TestPtp:
11+
def test_ptp(self):
12+
# GH#21614
13+
N = 1000
14+
arr = np.random.randn(N)
15+
ser = Series(arr)
16+
assert np.ptp(ser) == np.ptp(arr)

pandas/tests/series/test_timeseries.py

-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import pytest
32

43
import pandas as pd
54
from pandas import DataFrame, Series, date_range, timedelta_range
@@ -70,37 +69,3 @@ def test_view_tz(self):
7069
]
7170
)
7271
tm.assert_series_equal(result, expected)
73-
74-
@pytest.mark.parametrize("tz", [None, "US/Central"])
75-
def test_asarray_object_dt64(self, tz):
76-
ser = Series(pd.date_range("2000", periods=2, tz=tz))
77-
78-
with tm.assert_produces_warning(None):
79-
# Future behavior (for tzaware case) with no warning
80-
result = np.asarray(ser, dtype=object)
81-
82-
expected = np.array(
83-
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
84-
)
85-
tm.assert_numpy_array_equal(result, expected)
86-
87-
def test_asarray_tz_naive(self):
88-
# This shouldn't produce a warning.
89-
ser = Series(pd.date_range("2000", periods=2))
90-
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
91-
result = np.asarray(ser)
92-
93-
tm.assert_numpy_array_equal(result, expected)
94-
95-
def test_asarray_tz_aware(self):
96-
tz = "US/Central"
97-
ser = Series(pd.date_range("2000", periods=2, tz=tz))
98-
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
99-
result = np.asarray(ser, dtype="datetime64[ns]")
100-
101-
tm.assert_numpy_array_equal(result, expected)
102-
103-
# Old behavior with no warning
104-
result = np.asarray(ser, dtype="M8[ns]")
105-
106-
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)