Skip to content

Commit 3f39757

Browse files
authored
TST/REF: collect tests by method (#39832)
1 parent 7829f05 commit 3f39757

16 files changed

+276
-309
lines changed

pandas/tests/frame/indexing/test_indexing.py

+9-35
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,8 @@ def test_setitem_frame_mixed(self, float_string_frame):
11461146
f.loc[key] = piece
11471147
tm.assert_almost_equal(f.loc[f.index[0:2], ["A", "B"]].values, piece.values)
11481148

1149-
# rows unaligned
1149+
def test_setitem_frame_mixed_rows_unaligned(self, float_string_frame):
1150+
# GH#3216 rows unaligned
11501151
f = float_string_frame.copy()
11511152
piece = DataFrame(
11521153
[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]],
@@ -1159,7 +1160,8 @@ def test_setitem_frame_mixed(self, float_string_frame):
11591160
f.loc[f.index[0:2:], ["A", "B"]].values, piece.values[0:2]
11601161
)
11611162

1162-
# key is unaligned with values
1163+
def test_setitem_frame_mixed_key_unaligned(self, float_string_frame):
1164+
# GH#3216 key is unaligned with values
11631165
f = float_string_frame.copy()
11641166
piece = f.loc[f.index[:2], ["A"]]
11651167
piece.index = f.index[-2:]
@@ -1168,7 +1170,8 @@ def test_setitem_frame_mixed(self, float_string_frame):
11681170
piece["B"] = np.nan
11691171
tm.assert_almost_equal(f.loc[f.index[-2:], ["A", "B"]].values, piece.values)
11701172

1171-
# ndarray
1173+
def test_setitem_frame_mixed_ndarray(self, float_string_frame):
1174+
# GH#3216 ndarray
11721175
f = float_string_frame.copy()
11731176
piece = float_string_frame.loc[f.index[:2], ["A", "B"]]
11741177
key = (f.index[slice(-2, None)], ["A", "B"])
@@ -1471,28 +1474,14 @@ def test_loc_setitem_datetimeindex_tz(self, idxer, tz_naive_fixture):
14711474
result.loc[:, idxer] = expected
14721475
tm.assert_frame_equal(result, expected)
14731476

1474-
def test_at_time_between_time_datetimeindex(self):
1477+
def test_loc_setitem_time_key(self):
14751478
index = date_range("2012-01-01", "2012-01-05", freq="30min")
14761479
df = DataFrame(np.random.randn(len(index), 5), index=index)
14771480
akey = time(12, 0, 0)
14781481
bkey = slice(time(13, 0, 0), time(14, 0, 0))
14791482
ainds = [24, 72, 120, 168]
14801483
binds = [26, 27, 28, 74, 75, 76, 122, 123, 124, 170, 171, 172]
14811484

1482-
result = df.at_time(akey)
1483-
expected = df.loc[akey]
1484-
expected2 = df.iloc[ainds]
1485-
tm.assert_frame_equal(result, expected)
1486-
tm.assert_frame_equal(result, expected2)
1487-
assert len(result) == 4
1488-
1489-
result = df.between_time(bkey.start, bkey.stop)
1490-
expected = df.loc[bkey]
1491-
expected2 = df.iloc[binds]
1492-
tm.assert_frame_equal(result, expected)
1493-
tm.assert_frame_equal(result, expected2)
1494-
assert len(result) == 12
1495-
14961485
result = df.copy()
14971486
result.loc[akey] = 0
14981487
result = result.loc[akey]
@@ -1529,26 +1518,11 @@ def test_loc_getitem_index_namedtuple(self):
15291518
result = df.loc[IndexType("foo", "bar")]["A"]
15301519
assert result == 1
15311520

1532-
@pytest.mark.parametrize(
1533-
"tpl",
1534-
[
1535-
(1,),
1536-
(
1537-
1,
1538-
2,
1539-
),
1540-
],
1541-
)
1521+
@pytest.mark.parametrize("tpl", [(1,), (1, 2)])
15421522
def test_loc_getitem_index_single_double_tuples(self, tpl):
15431523
# GH 20991
15441524
idx = Index(
1545-
[
1546-
(1,),
1547-
(
1548-
1,
1549-
2,
1550-
),
1551-
],
1525+
[(1,), (1, 2)],
15521526
name="A",
15531527
tupleize_cols=False,
15541528
)

pandas/tests/frame/methods/test_at_time.py

