Skip to content
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

TST: Add tests for old issues #41482

Merged
merged 10 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,11 @@ def test_apply_np_reducer(float_frame, op, how):
getattr(np, op)(float_frame, axis=0, **kwargs), index=float_frame.columns
)
tm.assert_series_equal(result, expected)


def test_apply_getitem_axis_1():
# GH 13427
df = DataFrame({"a": [0, 1, 2], "b": [1, 2, 3]})
result = df[["a", "a"]].apply(lambda x: x[0] + x[1], axis=1)
expected = Series([0, 2, 4])
tm.assert_series_equal(result, expected)
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,13 @@ def test_drop_with_duplicate_columns2(self):
df2 = df.take([2, 0, 1, 2, 1], axis=1)
result = df2.drop("C", axis=1)
tm.assert_frame_equal(result, expected)

def test_drop_inplace_no_leftover_column_reference(self):
# GH 13934
df = DataFrame()
df["a"] = [1, 2, 3]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it important that this is set here instead of being part of the constructor?

a = df.a
df.drop(["a"], axis=1, inplace=True)
tm.assert_index_equal(df.columns, Index([], dtype="object"))
a -= a.mean()
tm.assert_index_equal(df.columns, Index([], dtype="object"))
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,15 @@ def test_frame_single_columns_object_sum_axis_1():
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("ts_value", [pd.Timestamp("2000-01-01"), pd.NaT])
def test_frame_mixed_numeric_object_with_timestamp(ts_value):
# GH 13912
df = DataFrame({"a": [1], "b": [1.1], "c": ["foo"], "d": [ts_value]})
result = df.sum()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_frame_reductions i think

expected = Series([1, 1.1, "foo"], index=list("abc"))
tm.assert_series_equal(result, expected)


# -------------------------------------------------------------------
# Unsorted
# These arithmetic tests were previously in other files, eventually
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2018,3 +2018,18 @@ def test_stack_nan_level(self):
),
)
tm.assert_frame_equal(result, expected)

def test_unstack_categorical(self):
# GH 14018
idx = MultiIndex.from_product([["A"], [0, 1]])
df = DataFrame({"cat": pd.Categorical(["a", "b"])}, index=idx)
result = df.unstack()
expected = DataFrame(
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pass this as a dict or use from_arrays? IIRC the constructor is wonky specifically if the first listlike is a categorical

pd.Categorical(["a"], categories=["a", "b"]),
pd.Categorical(["b"], categories=["a", "b"]),
],
index=["A"],
columns=MultiIndex.from_tuples([("cat", 0), ("cat", 1)]),
)
tm.assert_frame_equal(result, expected)
12 changes: 12 additions & 0 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,15 @@ def test_union_name_preservation(
else:
expected = Index(vals, name=expected_name)
tm.equalContents(union, expected)

@pytest.mark.parametrize(
"diff_type, expected",
[["difference", [1, "B"]], ["symmetric_difference", [1, 2, "B", "C"]]],
)
def test_difference_object_type(self, diff_type, expected):
# GH 13432
idx1 = Index([0, 1, "A", "B"])
idx2 = Index([0, 2, "A", "C"])
result = getattr(idx1, diff_type)(idx2)
expected = Index(expected)
tm.assert_index_equal(result, expected)
13 changes: 13 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,16 @@ def test_mi_columns_loc_list_label_order():
columns=MultiIndex.from_tuples([("B", 1), ("B", 2), ("A", 1), ("A", 2)]),
)
tm.assert_frame_equal(result, expected)


def test_mi_partial_indexing_list_raises():
# GH 13501
frame = DataFrame(
np.arange(12).reshape((4, 3)),
index=[["a", "a", "b", "b"], [1, 2, 1, 2]],
columns=[["Ohio", "Ohio", "Colorado"], ["Green", "Red", "Green"]],
)
frame.index.names = ["key1", "key2"]
frame.columns.names = ["state", "color"]
with pytest.raises(KeyError, match="\\[2\\] not in index"):
frame.loc[["b", 2], "Colorado"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is bc the user passed a list instead of a tuple?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, as a tuple I think this should correctly select

In [2]: frame
Out[2]:
state      Ohio     Colorado
color     Green Red    Green
key1 key2
a    1        0   1        2
     2        3   4        5
b    1        6   7        8
     2        9  10       11

In [3]:  frame.loc[('b', 2), 'Colorado']
Out[3]:
color
Green    11
Name: (b, 2), dtype: int64

7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,10 @@ def test_iloc_setitem_chained_assignment(self):

df["bb"].iloc[0] = 0.15
assert df["bb"].iloc[0] == 0.15

def test_getitem_loc_assignment_slice_state(self):
# GH 13569
df = DataFrame({"a": [10, 20, 30]})
df["a"].loc[4] = 40
tm.assert_frame_equal(df, DataFrame({"a": [10, 20, 30]}))
tm.assert_series_equal(df["a"], Series([10, 20, 30], name="a"))
8 changes: 8 additions & 0 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for the pandas.io.common functionalities
"""
import codecs
import errno
from functools import partial
from io import (
BytesIO,
Expand Down Expand Up @@ -519,3 +520,10 @@ def test_bad_encdoing_errors():
with tm.ensure_clean() as path:
with pytest.raises(ValueError, match="Invalid value for `encoding_errors`"):
icom.get_handle(path, "w", errors="bad")


def test_errno_attribute():
# GH 13872
with pytest.raises(FileNotFoundError, match="\\[Errno 2\\]") as err:
pd.read_csv("doesnt_exist")
assert err.errno == errno.ENOENT