Skip to content

Commit 3b3ec4f

Browse files
mroeschkeJulianWgs
authored andcommitted
TST: Add test for old issues (pandas-dev#41431)
1 parent b42b9d7 commit 3b3ec4f

File tree

9 files changed

+144
-1
lines changed

9 files changed

+144
-1
lines changed

pandas/tests/arrays/sparse/test_array.py

+8
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,14 @@ def test_dropna(fill_value):
13131313
tm.assert_equal(df.dropna(), expected_df)
13141314

13151315

1316+
def test_drop_duplicates_fill_value():
1317+
# GH 11726
1318+
df = pd.DataFrame(np.zeros((5, 5))).apply(lambda x: SparseArray(x, fill_value=0))
1319+
result = df.drop_duplicates()
1320+
expected = pd.DataFrame({i: SparseArray([0.0], fill_value=0) for i in range(5)})
1321+
tm.assert_frame_equal(result, expected)
1322+
1323+
13161324
class TestMinMax:
13171325
plain_data = np.arange(5).astype(float)
13181326
data_neg = plain_data * (-1)

pandas/tests/frame/methods/test_to_dict.py

+7
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,10 @@ def test_to_dict_scalar_constructor_orient_dtype(self, data, expected_dtype):
304304
d = df.to_dict(orient="records")
305305
result = type(d[0]["a"])
306306
assert result is expected_dtype
307+
308+
def test_to_dict_mixed_numeric_frame(self):
309+
# GH 12859
310+
df = DataFrame({"a": [1.0], "b": [9.0]})
311+
result = df.reset_index().to_dict("records")
312+
expected = [{"index": 0, "a": 1.0, "b": 9.0}]
313+
assert result == expected

pandas/tests/groupby/test_apply.py

+24
Original file line numberDiff line numberDiff line change
@@ -1121,3 +1121,27 @@ def test_apply_dropna_with_indexed_same():
11211121
)
11221122

11231123
tm.assert_frame_equal(result, expected)
1124+
1125+
1126+
@pytest.mark.parametrize(
1127+
"as_index, expected",
1128+
[
1129+
[
1130+
False,
1131+
DataFrame(
1132+
[[1, 1, 1], [2, 2, 1]], columns=Index(["a", "b", None], dtype=object)
1133+
),
1134+
],
1135+
[
1136+
True,
1137+
Series(
1138+
[1, 1], index=MultiIndex.from_tuples([(1, 1), (2, 2)], names=["a", "b"])
1139+
),
1140+
],
1141+
],
1142+
)
1143+
def test_apply_as_index_constant_lambda(as_index, expected):
1144+
# GH 13217
1145+
df = DataFrame({"a": [1, 1, 2, 2], "b": [1, 1, 2, 2], "c": [1, 1, 1, 1]})
1146+
result = df.groupby(["a", "b"], as_index=as_index).apply(lambda x: 1)
1147+
tm.assert_equal(result, expected)

pandas/tests/groupby/test_apply_mutate.py

+60
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,63 @@ def fn(x):
6868
name="col2",
6969
)
7070
tm.assert_series_equal(result, expected)
71+
72+
73+
def test_apply_mutate_columns_multiindex():
74+
# GH 12652
75+
df = pd.DataFrame(
76+
{
77+
("C", "julian"): [1, 2, 3],
78+
("B", "geoffrey"): [1, 2, 3],
79+
("A", "julian"): [1, 2, 3],
80+
("B", "julian"): [1, 2, 3],
81+
("A", "geoffrey"): [1, 2, 3],
82+
("C", "geoffrey"): [1, 2, 3],
83+
},
84+
columns=pd.MultiIndex.from_tuples(
85+
[
86+
("A", "julian"),
87+
("A", "geoffrey"),
88+
("B", "julian"),
89+
("B", "geoffrey"),
90+
("C", "julian"),
91+
("C", "geoffrey"),
92+
]
93+
),
94+
)
95+
96+
def add_column(grouped):
97+
name = grouped.columns[0][1]
98+
grouped["sum", name] = grouped.sum(axis=1)
99+
return grouped
100+
101+
result = df.groupby(level=1, axis=1).apply(add_column)
102+
expected = pd.DataFrame(
103+
[
104+
[1, 1, 1, 3, 1, 1, 1, 3],
105+
[2, 2, 2, 6, 2, 2, 2, 6],
106+
[
107+
3,
108+
3,
109+
3,
110+
9,
111+
3,
112+
3,
113+
3,
114+
9,
115+
],
116+
],
117+
columns=pd.MultiIndex.from_tuples(
118+
[
119+
("geoffrey", "A", "geoffrey"),
120+
("geoffrey", "B", "geoffrey"),
121+
("geoffrey", "C", "geoffrey"),
122+
("geoffrey", "sum", "geoffrey"),
123+
("julian", "A", "julian"),
124+
("julian", "B", "julian"),
125+
("julian", "C", "julian"),
126+
("julian", "sum", "julian"),
127+
]
128+
),
129+
)
130+
tm.assert_frame_equal(result, expected)

