Skip to content

Commit 3cb6ee8

Browse files
authored
TST/REF: collect/parametrize tests from tests.generic (#37730)
1 parent 68f53cd commit 3cb6ee8

File tree

7 files changed

+228
-208
lines changed

7 files changed

+228
-208
lines changed

pandas/tests/frame/test_alter_axes.py

+65-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime
22

33
import numpy as np
4+
import pytest
5+
import pytz
46

57
from pandas.core.dtypes.common import (
68
is_categorical_dtype,
@@ -17,19 +19,16 @@
1719
Timestamp,
1820
cut,
1921
date_range,
20-
to_datetime,
2122
)
2223
import pandas._testing as tm
2324

2425

2526
class TestDataFrameAlterAxes:
26-
def test_convert_dti_to_series(self):
27-
# don't cast a DatetimeIndex WITH a tz, leave as object
28-
# GH 6032
29-
idx = DatetimeIndex(
30-
to_datetime(["2013-1-1 13:00", "2013-1-2 14:00"]), name="B"
31-
).tz_localize("US/Pacific")
32-
df = DataFrame(np.random.randn(2, 1), columns=["A"])
27+
@pytest.fixture
28+
def idx_expected(self):
29+
idx = DatetimeIndex(["2013-1-1 13:00", "2013-1-2 14:00"], name="B").tz_localize(
30+
"US/Pacific"
31+
)
3332

3433
expected = Series(
3534
np.array(
@@ -41,49 +40,76 @@ def test_convert_dti_to_series(self):
4140
),
4241
name="B",
4342
)
43+
assert expected.dtype == idx.dtype
44+
return idx, expected
4445

45-
# convert index to series
46-
result = Series(idx)
47-
tm.assert_series_equal(result, expected)
48-
49-
# assign to frame
50-
df["B"] = idx
51-
result = df["B"]
52-
tm.assert_series_equal(result, expected)
53-
46+
def test_to_series_keep_tz_deprecated_true(self, idx_expected):
5447
# convert to series while keeping the timezone
48+
idx, expected = idx_expected
49+
5550
msg = "stop passing 'keep_tz'"
5651
with tm.assert_produces_warning(FutureWarning) as m:
5752
result = idx.to_series(keep_tz=True, index=[0, 1])
53+
assert msg in str(m[0].message)
54+
5855
tm.assert_series_equal(result, expected)
56+
57+
def test_to_series_keep_tz_deprecated_false(self, idx_expected):
58+
idx, expected = idx_expected
59+
60+
with tm.assert_produces_warning(FutureWarning) as m:
61+
result = idx.to_series(keep_tz=False, index=[0, 1])
62+
tm.assert_series_equal(result, expected.dt.tz_convert(None))
63+
msg = "do 'idx.tz_convert(None)' before calling"
5964
assert msg in str(m[0].message)
6065

66+
def test_setitem_dt64series(self, idx_expected):
6167
# convert to utc
68+
idx, expected = idx_expected
69+
df = DataFrame(np.random.randn(2, 1), columns=["A"])
70+
df["B"] = idx
71+
6272
with tm.assert_produces_warning(FutureWarning) as m:
6373
df["B"] = idx.to_series(keep_tz=False, index=[0, 1])
64-
result = df["B"]
65-
comp = Series(DatetimeIndex(expected.values).tz_localize(None), name="B")
66-
tm.assert_series_equal(result, comp)
6774
msg = "do 'idx.tz_convert(None)' before calling"
6875
assert msg in str(m[0].message)
6976

70-
result = idx.to_series(index=[0, 1])
77+
result = df["B"]
78+
comp = Series(idx.tz_convert("UTC").tz_localize(None), name="B")
79+
tm.assert_series_equal(result, comp)
80+
81+
def test_setitem_datetimeindex(self, idx_expected):
82+
# setting a DataFrame column with a tzaware DTI retains the dtype
83+
idx, expected = idx_expected
84+
df = DataFrame(np.random.randn(2, 1), columns=["A"])
85+
86+
# assign to frame
87+
df["B"] = idx
88+
result = df["B"]
7189
tm.assert_series_equal(result, expected)
7290

73-
with tm.assert_produces_warning(FutureWarning) as m:
74-
result = idx.to_series(keep_tz=False, index=[0, 1])
75-
tm.assert_series_equal(result, expected.dt.tz_convert(None))
76-
msg = "do 'idx.tz_convert(None)' before calling"
77-
assert msg in str(m[0].message)
91+
def test_setitem_object_array_of_tzaware_datetimes(self, idx_expected):
92+
# setting a DataFrame column with a tzaware DTI retains the dtype
93+
idx, expected = idx_expected
94+
df = DataFrame(np.random.randn(2, 1), columns=["A"])
7895

79-
# list of datetimes with a tz
96+
# object array of datetimes with a tz
8097
df["B"] = idx.to_pydatetime()
8198
result = df["B"]
8299
tm.assert_series_equal(result, expected)
83100

101+
def test_constructor_from_tzaware_datetimeindex(self, idx_expected):
102+
# don't cast a DatetimeIndex WITH a tz, leave as object
103+
# GH 6032
104+
idx, expected = idx_expected
105+
106+
# convert index to series
107+
result = Series(idx)
108+
tm.assert_series_equal(result, expected)
109+
110+
def test_set_axis_setattr_index(self):
84111
# GH 6785
85112
# set the index manually
86-
import pytz
87113

88114
df = DataFrame([{"ts": datetime(2014, 4, 1, tzinfo=pytz.utc), "foo": 1}])
89115
expected = df.set_index("ts")
@@ -102,6 +128,7 @@ def test_dti_set_index_reindex(self):
102128
df = df.reindex(idx2)
103129
tm.assert_index_equal(df.index, idx2)
104130

131+
def test_dti_set_index_reindex_with_tz(self):
105132
# GH 11314
106133
# with tz
107134
index = date_range(
@@ -130,16 +157,16 @@ class TestIntervalIndex:
130157
def test_setitem(self):
131158

132159
df = DataFrame({"A": range(10)})
133-
s = cut(df.A, 5)
134-
assert isinstance(s.cat.categories, IntervalIndex)
160+
ser = cut(df["A"], 5)
161+
assert isinstance(ser.cat.categories, IntervalIndex)
135162

136163
# B & D end up as Categoricals
137164
# the remainer are converted to in-line objects
138165
# contining an IntervalIndex.values
139-
df["B"] = s
140-
df["C"] = np.array(s)
141-
df["D"] = s.values
142-
df["E"] = np.array(s.values)
166+
df["B"] = ser
167+
df["C"] = np.array(ser)
168+
df["D"] = ser.values
169+
df["E"] = np.array(ser.values)
143170

144171
assert is_categorical_dtype(df["B"].dtype)
145172
assert is_interval_dtype(df["B"].cat.categories)
@@ -152,17 +179,17 @@ def test_setitem(self):
152179
# they compare equal as Index
153180
# when converted to numpy objects
154181
c = lambda x: Index(np.array(x))
155-
tm.assert_index_equal(c(df.B), c(df.B), check_names=False)
182+
tm.assert_index_equal(c(df.B), c(df.B))
156183
tm.assert_index_equal(c(df.B), c(df.C), check_names=False)
157184
tm.assert_index_equal(c(df.B), c(df.D), check_names=False)
158-
tm.assert_index_equal(c(df.B), c(df.D), check_names=False)
185+
tm.assert_index_equal(c(df.C), c(df.D), check_names=False)
159186

160187
# B & D are the same Series
161-
tm.assert_series_equal(df["B"], df["B"], check_names=False)
188+
tm.assert_series_equal(df["B"], df["B"])
162189
tm.assert_series_equal(df["B"], df["D"], check_names=False)
163190

164191
# C & E are the same Series
165-
tm.assert_series_equal(df["C"], df["C"], check_names=False)
192+
tm.assert_series_equal(df["C"], df["C"])
166193
tm.assert_series_equal(df["C"], df["E"], check_names=False)
167194

168195
def test_set_reset_index(self):

pandas/tests/frame/test_logical_ops.py

+36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@
1111
class TestDataFrameLogicalOperators:
1212
# &, |, ^
1313

14+
@pytest.mark.parametrize(
15+
"left, right, op, expected",
16+
[
17+
(
18+
[True, False, np.nan],
19+
[True, False, True],
20+
operator.and_,
21+
[True, False, False],
22+
),
23+
(
24+
[True, False, True],
25+
[True, False, np.nan],
26+
operator.and_,
27+
[True, False, False],
28+
),
29+
(
30+
[True, False, np.nan],
31+
[True, False, True],
32+
operator.or_,
33+
[True, False, False],
34+
),
35+
(
36+
[True, False, True],
37+
[True, False, np.nan],
38+
operator.or_,
39+
[True, False, True],
40+
),
41+
],
42+
)
43+
def test_logical_operators_nans(self, left, right, op, expected, frame_or_series):
44+
# GH#13896
45+
result = op(frame_or_series(left), frame_or_series(right))
46+
expected = frame_or_series(expected)
47+
48+
tm.assert_equal(result, expected)
49+
1450
def test_logical_ops_empty_frame(self):
1551
# GH#5808
1652
# empty frames, non-mixed dtype

pandas/tests/frame/test_nonunique_indexes.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import pandas._testing as tm
77

88

9+
def check(result, expected=None):
10+
if expected is not None:
11+
tm.assert_frame_equal(result, expected)
12+
result.dtypes
13+
str(result)
14+
15+
916
class TestDataFrameNonuniqueIndexes:
1017
def test_column_dups_operations(self):
11-
def check(result, expected=None):
12-
if expected is not None:
13-
tm.assert_frame_equal(result, expected)
14-
result.dtypes
15-
str(result)
1618

1719
# assignment
1820
# GH 3687
@@ -308,13 +310,7 @@ def test_column_dups2(self):
308310
result = df.dropna(subset=["A", "C"], how="all")
309311
tm.assert_frame_equal(result, expected)
310312

311-
def test_column_dups_indexing(self):
312-
def check(result, expected=None):
313-
if expected is not None:
314-
tm.assert_frame_equal(result, expected)
315-
result.dtypes
316-
str(result)
317-
313+
def test_getitem_boolean_series_with_duplicate_columns(self):
318314
# boolean indexing
319315
# GH 4879
320316
dups = ["A", "A", "C", "D"]
@@ -327,22 +323,32 @@ def check(result, expected=None):
327323
result = df[df.C > 6]
328324
check(result, expected)
329325

326+
def test_getitem_boolean_frame_with_duplicate_columns(self):
327+
dups = ["A", "A", "C", "D"]
328+
330329
# where
331330
df = DataFrame(
332331
np.arange(12).reshape(3, 4), columns=["A", "B", "C", "D"], dtype="float64"
333332
)
333+
# `df > 6` is a DataFrame with the same shape+alignment as df
334334
expected = df[df > 6]
335335
expected.columns = dups
336336
df = DataFrame(np.arange(12).reshape(3, 4), columns=dups, dtype="float64")
337337
result = df[df > 6]
338338
check(result, expected)
339339

340+
def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self):
341+
# `df.A > 6` is a DataFrame with a different shape from df
342+
dups = ["A", "A", "C", "D"]
343+
340344
# boolean with the duplicate raises
341345
df = DataFrame(np.arange(12).reshape(3, 4), columns=dups, dtype="float64")
342346
msg = "cannot reindex from a duplicate axis"
343347
with pytest.raises(ValueError, match=msg):
344348
df[df.A > 6]
345349

350+
def test_column_dups_indexing(self):
351+
346352
# dup aligning operations should work
347353
# GH 5185
348354
df1 = DataFrame([1, 2, 3, 4, 5], index=[1, 2, 1, 2, 3])

pandas/tests/generic/test_generic.py

+35-34
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,22 @@ def test_transpose(self):
383383
for df in [tm.makeTimeDataFrame()]:
384384
tm.assert_frame_equal(df.transpose().transpose(), df)
385385

386-
def test_numpy_transpose(self):
387-
msg = "the 'axes' parameter is not supported"
386+
def test_numpy_transpose(self, frame_or_series):
388387

389-
s = tm.makeFloatSeries()
390-
tm.assert_series_equal(np.transpose(s), s)
388+
obj = tm.makeTimeDataFrame()
389+
if frame_or_series is Series:
390+
obj = obj["A"]
391391

392-
with pytest.raises(ValueError, match=msg):
393-
np.transpose(s, axes=1)
392+
if frame_or_series is Series:
393+
# 1D -> np.transpose is no-op
394+
tm.assert_series_equal(np.transpose(obj), obj)
394395

395-
df = tm.makeTimeDataFrame()
396-
tm.assert_frame_equal(np.transpose(np.transpose(df)), df)
396+
# round-trip preserved
397+
tm.assert_equal(np.transpose(np.transpose(obj)), obj)
397398

399+
msg = "the 'axes' parameter is not supported"
398400
with pytest.raises(ValueError, match=msg):
399-
np.transpose(df, axes=1)
401+
np.transpose(obj, axes=1)
400402

401403
def test_take(self):
402404
indices = [1, 5, -2, 6, 3, -1]
@@ -415,23 +417,24 @@ def test_take(self):
415417
)
416418
tm.assert_frame_equal(out, expected)
417419

