|
| 1 | +from datetime import date, datetime |
| 2 | +import itertools |
| 3 | + |
1 | 4 | import numpy as np
|
2 | 5 | import pytest
|
3 | 6 |
|
|
6 | 9 | from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
|
7 | 10 |
|
8 | 11 | import pandas as pd
|
9 |
| -from pandas import Index, MultiIndex, date_range |
| 12 | +from pandas import Index, MultiIndex, Series, date_range |
10 | 13 | import pandas._testing as tm
|
11 | 14 |
|
12 | 15 |
|
@@ -723,3 +726,73 @@ def test_index_equal_empty_iterable():
|
723 | 726 | a = MultiIndex(levels=[[], []], codes=[[], []], names=["a", "b"])
|
724 | 727 | b = MultiIndex.from_arrays(arrays=[[], []], names=["a", "b"])
|
725 | 728 | tm.assert_index_equal(a, b)
|
| 729 | + |
| 730 | + |
| 731 | +def test_raise_invalid_sortorder(): |
| 732 | + # Test that the MultiIndex constructor raise when a incorrect sortorder is given |
| 733 | + # GH#28518 |
| 734 | + |
| 735 | + levels = [[0, 1], [0, 1, 2]] |
| 736 | + |
| 737 | + # Correct sortorder |
| 738 | + MultiIndex( |
| 739 | + levels=levels, codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], sortorder=2 |
| 740 | + ) |
| 741 | + |
| 742 | + with pytest.raises(ValueError, match=r".* sortorder 2 with lexsort_depth 1.*"): |
| 743 | + MultiIndex( |
| 744 | + levels=levels, codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 2, 1]], sortorder=2, |
| 745 | + ) |
| 746 | + |
| 747 | + with pytest.raises(ValueError, match=r".* sortorder 1 with lexsort_depth 0.*"): |
| 748 | + MultiIndex( |
| 749 | + levels=levels, codes=[[0, 0, 1, 0, 1, 1], [0, 1, 0, 2, 2, 1]], sortorder=1, |
| 750 | + ) |
| 751 | + |
| 752 | + |
| 753 | +def test_datetimeindex(): |
| 754 | + idx1 = pd.DatetimeIndex( |
| 755 | + ["2013-04-01 9:00", "2013-04-02 9:00", "2013-04-03 9:00"] * 2, tz="Asia/Tokyo", |
| 756 | + ) |
| 757 | + idx2 = pd.date_range("2010/01/01", periods=6, freq="M", tz="US/Eastern") |
| 758 | + idx = MultiIndex.from_arrays([idx1, idx2]) |
| 759 | + |
| 760 | + expected1 = pd.DatetimeIndex( |
| 761 | + ["2013-04-01 9:00", "2013-04-02 9:00", "2013-04-03 9:00"], tz="Asia/Tokyo" |
| 762 | + ) |
| 763 | + |
| 764 | + tm.assert_index_equal(idx.levels[0], expected1) |
| 765 | + tm.assert_index_equal(idx.levels[1], idx2) |
| 766 | + |
| 767 | + # from datetime combos |
| 768 | + # GH 7888 |
| 769 | + date1 = date.today() |
| 770 | + date2 = datetime.today() |
| 771 | + date3 = Timestamp.today() |
| 772 | + |
| 773 | + for d1, d2 in itertools.product([date1, date2, date3], [date1, date2, date3]): |
| 774 | + index = MultiIndex.from_product([[d1], [d2]]) |
| 775 | + assert isinstance(index.levels[0], pd.DatetimeIndex) |
| 776 | + assert isinstance(index.levels[1], pd.DatetimeIndex) |
| 777 | + |
| 778 | + |
| 779 | +def test_constructor_with_tz(): |
| 780 | + |
| 781 | + index = pd.DatetimeIndex( |
| 782 | + ["2013/01/01 09:00", "2013/01/02 09:00"], name="dt1", tz="US/Pacific" |
| 783 | + ) |
| 784 | + columns = pd.DatetimeIndex( |
| 785 | + ["2014/01/01 09:00", "2014/01/02 09:00"], name="dt2", tz="Asia/Tokyo" |
| 786 | + ) |
| 787 | + |
| 788 | + result = MultiIndex.from_arrays([index, columns]) |
| 789 | + |
| 790 | + assert result.names == ["dt1", "dt2"] |
| 791 | + tm.assert_index_equal(result.levels[0], index) |
| 792 | + tm.assert_index_equal(result.levels[1], columns) |
| 793 | + |
| 794 | + result = MultiIndex.from_arrays([Series(index), Series(columns)]) |
| 795 | + |
| 796 | + assert result.names == ["dt1", "dt2"] |
| 797 | + tm.assert_index_equal(result.levels[0], index) |
| 798 | + tm.assert_index_equal(result.levels[1], columns) |
0 commit comments