pandas/tests/indexes/object/__init__.py

Whitespace-only changes.

pandas/tests/indexes/test_base.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ async def test_tab_complete_warning(self, ip):
13661366
pytest.importorskip("IPython", minversion="6.0.0")
13671367
from IPython.core.completer import provisionalcompleter
13681368

1369-
code = "import pandas as pd; idx = Index([1, 2])"
1369+
code = "import pandas as pd; idx = pd.Index([1, 2])"
13701370
await ip.run_code(code)
13711371

13721372
# GH 31324 newer jedi version raises Deprecation warning;
@@ -1720,3 +1720,21 @@ def test_validate_1d_input():
17201720
ser = Series(0, range(4))
17211721
with pytest.raises(ValueError, match=msg):
17221722
ser.index = np.array([[2, 3]] * 4)
1723+
1724+
1725+
@pytest.mark.parametrize(
1726+
"klass, extra_kwargs",
1727+
[
1728+
[Index, {}],
1729+
[Int64Index, {}],
1730+
[Float64Index, {}],
1731+
[DatetimeIndex, {}],
1732+
[TimedeltaIndex, {}],
1733+
[PeriodIndex, {"freq": "Y"}],
1734+
],
1735+
)
1736+
def test_construct_from_memoryview(klass, extra_kwargs):
1737+
# GH 13120
1738+
result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
1739+
expected = klass(range(2000, 2005), **extra_kwargs)
1740+
tm.assert_index_equal(result, expected)

pandas/tests/reshape/merge/test_merge.py

+11
Original file line numberDiff line numberDiff line change
@@ -2446,3 +2446,14 @@ def test_merge_duplicate_columns_with_suffix_causing_another_duplicate():
24462446
result = merge(left, right, on="a")
24472447
expected = DataFrame([[1, 1, 1, 1, 2]], columns=["a", "b_x", "b_x", "b_x", "b_y"])
24482448
tm.assert_frame_equal(result, expected)
2449+
2450+
2451+
def test_merge_string_float_column_result():
2452+
# GH 13353
2453+
df1 = DataFrame([[1, 2], [3, 4]], columns=pd.Index(["a", 114.0]))
2454+
df2 = DataFrame([[9, 10], [11, 12]], columns=["x", "y"])
2455+
result = merge(df2, df1, how="inner", left_index=True, right_index=True)
2456+
expected = DataFrame(
2457+
[[9, 10, 1, 2], [11, 12, 3, 4]], columns=pd.Index(["x", "y", "a", 114.0])
2458+
)
2459+
tm.assert_frame_equal(result, expected)

pandas/tests/series/indexing/test_getitem.py

+8
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,11 @@ def test_getitem_categorical_str():
662662
def test_slice_can_reorder_not_uniquely_indexed():
663663
ser = Series(1, index=["a", "a", "b", "b", "c"])
664664
ser[::-1] # it works!
665+
666+
667+
@pytest.mark.parametrize("index_vals", ["aabcd", "aadcb"])
668+
def test_duplicated_index_getitem_positional_indexer(index_vals):
669+
# GH 11747
670+
s = Series(range(5), index=list(index_vals))
671+
result = s[3]
672+
assert result == 3

pandas/tests/series/indexing/test_setitem.py

+7
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ def test_setitem_with_bool_mask_and_values_matching_n_trues_in_length(self):
286286
expected = Series([None] * 3 + list(range(5)) + [None] * 2).astype("object")
287287
tm.assert_series_equal(result, expected)
288288

289+
def test_setitem_nan_with_bool(self):
290+
# GH 13034
291+
result = Series([True, False, True])
292+
result[0] = np.nan
293+
expected = Series([np.nan, False, True], dtype=object)
294+
tm.assert_series_equal(result, expected)
295+
289296

290297
class TestSetitemViewCopySemantics:
291298
def test_setitem_invalidates_datetime_index_freq(self):

0 commit comments

Comments
 (0)