+13
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,16 @@ def test_at_time_axis(self, axis):
113113
result.index = result.index._with_freq(None)
114114
expected.index = expected.index._with_freq(None)
115115
tm.assert_frame_equal(result, expected)
116+
117+
def test_at_time_datetimeindex(self):
118+
index = date_range("2012-01-01", "2012-01-05", freq="30min")
119+
df = DataFrame(np.random.randn(len(index), 5), index=index)
120+
akey = time(12, 0, 0)
121+
ainds = [24, 72, 120, 168]
122+
123+
result = df.at_time(akey)
124+
expected = df.loc[akey]
125+
expected2 = df.iloc[ainds]
126+
tm.assert_frame_equal(result, expected)
127+
tm.assert_frame_equal(result, expected2)
128+
assert len(result) == 4

pandas/tests/frame/methods/test_between_time.py

+13
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,16 @@ def test_between_time_axis_raises(self, axis):
194194
ts.columns = mask
195195
with pytest.raises(TypeError, match=msg):
196196
ts.between_time(stime, etime, axis=1)
197+
198+
def test_between_time_datetimeindex(self):
199+
index = date_range("2012-01-01", "2012-01-05", freq="30min")
200+
df = DataFrame(np.random.randn(len(index), 5), index=index)
201+
bkey = slice(time(13, 0, 0), time(14, 0, 0))
202+
binds = [26, 27, 28, 74, 75, 76, 122, 123, 124, 170, 171, 172]
203+
204+
result = df.between_time(bkey.start, bkey.stop)
205+
expected = df.loc[bkey]
206+
expected2 = df.iloc[binds]
207+
tm.assert_frame_equal(result, expected)
208+
tm.assert_frame_equal(result, expected2)
209+
assert len(result) == 12

pandas/tests/frame/methods/test_describe.py

