Skip to content

TST: Add test for old issues #41431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,14 @@ def test_dropna(fill_value):
tm.assert_equal(df.dropna(), expected_df)


def test_drop_duplicates_fill_value():
# GH 11726
df = pd.DataFrame(np.zeros((5, 5))).apply(lambda x: SparseArray(x, fill_value=0))
result = df.drop_duplicates()
expected = pd.DataFrame({i: SparseArray([0.0], fill_value=0) for i in range(5)})
tm.assert_frame_equal(result, expected)


class TestMinMax:
plain_data = np.arange(5).astype(float)
data_neg = plain_data * (-1)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,10 @@ def test_to_dict_scalar_constructor_orient_dtype(self, data, expected_dtype):
d = df.to_dict(orient="records")
result = type(d[0]["a"])
assert result is expected_dtype

def test_to_dict_mixed_numeric_frame(self):
# GH 12859
df = DataFrame({"a": [1.0], "b": [9.0]})
result = df.reset_index().to_dict("records")
expected = [{"index": 0, "a": 1.0, "b": 9.0}]
assert result == expected
24 changes: 24 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,3 +1121,27 @@ def test_apply_dropna_with_indexed_same():
)

tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"as_index, expected",
[
[
False,
DataFrame(
[[1, 1, 1], [2, 2, 1]], columns=Index(["a", "b", None], dtype=object)
),
],
[
True,
Series(
[1, 1], index=MultiIndex.from_tuples([(1, 1), (2, 2)], names=["a", "b"])
),
],
],
)
def test_apply_as_index_constant_lambda(as_index, expected):
# GH 13217
df = DataFrame({"a": [1, 1, 2, 2], "b": [1, 1, 2, 2], "c": [1, 1, 1, 1]})
result = df.groupby(["a", "b"], as_index=as_index).apply(lambda x: 1)
tm.assert_equal(result, expected)
60 changes: 60 additions & 0 deletions pandas/tests/groupby/test_apply_mutate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,63 @@ def fn(x):
name="col2",
)
tm.assert_series_equal(result, expected)


def test_apply_mutate_columns_multiindex():
# GH 12652
df = pd.DataFrame(
{
("C", "julian"): [1, 2, 3],
("B", "geoffrey"): [1, 2, 3],
("A", "julian"): [1, 2, 3],
("B", "julian"): [1, 2, 3],
("A", "geoffrey"): [1, 2, 3],
("C", "geoffrey"): [1, 2, 3],
},
columns=pd.MultiIndex.from_tuples(
[
("A", "julian"),
("A", "geoffrey"),
("B", "julian"),
("B", "geoffrey"),
("C", "julian"),
("C", "geoffrey"),
]
),
)

def add_column(grouped):
name = grouped.columns[0][1]
grouped["sum", name] = grouped.sum(axis=1)
return grouped

result = df.groupby(level=1, axis=1).apply(add_column)
expected = pd.DataFrame(
[
[1, 1, 1, 3, 1, 1, 1, 3],
[2, 2, 2, 6, 2, 2, 2, 6],
[
3,
3,
3,
9,
3,
3,
3,
9,
],
],
columns=pd.MultiIndex.from_tuples(
[
("geoffrey", "A", "geoffrey"),
("geoffrey", "B", "geoffrey"),
("geoffrey", "C", "geoffrey"),
("geoffrey", "sum", "geoffrey"),
("julian", "A", "julian"),
("julian", "B", "julian"),
("julian", "C", "julian"),
("julian", "sum", "julian"),
]
),
)
tm.assert_frame_equal(result, expected)
Empty file.
20 changes: 19 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ async def test_tab_complete_warning(self, ip):
pytest.importorskip("IPython", minversion="6.0.0")
from IPython.core.completer import provisionalcompleter

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

# GH 31324 newer jedi version raises Deprecation warning;
Expand Down Expand Up @@ -1720,3 +1720,21 @@ def test_validate_1d_input():
ser = Series(0, range(4))
with pytest.raises(ValueError, match=msg):
ser.index = np.array([[2, 3]] * 4)


@pytest.mark.parametrize(
"klass, extra_kwargs",
[
[Index, {}],
[Int64Index, {}],
[Float64Index, {}],
[DatetimeIndex, {}],
[TimedeltaIndex, {}],
[PeriodIndex, {"freq": "Y"}],
],
)
def test_construct_from_memoryview(klass, extra_kwargs):
# GH 13120
result = klass(memoryview(np.arange(2000, 2005)), **extra_kwargs)
expected = klass(range(2000, 2005), **extra_kwargs)
tm.assert_index_equal(result, expected)
11 changes: 11 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2446,3 +2446,14 @@ def test_merge_duplicate_columns_with_suffix_causing_another_duplicate():
result = merge(left, right, on="a")
expected = DataFrame([[1, 1, 1, 1, 2]], columns=["a", "b_x", "b_x", "b_x", "b_y"])
tm.assert_frame_equal(result, expected)


def test_merge_string_float_column_result():
# GH 13353
df1 = DataFrame([[1, 2], [3, 4]], columns=pd.Index(["a", 114.0]))
df2 = DataFrame([[9, 10], [11, 12]], columns=["x", "y"])
result = merge(df2, df1, how="inner", left_index=True, right_index=True)
expected = DataFrame(
[[9, 10, 1, 2], [11, 12, 3, 4]], columns=pd.Index(["x", "y", "a", 114.0])
)
tm.assert_frame_equal(result, expected)
8 changes: 8 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,11 @@ def test_getitem_categorical_str():
def test_slice_can_reorder_not_uniquely_indexed():
ser = Series(1, index=["a", "a", "b", "b", "c"])
ser[::-1] # it works!


@pytest.mark.parametrize("index_vals", ["aabcd", "aadcb"])
def test_duplicated_index_getitem_positional_indexer(index_vals):
# GH 11747
s = Series(range(5), index=list(index_vals))
result = s[3]
assert result == 3
7 changes: 7 additions & 0 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ def test_setitem_with_bool_mask_and_values_matching_n_trues_in_length(self):
expected = Series([None] * 3 + list(range(5)) + [None] * 2).astype("object")
tm.assert_series_equal(result, expected)

def test_setitem_nan_with_bool(self):
# GH 13034
result = Series([True, False, True])
result[0] = np.nan
expected = Series([np.nan, False, True], dtype=object)
tm.assert_series_equal(result, expected)


class TestSetitemViewCopySemantics:
def test_setitem_invalidates_datetime_index_freq(self):
Expand Down