test_drop.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import pytest
  2. from pandas import (
  3. Index,
  4. Series,
  5. )
  6. import pandas._testing as tm
  7. from pandas.api.types import is_bool_dtype
  8. @pytest.mark.parametrize(
  9. "data, index, drop_labels, axis, expected_data, expected_index",
  10. [
  11. # Unique Index
  12. ([1, 2], ["one", "two"], ["two"], 0, [1], ["one"]),
  13. ([1, 2], ["one", "two"], ["two"], "rows", [1], ["one"]),
  14. ([1, 1, 2], ["one", "two", "one"], ["two"], 0, [1, 2], ["one", "one"]),
  15. # GH 5248 Non-Unique Index
  16. ([1, 1, 2], ["one", "two", "one"], "two", 0, [1, 2], ["one", "one"]),
  17. ([1, 1, 2], ["one", "two", "one"], ["one"], 0, [1], ["two"]),
  18. ([1, 1, 2], ["one", "two", "one"], "one", 0, [1], ["two"]),
  19. ],
  20. )
  21. def test_drop_unique_and_non_unique_index(
  22. data, index, axis, drop_labels, expected_data, expected_index
  23. ):
  24. ser = Series(data=data, index=index)
  25. result = ser.drop(drop_labels, axis=axis)
  26. expected = Series(data=expected_data, index=expected_index)
  27. tm.assert_series_equal(result, expected)
  28. @pytest.mark.parametrize(
  29. "data, index, drop_labels, axis, error_type, error_desc",
  30. [
  31. # single string/tuple-like
  32. (range(3), list("abc"), "bc", 0, KeyError, "not found in axis"),
  33. # bad axis
  34. (range(3), list("abc"), ("a",), 0, KeyError, "not found in axis"),
  35. (range(3), list("abc"), "one", "columns", ValueError, "No axis named columns"),
  36. ],
  37. )
  38. def test_drop_exception_raised(data, index, drop_labels, axis, error_type, error_desc):
  39. ser = Series(data, index=index)
  40. with pytest.raises(error_type, match=error_desc):
  41. ser.drop(drop_labels, axis=axis)
  42. def test_drop_with_ignore_errors():
  43. # errors='ignore'
  44. ser = Series(range(3), index=list("abc"))
  45. result = ser.drop("bc", errors="ignore")
  46. tm.assert_series_equal(result, ser)
  47. result = ser.drop(["a", "d"], errors="ignore")
  48. expected = ser.iloc[1:]
  49. tm.assert_series_equal(result, expected)
  50. # GH 8522
  51. ser = Series([2, 3], index=[True, False])
  52. assert is_bool_dtype(ser.index)
  53. assert ser.index.dtype == bool
  54. result = ser.drop(True)
  55. expected = Series([3], index=[False])
  56. tm.assert_series_equal(result, expected)
  57. @pytest.mark.parametrize("index", [[1, 2, 3], [1, 1, 3]])
  58. @pytest.mark.parametrize("drop_labels", [[], [1], [3]])
  59. def test_drop_empty_list(index, drop_labels):
  60. # GH 21494
  61. expected_index = [i for i in index if i not in drop_labels]
  62. series = Series(index=index, dtype=object).drop(drop_labels)
  63. expected = Series(index=expected_index, dtype=object)
  64. tm.assert_series_equal(series, expected)
  65. @pytest.mark.parametrize(
  66. "data, index, drop_labels",
  67. [
  68. (None, [1, 2, 3], [1, 4]),
  69. (None, [1, 2, 2], [1, 4]),
  70. ([2, 3], [0, 1], [False, True]),
  71. ],
  72. )
  73. def test_drop_non_empty_list(data, index, drop_labels):
  74. # GH 21494 and GH 16877
  75. dtype = object if data is None else None
  76. ser = Series(data=data, index=index, dtype=dtype)
  77. with pytest.raises(KeyError, match="not found in axis"):
  78. ser.drop(drop_labels)
  79. def test_drop_index_ea_dtype(any_numeric_ea_dtype):
  80. # GH#45860
  81. df = Series(100, index=Index([1, 2, 2], dtype=any_numeric_ea_dtype))
  82. idx = Index([df.index[1]])
  83. result = df.drop(idx)
  84. expected = Series(100, index=Index([1], dtype=any_numeric_ea_dtype))
  85. tm.assert_series_equal(result, expected)