+11
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,14 @@ def test_describe_when_include_all_exclude_not_allowed(self, exclude):
391391
msg = "exclude must be None when include is 'all'"
392392
with pytest.raises(ValueError, match=msg):
393393
df.describe(include="all", exclude=exclude)
394+
395+
def test_describe_with_duplicate_columns(self):
396+
df = DataFrame(
397+
[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
398+
columns=["bar", "a", "a"],
399+
dtype="float64",
400+
)
401+
result = df.describe()
402+
ser = df.iloc[:, 0].describe()
403+
expected = pd.concat([ser, ser, ser], keys=df.columns, axis=1)
404+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_drop.py

+10
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,13 @@ def test_drop_with_non_unique_multiindex(self):
457457
result = df.drop(index="x")
458458
expected = DataFrame([2], index=MultiIndex.from_arrays([["y"], ["j"]]))
459459
tm.assert_frame_equal(result, expected)
460+
461+
def test_drop_with_duplicate_columns(self):
462+
df = DataFrame(
463+
[[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "a", "a"]
464+
)
465+
result = df.drop(["a"], axis=1)
466+
expected = DataFrame([[1], [1], [1]], columns=["bar"])
467+
tm.assert_frame_equal(result, expected)
468+
result = df.drop("a", axis=1)
469+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_reindex.py

+12
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,18 @@ def test_reindex_dups(self):
644644
with pytest.raises(ValueError, match=msg):
645645
df.reindex(index=list(range(len(df))))
646646

647+
def test_reindex_with_duplicate_columns(self):
648+
649+
# reindex is invalid!
650+
df = DataFrame(
651+
[[1, 5, 7.0], [1, 5, 7.0], [1, 5, 7.0]], columns=["bar", "a", "a"]
652+
)
653+
msg = "cannot reindex from a duplicate axis"
654+
with pytest.raises(ValueError, match=msg):
655+
df.reindex(columns=["bar"])
656+
with pytest.raises(ValueError, match=msg):
657+
df.reindex(columns=["bar", "foo"])
658+
647659
def test_reindex_axis_style(self):
648660
# https://github.com/pandas-dev/pandas/issues/12392
649661
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

pandas/tests/frame/methods/test_rename.py

+45
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import numpy as np
55
import pytest
66

7+
import pandas.util._test_decorators as td
8+
79
from pandas import (
810
DataFrame,
911
Index,
1012
MultiIndex,
1113
Series,
14+
merge,
1215
)
1316
import pandas._testing as tm
1417

@@ -357,3 +360,45 @@ def test_rename_mapper_and_positional_arguments_raises(self):
357360

358361
with pytest.raises(TypeError, match=msg):
359362
df.rename({}, columns={}, index={})
363+
364+
@td.skip_array_manager_not_yet_implemented
365+
def test_rename_with_duplicate_columns(self):
366+
# GH#4403
367+
df4 = DataFrame(
368+
{"RT": [0.0454], "TClose": [22.02], "TExg": [0.0422]},
369+
index=MultiIndex.from_tuples(
370+
[(600809, 20130331)], names=["STK_ID", "RPT_Date"]
371+
),
372+
)
373+
374+
df5 = DataFrame(
375+
{
376+
"RPT_Date": [20120930, 20121231, 20130331],
377+
"STK_ID": [600809] * 3,
378+
"STK_Name": ["饡驦", "饡驦", "饡驦"],
379+
"TClose": [38.05, 41.66, 30.01],
380+
},
381+
index=MultiIndex.from_tuples(
382+
[(600809, 20120930), (600809, 20121231), (600809, 20130331)],
383+
names=["STK_ID", "RPT_Date"],
384+
),
385+
)
386+
# TODO: can we construct this without merge?
387+
k = merge(df4, df5, how="inner", left_index=True, right_index=True)
388+
result = k.rename(columns={"TClose_x": "TClose", "TClose_y": "QT_Close"})
389+
str(result)
390+
result.dtypes
391+
392+
expected = DataFrame(
393+
[[0.0454, 22.02, 0.0422, 20130331, 600809, "饡驦", 30.01]],
394+
columns=[
395+
"RT",
396+
"TClose",
397+
"TExg",
398+
"RPT_Date",
399+
"STK_ID",
400+
"STK_Name",
401+
"QT_Close",
402+
],
403+
).set_index(["STK_ID", "RPT_Date"], drop=False)
404+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_values.py

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def test_values_duplicates(self):
5555

5656
tm.assert_numpy_array_equal(result, expected)
5757

58+
def test_values_with_duplicate_columns(self):
59+
df = DataFrame([[1, 2.5], [3, 4.5]], index=[1, 2], columns=["x", "x"])
60+
result = df.values
61+
expected = np.array([[1, 2.5], [3, 4.5]])
62+
assert (result == expected).all().all()
63+
5864
@pytest.mark.parametrize("constructor", [date_range, period_range])
5965
def test_values_casts_datetimelike_to_object(self, constructor):
6066
series = Series(constructor("2000-01-01", periods=10, freq="D"))

pandas/tests/frame/test_arithmetic.py

+35
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ def any(self, axis=None):
5757
class TestFrameComparisons:
5858
# Specifically _not_ flex-comparisons
5959

60+
def test_comparison_with_categorical_dtype(self):
61+
# GH#12564
62+
63+
df = DataFrame({"A": ["foo", "bar", "baz"]})
64+
exp = DataFrame({"A": [True, False, False]})
65+
66+
res = df == "foo"
67+
tm.assert_frame_equal(res, exp)
68+
69+
# casting to categorical shouldn't affect the result
70+
df["A"] = df["A"].astype("category")
71+
72+
res = df == "foo"
73+
tm.assert_frame_equal(res, exp)
74+
6075
def test_frame_in_list(self):
6176
# GH#12689 this should raise at the DataFrame level, not blocks
6277
df = DataFrame(np.random.randn(6, 4), columns=list("ABCD"))
@@ -597,6 +612,26 @@ def test_flex_add_scalar_fill_value(self):
597612
res = df.add(2, fill_value=0)
598613
tm.assert_frame_equal(res, exp)
599614

615+
def test_sub_alignment_with_duplicate_index(self):
616+
# GH#5185 dup aligning operations should work
617+
df1 = DataFrame([1, 2, 3, 4, 5], index=[1, 2, 1, 2, 3])
618+
df2 = DataFrame([1, 2, 3], index=[1, 2, 3])
619+
expected = DataFrame([0, 2, 0, 2, 2], index=[1, 1, 2, 2, 3])
620+
result = df1.sub(df2)
621+
tm.assert_frame_equal(result, expected)
622+
623+
@pytest.mark.parametrize("op", ["__add__", "__mul__", "__sub__", "__truediv__"])
624+
def test_arithmetic_with_duplicate_columns(self, op):
625+
# operations
626+
df = DataFrame({"A": np.arange(10), "B": np.random.rand(10)})
627+
expected = getattr(df, op)(df)
628+
expected.columns = ["A", "A"]
629+
df.columns = ["A", "A"]
630+
result = getattr(df, op)(df)
631+
tm.assert_frame_equal(result, expected)
632+
str(result)
633+
result.dtypes
634+
600635

601636
class TestFrameArithmetic:
602637
def test_td64_op_nat_casting(self):

0 commit comments

Comments
 (0)