From 91dd7d411b601f79885023e04d28a283b18653bb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 21 Nov 2019 20:11:26 -0800 Subject: [PATCH 1/2] DEPR: indexing with a listlike with missing indexer raises --- pandas/core/indexing.py | 16 +-- pandas/io/formats/excel.py | 11 +- pandas/tests/indexing/test_datetime.py | 16 +-- pandas/tests/indexing/test_floats.py | 28 ++--- pandas/tests/indexing/test_iloc.py | 17 +-- pandas/tests/indexing/test_indexing.py | 56 ++------- pandas/tests/indexing/test_loc.py | 84 ++++++-------- pandas/tests/indexing/test_partial.py | 108 +++++++----------- pandas/tests/io/excel/test_writers.py | 6 +- pandas/tests/series/indexing/test_indexing.py | 21 ++-- pandas/tests/series/indexing/test_numeric.py | 6 +- 11 files changed, 125 insertions(+), 244 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index b52015b738c6e..10e8a3601bed5 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1174,18 +1174,12 @@ def _validate_read_indexer( # non-missing values), but a bit later in the # code, so we want to avoid warning & then # just raising - - _missing_key_warning = textwrap.dedent( - """ - Passing list-likes to .loc or [] with any missing label will raise - KeyError in the future, you can use .reindex() as an alternative. - - See the documentation here: - https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike""" # noqa: E501 - ) - if not (ax.is_categorical() or ax.is_interval()): - warnings.warn(_missing_key_warning, FutureWarning, stacklevel=6) + raise KeyError( + "Passing list-likes to .loc or [] with any missing labels " + "is no longer supported, see " + "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501 + ) def _convert_to_indexer(self, obj, axis: int, raise_missing: bool = False): """ diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index cd0889044094f..693b6c6836388 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -393,16 +393,11 @@ def __init__( if not len(Index(cols) & df.columns): raise KeyError("passes columns are not ALL present dataframe") - # deprecatedin gh-17295 - # 1 missing is ok (for now) if len(Index(cols) & df.columns) != len(cols): - warnings.warn( - "Not all names specified in 'columns' are found; " - "this will raise a KeyError in the future", - FutureWarning, - ) + # Deprecated in GH#17295, enforced in 1.0.0 + raise KeyError("Not all names specified in 'columns' are found") - self.df = df.reindex(columns=cols) + self.df = df.reindex(columns=cols) # TODO: Should this change? self.columns = self.df.columns self.float_format = float_format self.index = index diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index ab4a8fe89c6e3..f2e3f7f6b3723 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -2,6 +2,7 @@ from dateutil import tz import numpy as np +import pytest import pandas as pd from pandas import DataFrame, Index, Series, Timestamp, date_range @@ -242,11 +243,8 @@ def test_series_partial_set_datetime(self): Timestamp("2011-01-02"), Timestamp("2011-01-03"), ] - exp = Series( - [np.nan, 0.2, np.nan], index=pd.DatetimeIndex(keys, name="idx"), name="s" - ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - tm.assert_series_equal(ser.loc[keys], exp, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[keys] def test_series_partial_set_period(self): # GH 11497 @@ -273,12 +271,8 @@ def test_series_partial_set_period(self): pd.Period("2011-01-02", freq="D"), pd.Period("2011-01-03", freq="D"), ] - exp = Series( - [np.nan, 0.2, np.nan], index=pd.PeriodIndex(keys, name="idx"), name="s" - ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[keys] - tm.assert_series_equal(result, exp) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[keys] def test_nanosecond_getitem_setitem_with_tz(self): # GH 11679 diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index eadaeaba63a26..0a3b513ff0167 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -726,25 +726,15 @@ def test_floating_misc(self): tm.assert_series_equal(result1, result3) tm.assert_series_equal(result1, result4) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result1 = s[[1.6, 5, 10]] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result2 = s.loc[[1.6, 5, 10]] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result3 = s.loc[[1.6, 5, 10]] - tm.assert_series_equal(result1, result2) - tm.assert_series_equal(result1, result3) - tm.assert_series_equal(result1, Series([np.nan, 2, 4], index=[1.6, 5, 10])) - - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result1 = s[[0, 1, 2]] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result2 = s.loc[[0, 1, 2]] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result3 = s.loc[[0, 1, 2]] - tm.assert_series_equal(result1, result2) - tm.assert_series_equal(result1, result3) - tm.assert_series_equal(result1, Series([0.0, np.nan, np.nan], index=[0, 1, 2])) + with pytest.raises(KeyError, match="with any missing labels"): + s[[1.6, 5, 10]] + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[[1.6, 5, 10]] + + with pytest.raises(KeyError, match="with any missing labels"): + s[[0, 1, 2]] + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[[0, 1, 2]] result1 = s.loc[[2.5, 5]] result2 = s.loc[[2.5, 5]] diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 7c1d8ddd14317..16589d24bafc4 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -750,20 +750,9 @@ def test_iloc_non_unique_indexing(self): df2 = DataFrame({"A": [0.1] * 1000, "B": [1] * 1000}) df2 = concat([df2, 2 * df2, 3 * df2]) - sidx = df2.index.to_series() - expected = df2.iloc[idx[idx <= sidx.max()]] - - new_list = [] - for r, s in expected.iterrows(): - new_list.append(s) - new_list.append(s * 2) - new_list.append(s * 3) - - expected = DataFrame(new_list) - expected = concat([expected, DataFrame(index=idx[idx > sidx.max()])], sort=True) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df2.loc[idx] - tm.assert_frame_equal(result, expected, check_index_type=False) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should non-uniquness play a part in the error message? + df2.loc[idx] def test_iloc_empty_list_indexer_is_ok(self): diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index fc5753ec2955c..3d67fbb971cc6 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -299,32 +299,13 @@ def test_dups_fancy_indexing(self): tm.assert_frame_equal(result, expected) rows = ["C", "B", "E"] - expected = DataFrame( - { - "test": [11, 9, np.nan], - "test1": [7.0, 6, np.nan], - "other": ["d", "c", np.nan], - }, - index=rows, - ) - - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[rows] - tm.assert_frame_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[rows] # see GH5553, make sure we use the right indexer rows = ["F", "G", "H", "C", "B", "E"] - expected = DataFrame( - { - "test": [np.nan, np.nan, np.nan, 11, 9, np.nan], - "test1": [np.nan, np.nan, np.nan, 7.0, 6, np.nan], - "other": [np.nan, np.nan, np.nan, "d", "c", np.nan], - }, - index=rows, - ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[rows] - tm.assert_frame_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[rows] # List containing only missing label dfnu = DataFrame(np.random.randn(5, 3), index=list("AABCD")) @@ -340,38 +321,25 @@ def test_dups_fancy_indexing(self): # GH 4619; duplicate indexer with missing label df = DataFrame({"A": [0, 1, 2]}) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[[0, 8, 0]] - expected = DataFrame({"A": [0, np.nan, 0]}, index=[0, 8, 0]) - tm.assert_frame_equal(result, expected, check_index_type=False) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[[0, 8, 0]] df = DataFrame({"A": list("abc")}) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[[0, 8, 0]] - expected = DataFrame({"A": ["a", np.nan, "a"]}, index=[0, 8, 0]) - tm.assert_frame_equal(result, expected, check_index_type=False) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[[0, 8, 0]] # non unique with non unique selector df = DataFrame({"test": [5, 7, 9, 11]}, index=["A", "A", "B", "C"]) - expected = DataFrame( - {"test": [5, 7, 5, 7, np.nan]}, index=["A", "A", "A", "A", "E"] - ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[["A", "A", "E"]] - tm.assert_frame_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[["A", "A", "E"]] def test_dups_fancy_indexing2(self): # GH 5835 # dups on index and missing values df = DataFrame(np.random.randn(5, 5), columns=["A", "B", "B", "B", "A"]) - expected = pd.concat( - [df.loc[:, ["A", "B"]], DataFrame(np.nan, columns=["C"], index=df.index)], - axis=1, - ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = df.loc[:, ["A", "B", "C"]] - tm.assert_frame_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + df.loc[:, ["A", "B", "C"]] # GH 6504, multi-axis indexing df = DataFrame( diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 732914b3b8947..b3c21d25aa1ec 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -221,51 +221,49 @@ def test_loc_getitem_label_list_with_missing(self): typs=["empty"], fails=KeyError, ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - self.check_result( - "list lbl", - "loc", - [0, 2, 10], - "ix", - [0, 2, 10], - typs=["ints", "uints", "floats"], - axes=0, - fails=KeyError, - ) + self.check_result( + "list lbl", + "loc", + [0, 2, 10], + "ix", + [0, 2, 10], + typs=["ints", "uints", "floats"], + axes=0, + fails=KeyError, + ) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - self.check_result( - "list lbl", - "loc", - [3, 6, 7], - "ix", - [3, 6, 7], - typs=["ints", "uints", "floats"], - axes=1, - fails=KeyError, - ) + self.check_result( + "list lbl", + "loc", + [3, 6, 7], + "ix", + [3, 6, 7], + typs=["ints", "uints", "floats"], + axes=1, + fails=KeyError, + ) # GH 17758 - MultiIndex and missing keys - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - self.check_result( - "list lbl", - "loc", - [(1, 3), (1, 4), (2, 5)], - "ix", - [(1, 3), (1, 4), (2, 5)], - typs=["multi"], - axes=0, - ) + self.check_result( + "list lbl", + "loc", + [(1, 3), (1, 4), (2, 5)], + "ix", + [(1, 3), (1, 4), (2, 5)], + typs=["multi"], + axes=0, + fails=KeyError, + ) def test_getitem_label_list_with_missing(self): s = Series(range(3), index=["a", "b", "c"]) # consistency - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="with any missing labels"): s[["a", "d"]] s = Series(range(3)) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="with any missing labels"): s[[0, 3]] def test_loc_getitem_label_list_fails(self): @@ -386,10 +384,8 @@ def test_loc_to_fail(self): s.loc[["4"]] s.loc[-1] = 3 - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[-1, -2]] - expected = Series([3, np.nan], index=[-1, -2]) - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[[-1, -2]] s["a"] = 2 msg = ( @@ -435,10 +431,8 @@ def test_loc_getitem_list_with_fail(self): s.loc[[3]] # a non-match and a match - with tm.assert_produces_warning(FutureWarning): - expected = s.loc[[2, 3]] - result = s.reindex([2, 3]) - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[[2, 3]] def test_loc_getitem_label_slice(self): @@ -1134,10 +1128,8 @@ def test_series_loc_getitem_label_list_missing_values(): ["2001-01-04", "2001-01-02", "2001-01-04", "2001-01-14"], dtype="datetime64" ) s = Series([2, 5, 8, 11], date_range("2001-01-01", freq="D", periods=4)) - expected = Series([11.0, 5.0, 11.0, np.nan], index=key) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[key] - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[key] @pytest.mark.parametrize( diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 0fb71bfea76c0..5460415a2f01e 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -186,17 +186,15 @@ def test_series_partial_set(self): # loc equiv to .reindex expected = Series([np.nan, 0.2, np.nan], index=[3, 2, 3]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="with any missing labels"): result = ser.loc[[3, 2, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) result = ser.reindex([3, 2, 3]) tm.assert_series_equal(result, expected, check_index_type=True) expected = Series([np.nan, 0.2, np.nan, np.nan], index=[3, 2, 3, "x"]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="with any missing labels"): result = ser.loc[[3, 2, 3, "x"]] - tm.assert_series_equal(result, expected, check_index_type=True) result = ser.reindex([3, 2, 3, "x"]) tm.assert_series_equal(result, expected, check_index_type=True) @@ -206,9 +204,8 @@ def test_series_partial_set(self): tm.assert_series_equal(result, expected, check_index_type=True) expected = Series([0.2, 0.2, np.nan, 0.1], index=[2, 2, "x", 1]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="with any missing labels"): result = ser.loc[[2, 2, "x", 1]] - tm.assert_series_equal(result, expected, check_index_type=True) result = ser.reindex([2, 2, "x", 1]) tm.assert_series_equal(result, expected, check_index_type=True) @@ -222,54 +219,54 @@ def test_series_partial_set(self): ser.loc[[3, 3, 3]] expected = Series([0.2, 0.2, np.nan], index=[2, 2, 3]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[[2, 2, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + ser.loc[[2, 2, 3]] result = ser.reindex([2, 2, 3]) tm.assert_series_equal(result, expected, check_index_type=True) s = Series([0.1, 0.2, 0.3], index=[1, 2, 3]) expected = Series([0.3, np.nan, np.nan], index=[3, 4, 4]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[3, 4, 4]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + s.loc[[3, 4, 4]] result = s.reindex([3, 4, 4]) tm.assert_series_equal(result, expected, check_index_type=True) s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([np.nan, 0.3, 0.3], index=[5, 3, 3]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[5, 3, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + s.loc[[5, 3, 3]] result = s.reindex([5, 3, 3]) tm.assert_series_equal(result, expected, check_index_type=True) s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([np.nan, 0.4, 0.4], index=[5, 4, 4]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[5, 4, 4]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + s.loc[[5, 4, 4]] result = s.reindex([5, 4, 4]) tm.assert_series_equal(result, expected, check_index_type=True) s = Series([0.1, 0.2, 0.3, 0.4], index=[4, 5, 6, 7]) expected = Series([0.4, np.nan, np.nan], index=[7, 2, 2]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[7, 2, 2]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + s.loc[[7, 2, 2]] result = s.reindex([7, 2, 2]) tm.assert_series_equal(result, expected, check_index_type=True) s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([0.4, np.nan, np.nan], index=[4, 5, 5]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s.loc[[4, 5, 5]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + # TODO: should the error message have something to do with duplicates? + s.loc[[4, 5, 5]] result = s.reindex([4, 5, 5]) tm.assert_series_equal(result, expected, check_index_type=True) @@ -286,28 +283,19 @@ def test_series_partial_set_with_name(self): ser = Series([0.1, 0.2], index=idx, name="s") # loc - exp_idx = Index([3, 2, 3], dtype="int64", name="idx") - expected = Series([np.nan, 0.2, np.nan], index=exp_idx, name="s") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[[3, 2, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[[3, 2, 3]] - exp_idx = Index([3, 2, 3, "x"], dtype="object", name="idx") - expected = Series([np.nan, 0.2, np.nan, np.nan], index=exp_idx, name="s") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[[3, 2, 3, "x"]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[[3, 2, 3, "x"]] exp_idx = Index([2, 2, 1], dtype="int64", name="idx") expected = Series([0.2, 0.2, 0.1], index=exp_idx, name="s") result = ser.loc[[2, 2, 1]] tm.assert_series_equal(result, expected, check_index_type=True) - exp_idx = Index([2, 2, "x", 1], dtype="object", name="idx") - expected = Series([0.2, 0.2, np.nan, 0.1], index=exp_idx, name="s") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[[2, 2, "x", 1]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[[2, 2, "x", 1]] # raises as nothing in in the index msg = ( @@ -317,46 +305,28 @@ def test_series_partial_set_with_name(self): with pytest.raises(KeyError, match=msg): ser.loc[[3, 3, 3]] - exp_idx = Index([2, 2, 3], dtype="int64", name="idx") - expected = Series([0.2, 0.2, np.nan], index=exp_idx, name="s") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = ser.loc[[2, 2, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + ser.loc[[2, 2, 3]] - exp_idx = Index([3, 4, 4], dtype="int64", name="idx") - expected = Series([0.3, np.nan, np.nan], index=exp_idx, name="s") idx = Index([1, 2, 3], dtype="int64", name="idx") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = Series([0.1, 0.2, 0.3], index=idx, name="s").loc[[3, 4, 4]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + Series([0.1, 0.2, 0.3], index=idx, name="s").loc[[3, 4, 4]] - exp_idx = Index([5, 3, 3], dtype="int64", name="idx") - expected = Series([np.nan, 0.3, 0.3], index=exp_idx, name="s") idx = Index([1, 2, 3, 4], dtype="int64", name="idx") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 3, 3]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 3, 3]] - exp_idx = Index([5, 4, 4], dtype="int64", name="idx") - expected = Series([np.nan, 0.4, 0.4], index=exp_idx, name="s") idx = Index([1, 2, 3, 4], dtype="int64", name="idx") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 4, 4]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[5, 4, 4]] - exp_idx = Index([7, 2, 2], dtype="int64", name="idx") - expected = Series([0.4, np.nan, np.nan], index=exp_idx, name="s") idx = Index([4, 5, 6, 7], dtype="int64", name="idx") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[7, 2, 2]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[7, 2, 2]] - exp_idx = Index([4, 5, 5], dtype="int64", name="idx") - expected = Series([0.4, np.nan, np.nan], index=exp_idx, name="s") idx = Index([1, 2, 3, 4], dtype="int64", name="idx") - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[4, 5, 5]] - tm.assert_series_equal(result, expected, check_index_type=True) + with pytest.raises(KeyError, match="with any missing labels"): + Series([0.1, 0.2, 0.3, 0.4], index=idx, name="s").loc[[4, 5, 5]] # iloc exp_idx = Index([2, 2, 1, 1], dtype="int64", name="idx") diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py index a7730e079a1bb..b1be0a1a2fece 100644 --- a/pandas/tests/io/excel/test_writers.py +++ b/pandas/tests/io/excel/test_writers.py @@ -1009,13 +1009,9 @@ def test_invalid_columns(self, path): # see gh-10982 write_frame = DataFrame({"A": [1, 1, 1], "B": [2, 2, 2]}) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + with pytest.raises(KeyError, match="Not all names specified"): write_frame.to_excel(path, "test1", columns=["B", "C"]) - expected = write_frame.reindex(columns=["B", "C"]) - read_frame = pd.read_excel(path, "test1", index_col=0) - tm.assert_frame_equal(expected, read_frame) - with pytest.raises( KeyError, match="'passes columns are not ALL present dataframe'" ): diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 5aba2920999d5..173bc9d9d6409 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -52,15 +52,11 @@ def test_basic_getitem_with_labels(datetime_series): s = Series(np.random.randn(10), index=list(range(0, 20, 2))) inds = [0, 2, 5, 7, 8] arr_inds = np.array([0, 2, 5, 7, 8]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s[inds] - expected = s.reindex(inds) - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s[inds] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s[arr_inds] - expected = s.reindex(arr_inds) - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s[arr_inds] # GH12089 # with tz for values @@ -262,12 +258,11 @@ def test_getitem_dups_with_missing(): # breaks reindex, so need to use .loc internally # GH 4246 s = Series([1, 2, 3, 4], ["foo", "bar", "foo", "bah"]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - expected = s.loc[["foo", "bar", "bah", "bam"]] + with pytest.raises(KeyError, match="with any missing labels"): + s.loc[["foo", "bar", "bah", "bam"]] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = s[["foo", "bar", "bah", "bam"]] - tm.assert_series_equal(result, expected) + with pytest.raises(KeyError, match="with any missing labels"): + s[["foo", "bar", "bah", "bam"]] def test_getitem_dups(): diff --git a/pandas/tests/series/indexing/test_numeric.py b/pandas/tests/series/indexing/test_numeric.py index 60b89c01cc22d..426a98b00827e 100644 --- a/pandas/tests/series/indexing/test_numeric.py +++ b/pandas/tests/series/indexing/test_numeric.py @@ -123,12 +123,10 @@ def test_get_nan_multiple(): s = pd.Float64Index(range(10)).to_series() idx = [2, 30] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - tm.assert_series_equal(s.get(idx), Series([2, np.nan], index=idx)) + assert s.get(idx) is None idx = [2, np.nan] - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - tm.assert_series_equal(s.get(idx), Series([2, np.nan], index=idx)) + assert s.get(idx) is None # GH 17295 - all missing keys idx = [20, 30] From dec0ebaa7522e0de5becfc13e285f7bd23b3e95b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 26 Nov 2019 17:25:12 -0800 Subject: [PATCH 2/2] update per comments --- pandas/io/formats/excel.py | 3 ++- pandas/tests/indexing/test_iloc.py | 1 - pandas/tests/indexing/test_partial.py | 6 ------ 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 693b6c6836388..0413dcf18d04a 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -397,7 +397,8 @@ def __init__( # Deprecated in GH#17295, enforced in 1.0.0 raise KeyError("Not all names specified in 'columns' are found") - self.df = df.reindex(columns=cols) # TODO: Should this change? + self.df = df + self.columns = self.df.columns self.float_format = float_format self.index = index diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 16589d24bafc4..ce1e85fdc15d3 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -751,7 +751,6 @@ def test_iloc_non_unique_indexing(self): df2 = concat([df2, 2 * df2, 3 * df2]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should non-uniquness play a part in the error message? df2.loc[idx] def test_iloc_empty_list_indexer_is_ok(self): diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 5460415a2f01e..aa49edd51aa39 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -220,7 +220,6 @@ def test_series_partial_set(self): expected = Series([0.2, 0.2, np.nan], index=[2, 2, 3]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? ser.loc[[2, 2, 3]] result = ser.reindex([2, 2, 3]) @@ -229,7 +228,6 @@ def test_series_partial_set(self): s = Series([0.1, 0.2, 0.3], index=[1, 2, 3]) expected = Series([0.3, np.nan, np.nan], index=[3, 4, 4]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? s.loc[[3, 4, 4]] result = s.reindex([3, 4, 4]) @@ -238,7 +236,6 @@ def test_series_partial_set(self): s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([np.nan, 0.3, 0.3], index=[5, 3, 3]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? s.loc[[5, 3, 3]] result = s.reindex([5, 3, 3]) @@ -247,7 +244,6 @@ def test_series_partial_set(self): s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([np.nan, 0.4, 0.4], index=[5, 4, 4]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? s.loc[[5, 4, 4]] result = s.reindex([5, 4, 4]) @@ -256,7 +252,6 @@ def test_series_partial_set(self): s = Series([0.1, 0.2, 0.3, 0.4], index=[4, 5, 6, 7]) expected = Series([0.4, np.nan, np.nan], index=[7, 2, 2]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? s.loc[[7, 2, 2]] result = s.reindex([7, 2, 2]) @@ -265,7 +260,6 @@ def test_series_partial_set(self): s = Series([0.1, 0.2, 0.3, 0.4], index=[1, 2, 3, 4]) expected = Series([0.4, np.nan, np.nan], index=[4, 5, 5]) with pytest.raises(KeyError, match="with any missing labels"): - # TODO: should the error message have something to do with duplicates? s.loc[[4, 5, 5]] result = s.reindex([4, 5, 5])