test_compat.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. from pandas.compat import PY3, long
  5. from pandas import MultiIndex
  6. import pandas.util.testing as tm
  7. def test_numeric_compat(idx):
  8. with pytest.raises(TypeError, match="cannot perform __mul__"):
  9. idx * 1
  10. with pytest.raises(TypeError, match="cannot perform __rmul__"):
  11. 1 * idx
  12. div_err = ("cannot perform __truediv__" if PY3
  13. else "cannot perform __div__")
  14. with pytest.raises(TypeError, match=div_err):
  15. idx / 1
  16. div_err = div_err.replace(" __", " __r")
  17. with pytest.raises(TypeError, match=div_err):
  18. 1 / idx
  19. with pytest.raises(TypeError, match="cannot perform __floordiv__"):
  20. idx // 1
  21. with pytest.raises(TypeError, match="cannot perform __rfloordiv__"):
  22. 1 // idx
  23. @pytest.mark.parametrize("method", ["all", "any"])
  24. def test_logical_compat(idx, method):
  25. msg = "cannot perform {method}".format(method=method)
  26. with pytest.raises(TypeError, match=msg):
  27. getattr(idx, method)()
  28. def test_boolean_context_compat(idx):
  29. with pytest.raises(ValueError):
  30. bool(idx)
  31. def test_boolean_context_compat2():
  32. # boolean context compat
  33. # GH7897
  34. i1 = MultiIndex.from_tuples([('A', 1), ('A', 2)])
  35. i2 = MultiIndex.from_tuples([('A', 1), ('A', 3)])
  36. common = i1.intersection(i2)
  37. with pytest.raises(ValueError):
  38. bool(common)
  39. def test_inplace_mutation_resets_values():
  40. levels = [['a', 'b', 'c'], [4]]
  41. levels2 = [[1, 2, 3], ['a']]
  42. codes = [[0, 1, 0, 2, 2, 0], [0, 0, 0, 0, 0, 0]]
  43. mi1 = MultiIndex(levels=levels, codes=codes)
  44. mi2 = MultiIndex(levels=levels2, codes=codes)
  45. vals = mi1.values.copy()
  46. vals2 = mi2.values.copy()
  47. assert mi1._tuples is not None
  48. # Make sure level setting works
  49. new_vals = mi1.set_levels(levels2).values
  50. tm.assert_almost_equal(vals2, new_vals)
  51. # Non-inplace doesn't kill _tuples [implementation detail]
  52. tm.assert_almost_equal(mi1._tuples, vals)
  53. # ...and values is still same too
  54. tm.assert_almost_equal(mi1.values, vals)
  55. # Inplace should kill _tuples
  56. mi1.set_levels(levels2, inplace=True)
  57. tm.assert_almost_equal(mi1.values, vals2)
  58. # Make sure label setting works too
  59. codes2 = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
  60. exp_values = np.empty((6,), dtype=object)
  61. exp_values[:] = [(long(1), 'a')] * 6
  62. # Must be 1d array of tuples
  63. assert exp_values.shape == (6,)
  64. new_values = mi2.set_codes(codes2).values
  65. # Not inplace shouldn't change
  66. tm.assert_almost_equal(mi2._tuples, vals2)
  67. # Should have correct values
  68. tm.assert_almost_equal(exp_values, new_values)
  69. # ...and again setting inplace should kill _tuples, etc
  70. mi2.set_codes(codes2, inplace=True)
  71. tm.assert_almost_equal(mi2.values, new_values)
  72. def test_ndarray_compat_properties(idx, compat_props):
  73. assert idx.T.equals(idx)
  74. assert idx.transpose().equals(idx)
  75. values = idx.values
  76. for prop in compat_props:
  77. assert getattr(idx, prop) == getattr(values, prop)
  78. # test for validity
  79. idx.nbytes
  80. idx.values.nbytes
  81. def test_compat(indices):
  82. assert indices.tolist() == list(indices)
  83. def test_pickle_compat_construction(holder):
  84. # this is testing for pickle compat
  85. if holder is None:
  86. return
  87. # need an object to create with
  88. pytest.raises(TypeError, holder)