From 0a06f60b4c26f0d8088e44f785860ce75a67952e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 20:48:32 -0700 Subject: [PATCH 01/10] Add test for GH 13427 --- pandas/tests/apply/test_frame_apply.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index a724f3d9f2a7d..fcccd0d846d0f 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -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) From e1ca83291642e41eaaf6a4e00f5e2251c7063b69 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:05:01 -0700 Subject: [PATCH 02/10] Add test for GH 13432 --- pandas/tests/indexes/base_class/test_setops.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/indexes/base_class/test_setops.py b/pandas/tests/indexes/base_class/test_setops.py index 2bc9b2cd1a1bd..7a4ba52cdfdd5 100644 --- a/pandas/tests/indexes/base_class/test_setops.py +++ b/pandas/tests/indexes/base_class/test_setops.py @@ -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) From eed059e4cd240c4134e1a100cdd92738397a6dd2 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:10:27 -0700 Subject: [PATCH 03/10] Add test for GH 13501 --- pandas/tests/indexing/multiindex/test_loc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index c87efdbd35fa4..0c6f2faf77f00 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -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"] From 3b7177393dfc2da9b34d6586fc86c1404766b2d2 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:18:29 -0700 Subject: [PATCH 04/10] Add test for GH 13569 --- pandas/tests/indexing/test_chaining_and_caching.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index f450625629c71..a38c652953fab 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -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")) From bfe95bdfd3eab1a27332390c1b4c825eefcb9716 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:36:21 -0700 Subject: [PATCH 05/10] Add test for GH 13872 --- pandas/tests/io/test_common.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index c918832df7aee..9ac8686d83354 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -2,6 +2,7 @@ Tests for the pandas.io.common functionalities """ import codecs +import errno from functools import partial from io import ( BytesIO, @@ -519,3 +520,13 @@ 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\\] No such file or directory: 'doesnt_exist'", + ) as err: + pd.read_csv("doesnt_exist") + assert err.errno == errno.ENOENT From 77bed594b256cc31838c10c53687b8c4b78e66b2 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:45:51 -0700 Subject: [PATCH 06/10] Add test for GH 13912 --- pandas/tests/frame/test_arithmetic.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index b9f6e72acf71b..1eb41cd38bc5b 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -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() + 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 From beb18360aa453f907febd8490ec2be18796db1d0 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 21:50:52 -0700 Subject: [PATCH 07/10] Add test for GH 13934 --- pandas/tests/frame/methods/test_drop.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 5d2aabd372fd1..ec319d16592da 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -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] + 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")) From 8e4833edd8cb3178965e7ba3b058f91181c10656 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 14 May 2021 22:03:57 -0700 Subject: [PATCH 08/10] Add test for GH 14018 --- pandas/tests/frame/test_stack_unstack.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 365d8abcb6bac..2d92df7ea26d8 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -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( + [ + 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) From 2fcdc61a7095d1b14529481520ed9b138ca0d0ce Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 15 May 2021 00:03:27 -0700 Subject: [PATCH 09/10] Shorten match --- pandas/tests/io/test_common.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 9ac8686d83354..7519b24a48566 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -524,9 +524,6 @@ def test_bad_encdoing_errors(): def test_errno_attribute(): # GH 13872 - with pytest.raises( - FileNotFoundError, - match="\\[Errno 2\\] No such file or directory: 'doesnt_exist'", - ) as err: + with pytest.raises(FileNotFoundError, match="\\[Errno 2\\]") as err: pd.read_csv("doesnt_exist") assert err.errno == errno.ENOENT From 92a06a4f6c842a0fa5c47a91ff7fbfee40b2e7da Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 15 May 2021 15:13:30 -0700 Subject: [PATCH 10/10] Address comments --- pandas/tests/frame/methods/test_drop.py | 3 +-- pandas/tests/frame/test_arithmetic.py | 9 --------- pandas/tests/frame/test_reductions.py | 9 +++++++++ pandas/tests/frame/test_stack_unstack.py | 12 ++++++------ 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index ec319d16592da..523e5209f3762 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -484,8 +484,7 @@ def test_drop_with_duplicate_columns2(self): def test_drop_inplace_no_leftover_column_reference(self): # GH 13934 - df = DataFrame() - df["a"] = [1, 2, 3] + df = DataFrame({"a": [1, 2, 3]}) a = df.a df.drop(["a"], axis=1, inplace=True) tm.assert_index_equal(df.columns, Index([], dtype="object")) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index 1eb41cd38bc5b..b9f6e72acf71b 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -1032,15 +1032,6 @@ 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() - 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 diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index 2df59923221ec..0ca523db60889 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -1638,3 +1638,12 @@ def test_groupy_regular_arithmetic_equivalent(meth): result = getattr(df.groupby(level=0), meth)(numeric_only=False) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize("ts_value", [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() + expected = Series([1, 1.1, "foo"], index=list("abc")) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 2d92df7ea26d8..6348014ca72d2 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -2019,17 +2019,17 @@ def test_stack_nan_level(self): ) tm.assert_frame_equal(result, expected) - def test_unstack_categorical(self): + def test_unstack_categorical_columns(self): # GH 14018 idx = MultiIndex.from_product([["A"], [0, 1]]) df = DataFrame({"cat": pd.Categorical(["a", "b"])}, index=idx) result = df.unstack() expected = DataFrame( - [ - pd.Categorical(["a"], categories=["a", "b"]), - pd.Categorical(["b"], categories=["a", "b"]), - ], + { + 0: pd.Categorical(["a"], categories=["a", "b"]), + 1: pd.Categorical(["b"], categories=["a", "b"]), + }, index=["A"], - columns=MultiIndex.from_tuples([("cat", 0), ("cat", 1)]), ) + expected.columns = MultiIndex.from_tuples([("cat", 0), ("cat", 1)]) tm.assert_frame_equal(result, expected)