418-
def test_take_invalid_kwargs(self):
420+
def test_take_invalid_kwargs(self, frame_or_series):
419421
indices = [-3, 2, 0, 1]
420-
s = tm.makeFloatSeries()
421-
df = tm.makeTimeDataFrame()
422422

423-
for obj in (s, df):
424-
msg = r"take\(\) got an unexpected keyword argument 'foo'"
425-
with pytest.raises(TypeError, match=msg):
426-
obj.take(indices, foo=2)
423+
obj = tm.makeTimeDataFrame()
424+
if frame_or_series is Series:
425+
obj = obj["A"]
426+
427+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
428+
with pytest.raises(TypeError, match=msg):
429+
obj.take(indices, foo=2)
427430

428-
msg = "the 'out' parameter is not supported"
429-
with pytest.raises(ValueError, match=msg):
430-
obj.take(indices, out=indices)
431+
msg = "the 'out' parameter is not supported"
432+
with pytest.raises(ValueError, match=msg):
433+
obj.take(indices, out=indices)
431434

432-
msg = "the 'mode' parameter is not supported"
433-
with pytest.raises(ValueError, match=msg):
434-
obj.take(indices, mode="clip")
435+
msg = "the 'mode' parameter is not supported"
436+
with pytest.raises(ValueError, match=msg):
437+
obj.take(indices, mode="clip")
435438

