test_constructors.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. Index,
  5. MultiIndex,
  6. )
  7. import pandas._testing as tm
  8. class TestIndexConstructor:
  9. # Tests for the Index constructor, specifically for cases that do
  10. # not return a subclass
  11. @pytest.mark.parametrize("value", [1, np.int64(1)])
  12. def test_constructor_corner(self, value):
  13. # corner case
  14. msg = (
  15. r"Index\(\.\.\.\) must be called with a collection of some "
  16. f"kind, {value} was passed"
  17. )
  18. with pytest.raises(TypeError, match=msg):
  19. Index(value)
  20. @pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
  21. def test_construction_list_mixed_tuples(self, index_vals):
  22. # see gh-10697: if we are constructing from a mixed list of tuples,
  23. # make sure that we are independent of the sorting order.
  24. index = Index(index_vals)
  25. assert isinstance(index, Index)
  26. assert not isinstance(index, MultiIndex)
  27. def test_constructor_cast(self):
  28. msg = "could not convert string to float"
  29. with pytest.raises(ValueError, match=msg):
  30. Index(["a", "b", "c"], dtype=float)
  31. @pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
  32. def test_construct_empty_tuples(self, tuple_list):
  33. # GH #45608
  34. result = Index(tuple_list)
  35. expected = MultiIndex.from_tuples(tuple_list)
  36. tm.assert_index_equal(result, expected)