diff --git a/pandas/tests/indexing/test_categorical.py b/pandas/tests/indexing/test_categorical.py index 1b9b6452b2e33..3b6bc42544c51 100644 --- a/pandas/tests/indexing/test_categorical.py +++ b/pandas/tests/indexing/test_categorical.py @@ -322,6 +322,7 @@ def test_loc_listlike_dtypes(self): with pytest.raises(KeyError, match=re.escape(msg)): df.loc[["a", "x"]] + def test_loc_listlike_dtypes_duplicated_categories_and_codes(self): # duplicated categories and codes index = CategoricalIndex(["a", "b", "a"]) df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=index) @@ -341,9 +342,11 @@ def test_loc_listlike_dtypes(self): ) tm.assert_frame_equal(res, exp, check_index_type=True) + msg = "The following labels were missing: Index(['x'], dtype='object')" with pytest.raises(KeyError, match=re.escape(msg)): df.loc[["a", "x"]] + def test_loc_listlike_dtypes_unused_category(self): # contains unused category index = CategoricalIndex(["a", "b", "a", "c"], categories=list("abcde")) df = DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}, index=index) @@ -363,6 +366,7 @@ def test_loc_listlike_dtypes(self): ) tm.assert_frame_equal(res, exp, check_index_type=True) + msg = "The following labels were missing: Index(['x'], dtype='object')" with pytest.raises(KeyError, match=re.escape(msg)): df.loc[["a", "x"]] @@ -405,6 +409,8 @@ def test_ix_categorical_index(self): expect = DataFrame(df.loc[:, ["X", "Y"]], index=cdf.index, columns=exp_columns) tm.assert_frame_equal(cdf.loc[:, ["X", "Y"]], expect) + def test_ix_categorical_index_non_unique(self): + # non-unique df = DataFrame(np.random.randn(3, 3), index=list("ABA"), columns=list("XYX")) cdf = df.copy() diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 1ac2a16660f93..25d4692e4cd1d 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -373,6 +373,7 @@ def test_setting_with_copy_bug(self): with pytest.raises(com.SettingWithCopyError, match=msg): df[["c"]][mask] = df[["b"]][mask] + def test_setting_with_copy_bug_no_warning(self): # invalid warning as we are returning a new object # GH 8730 df1 = DataFrame({"x": Series(["a", "b", "c"]), "y": Series(["d", "e", "f"])}) diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index 44a5e2ae6d9e9..9f58f4af0ba55 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -37,6 +37,7 @@ def test_indexing_with_datetime_tz(self): ) tm.assert_series_equal(result, expected) + def test_indexing_fast_xs(self): # indexing - fast_xs df = DataFrame({"a": date_range("2014-01-01", periods=10, tz="UTC")}) result = df.iloc[5] @@ -53,6 +54,7 @@ def test_indexing_with_datetime_tz(self): expected = df.iloc[4:] tm.assert_frame_equal(result, expected) + def test_setitem_with_expansion(self): # indexing - setting an element df = DataFrame( data=pd.to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]), @@ -234,21 +236,23 @@ def test_loc_setitem_with_existing_dst(self): def test_getitem_millisecond_resolution(self, frame_or_series): # GH#33589 + + keys = [ + "2017-10-25T16:25:04.151", + "2017-10-25T16:25:04.252", + "2017-10-25T16:50:05.237", + "2017-10-25T16:50:05.238", + ] obj = frame_or_series( [1, 2, 3, 4], - index=[ - Timestamp("2017-10-25T16:25:04.151"), - Timestamp("2017-10-25T16:25:04.252"), - Timestamp("2017-10-25T16:50:05.237"), - Timestamp("2017-10-25T16:50:05.238"), - ], + index=[Timestamp(x) for x in keys], ) - result = obj["2017-10-25T16:25:04.252":"2017-10-25T16:50:05.237"] + result = obj[keys[1] : keys[2]] expected = frame_or_series( [2, 3], index=[ - Timestamp("2017-10-25T16:25:04.252"), - Timestamp("2017-10-25T16:50:05.237"), + Timestamp(keys[1]), + Timestamp(keys[2]), ], ) tm.assert_equal(result, expected) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index dcd073681cecf..63313589d64f7 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -125,6 +125,7 @@ def test_inf_upcast(self): expected = pd.Float64Index([1, 2, np.inf]) tm.assert_index_equal(result, expected) + def test_inf_upcast_empty(self): # Test with np.inf in columns df = DataFrame() df.loc[0, 0] = 1 @@ -148,6 +149,9 @@ def test_setitem_dtype_upcast(self): ) tm.assert_frame_equal(df, expected) + @pytest.mark.parametrize("val", [3.14, "wxyz"]) + def test_setitem_dtype_upcast2(self, val): + # GH10280 df = DataFrame( np.arange(6, dtype="int64").reshape(2, 3), @@ -155,19 +159,19 @@ def test_setitem_dtype_upcast(self): columns=["foo", "bar", "baz"], ) - for val in [3.14, "wxyz"]: - left = df.copy() - left.loc["a", "bar"] = val - right = DataFrame( - [[0, val, 2], [3, 4, 5]], - index=list("ab"), - columns=["foo", "bar", "baz"], - ) + left = df.copy() + left.loc["a", "bar"] = val + right = DataFrame( + [[0, val, 2], [3, 4, 5]], + index=list("ab"), + columns=["foo", "bar", "baz"], + ) - tm.assert_frame_equal(left, right) - assert is_integer_dtype(left["foo"]) - assert is_integer_dtype(left["baz"]) + tm.assert_frame_equal(left, right) + assert is_integer_dtype(left["foo"]) + assert is_integer_dtype(left["baz"]) + def test_setitem_dtype_upcast3(self): left = DataFrame( np.arange(6, dtype="int64").reshape(2, 3) / 10.0, index=list("ab"), @@ -195,6 +199,8 @@ def test_dups_fancy_indexing(self): expected = Index(["b", "a", "a"]) tm.assert_index_equal(result, expected) + def test_dups_fancy_indexing_across_dtypes(self): + # across dtypes df = DataFrame([[1, 2, 1.0, 2.0, 3.0, "foo", "bar"]], columns=list("aaaaaaa")) df.head() @@ -208,6 +214,7 @@ def test_dups_fancy_indexing(self): tm.assert_frame_equal(df, result) + def test_dups_fancy_indexing_not_in_order(self): # GH 3561, dups not in selected order df = DataFrame( {"test": [5, 7, 9, 11], "test1": [4.0, 5, 6, 7], "other": list("abcd")}, @@ -232,6 +239,8 @@ def test_dups_fancy_indexing(self): with pytest.raises(KeyError, match="with any missing labels"): df.loc[rows] + def test_dups_fancy_indexing_only_missing_label(self): + # List containing only missing label dfnu = DataFrame(np.random.randn(5, 3), index=list("AABCD")) with pytest.raises( @@ -244,6 +253,8 @@ def test_dups_fancy_indexing(self): # ToDo: check_index_type can be True after GH 11497 + def test_dups_fancy_indexing_missing_label(self): + # GH 4619; duplicate indexer with missing label df = DataFrame({"A": [0, 1, 2]}) with pytest.raises(KeyError, match="with any missing labels"): @@ -253,6 +264,8 @@ def test_dups_fancy_indexing(self): with pytest.raises(KeyError, match="with any missing labels"): df.loc[[0, 8, 0]] + def test_dups_fancy_indexing_non_unique(self): + # non unique with non unique selector df = DataFrame({"test": [5, 7, 9, 11]}, index=["A", "A", "B", "C"]) with pytest.raises(KeyError, match="with any missing labels"): @@ -447,6 +460,7 @@ def test_multi_assign(self): df2.loc[mask, cols] = dft.loc[mask, cols].values tm.assert_frame_equal(df2, expected) + def test_multi_assign_broadcasting_rhs(self): # broadcasting on the rhs is required df = DataFrame( { @@ -781,14 +795,16 @@ def test_non_reducing_slice(self, slc): tslice_ = non_reducing_slice(slc) assert isinstance(df.loc[tslice_], DataFrame) - def test_list_slice(self): + @pytest.mark.parametrize("box", [list, Series, np.array]) + def test_list_slice(self, box): # like dataframe getitem - slices = [["A"], Series(["A"]), np.array(["A"])] + subset = box(["A"]) + df = DataFrame({"A": [1, 2], "B": [3, 4]}, index=["A", "B"]) expected = pd.IndexSlice[:, ["A"]] - for subset in slices: - result = non_reducing_slice(subset) - tm.assert_frame_equal(df.loc[result], df.loc[expected]) + + result = non_reducing_slice(subset) + tm.assert_frame_equal(df.loc[result], df.loc[expected]) def test_maybe_numeric_slice(self): df = DataFrame({"A": [1, 2], "B": ["c", "d"], "C": [True, False]})