436439
@pytest.mark.parametrize("is_copy", [True, False])
437440
def test_depr_take_kwarg_is_copy(self, is_copy, frame_or_series):
@@ -473,21 +476,19 @@ def test_axis_numbers_deprecated(self, frame_or_series):
473476
obj._AXIS_NUMBERS
474477

475478
def test_flags_identity(self, frame_or_series):
476-
s = Series([1, 2])
479+
obj = Series([1, 2])
477480
if frame_or_series is DataFrame:
478-
s = s.to_frame()
481+
obj = obj.to_frame()
479482

480-
assert s.flags is s.flags
481-
s2 = s.copy()
482-
assert s2.flags is not s.flags
483+
assert obj.flags is obj.flags
484+
obj2 = obj.copy()
485+
assert obj2.flags is not obj.flags
483486

484-
def test_slice_shift_deprecated(self):
487+
def test_slice_shift_deprecated(self, frame_or_series):
485488
# GH 37601
486-
df = DataFrame({"A": [1, 2, 3, 4]})
487-
s = Series([1, 2, 3, 4])
488-
489-
with tm.assert_produces_warning(FutureWarning):
490-
df["A"].slice_shift()
489+
obj = DataFrame({"A": [1, 2, 3, 4]})
490+
if frame_or_series is DataFrame:
491+
obj = obj["A"]
491492

492493
with tm.assert_produces_warning(FutureWarning):
493-
s.slice_shift()
494+
obj.slice_shift()

0 commit comments

Comments
 (0)