test_drop.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. from pandas.compat import lrange
  5. from pandas.errors import PerformanceWarning
  6. import pandas as pd
  7. from pandas import Index, MultiIndex
  8. import pandas.util.testing as tm
  9. def test_drop(idx):
  10. dropped = idx.drop([('foo', 'two'), ('qux', 'one')])
  11. index = MultiIndex.from_tuples([('foo', 'two'), ('qux', 'one')])
  12. dropped2 = idx.drop(index)
  13. expected = idx[[0, 2, 3, 5]]
  14. tm.assert_index_equal(dropped, expected)
  15. tm.assert_index_equal(dropped2, expected)
  16. dropped = idx.drop(['bar'])
  17. expected = idx[[0, 1, 3, 4, 5]]
  18. tm.assert_index_equal(dropped, expected)
  19. dropped = idx.drop('foo')
  20. expected = idx[[2, 3, 4, 5]]
  21. tm.assert_index_equal(dropped, expected)
  22. index = MultiIndex.from_tuples([('bar', 'two')])
  23. pytest.raises(KeyError, idx.drop, [('bar', 'two')])
  24. pytest.raises(KeyError, idx.drop, index)
  25. pytest.raises(KeyError, idx.drop, ['foo', 'two'])
  26. # partially correct argument
  27. mixed_index = MultiIndex.from_tuples([('qux', 'one'), ('bar', 'two')])
  28. pytest.raises(KeyError, idx.drop, mixed_index)
  29. # error='ignore'
  30. dropped = idx.drop(index, errors='ignore')
  31. expected = idx[[0, 1, 2, 3, 4, 5]]
  32. tm.assert_index_equal(dropped, expected)
  33. dropped = idx.drop(mixed_index, errors='ignore')
  34. expected = idx[[0, 1, 2, 3, 5]]
  35. tm.assert_index_equal(dropped, expected)
  36. dropped = idx.drop(['foo', 'two'], errors='ignore')
  37. expected = idx[[2, 3, 4, 5]]
  38. tm.assert_index_equal(dropped, expected)
  39. # mixed partial / full drop
  40. dropped = idx.drop(['foo', ('qux', 'one')])
  41. expected = idx[[2, 3, 5]]
  42. tm.assert_index_equal(dropped, expected)
  43. # mixed partial / full drop / error='ignore'
  44. mixed_index = ['foo', ('qux', 'one'), 'two']
  45. pytest.raises(KeyError, idx.drop, mixed_index)
  46. dropped = idx.drop(mixed_index, errors='ignore')
  47. expected = idx[[2, 3, 5]]
  48. tm.assert_index_equal(dropped, expected)
  49. def test_droplevel_with_names(idx):
  50. index = idx[idx.get_loc('foo')]
  51. dropped = index.droplevel(0)
  52. assert dropped.name == 'second'
  53. index = MultiIndex(
  54. levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))],
  55. codes=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array(
  56. [0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])],
  57. names=['one', 'two', 'three'])
  58. dropped = index.droplevel(0)
  59. assert dropped.names == ('two', 'three')
  60. dropped = index.droplevel('two')
  61. expected = index.droplevel(1)
  62. assert dropped.equals(expected)
  63. def test_droplevel_list():
  64. index = MultiIndex(
  65. levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))],
  66. codes=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array(
  67. [0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])],
  68. names=['one', 'two', 'three'])
  69. dropped = index[:2].droplevel(['three', 'one'])
  70. expected = index[:2].droplevel(2).droplevel(0)
  71. assert dropped.equals(expected)
  72. dropped = index[:2].droplevel([])
  73. expected = index[:2]
  74. assert dropped.equals(expected)
  75. with pytest.raises(ValueError):
  76. index[:2].droplevel(['one', 'two', 'three'])
  77. with pytest.raises(KeyError):
  78. index[:2].droplevel(['one', 'four'])
  79. def test_drop_not_lexsorted():
  80. # GH 12078
  81. # define the lexsorted version of the multi-index
  82. tuples = [('a', ''), ('b1', 'c1'), ('b2', 'c2')]
  83. lexsorted_mi = MultiIndex.from_tuples(tuples, names=['b', 'c'])
  84. assert lexsorted_mi.is_lexsorted()
  85. # and the not-lexsorted version
  86. df = pd.DataFrame(columns=['a', 'b', 'c', 'd'],
  87. data=[[1, 'b1', 'c1', 3], [1, 'b2', 'c2', 4]])
  88. df = df.pivot_table(index='a', columns=['b', 'c'], values='d')
  89. df = df.reset_index()
  90. not_lexsorted_mi = df.columns
  91. assert not not_lexsorted_mi.is_lexsorted()
  92. # compare the results
  93. tm.assert_index_equal(lexsorted_mi, not_lexsorted_mi)
  94. with tm.assert_produces_warning(PerformanceWarning):
  95. tm.assert_index_equal(lexsorted_mi.drop('a'),
  96. not_lexsorted_mi.drop('a'))