Python pandas.core.index.MultiIndex.from_product() Examples

The following are 30 code examples of pandas.core.index.MultiIndex.from_product(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.core.index.MultiIndex , or try the search function .
Example #1
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = pd.MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #2
Source File: test_constructors.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #3
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'], ['big', 'small'], ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'], ['big', 'small'], ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #4
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = MultiIndex.from_product(
            [pd.period_range('20130101', periods=3, freq='M'), list('abc')],
            names=['month', 'feature'])

        df = DataFrame(np.arange(9, dtype='int64').reshape(-1, 1),
                       index=idx, columns=['a'])
        expected = DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #5
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #6
Source File: test_multilevel.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'], ['big', 'small'], ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'], ['big', 'small'], ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #7
Source File: test_multilevel.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = MultiIndex.from_product(
            [pd.period_range('20130101', periods=3, freq='M'), list('abc')],
            names=['month', 'feature'])

        df = DataFrame(np.arange(9, dtype='int64').reshape(-1, 1),
                       index=idx, columns=['a'])
        expected = DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #8
Source File: test_multilevel.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #9
Source File: test_constructors.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #10
Source File: test_categorical.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = pd.Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = pd.Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #11
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = pd.DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'],
                 ['big', 'small'],
                 ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'],
             ['big', 'small'],
             ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #12
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = pd.MultiIndex.from_product([pd.period_range('20130101',
                                                          periods=3, freq='M'),
                                          ['a', 'b', 'c']],
                                         names=['month', 'feature'])

        df = pd.DataFrame(np.arange(9, dtype='int64')
                          .reshape(-1, 1),
                          index=idx, columns=['a'])
        expected = pd.DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #13
Source File: test_constructors.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #14
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'], ['big', 'small'], ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'], ['big', 'small'], ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #15
Source File: test_multilevel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #16
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = MultiIndex.from_product(
            [pd.period_range('20130101', periods=3, freq='M'), list('abc')],
            names=['month', 'feature'])

        df = DataFrame(np.arange(9, dtype='int64').reshape(-1, 1),
                       index=idx, columns=['a'])
        expected = DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #17
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #18
Source File: test_multilevel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = MultiIndex.from_product(
            [pd.period_range('20130101', periods=3, freq='M'), list('abc')],
            names=['month', 'feature'])

        df = DataFrame(np.arange(9, dtype='int64').reshape(-1, 1),
                       index=idx, columns=['a'])
        expected = DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #19
Source File: test_constructors.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #20
Source File: test_constructors.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_constructor_with_generator(self):
        # This was raising an Error in isna(single_val).any() because isna
        # returned a scalar for a generator
        xrange = range

        exp = Categorical([0, 1, 2])
        cat = Categorical((x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical(xrange(3))
        tm.assert_categorical_equal(cat, exp)

        # This uses xrange internally
        from pandas.core.index import MultiIndex
        MultiIndex.from_product([range(5), ['a', 'b', 'c']])

        # check that categories accept generators and sequences
        cat = Categorical([0, 1, 2], categories=(x for x in [0, 1, 2]))
        tm.assert_categorical_equal(cat, exp)
        cat = Categorical([0, 1, 2], categories=xrange(3))
        tm.assert_categorical_equal(cat, exp) 
Example #21
Source File: test_multilevel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'], ['big', 'small'], ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'], ['big', 'small'], ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #22
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_sort_index_reorder_on_ops(self):
        # 15687
        df = DataFrame(
            np.random.randn(8, 2),
            index=MultiIndex.from_product(
                [['a', 'b'], ['big', 'small'], ['red', 'blu']],
                names=['letter', 'size', 'color']),
            columns=['near', 'far'])
        df = df.sort_index()

        def my_func(group):
            group.index = ['newz', 'newa']
            return group

        result = df.groupby(level=['letter', 'size']).apply(
            my_func).sort_index()
        expected = MultiIndex.from_product(
            [['a', 'b'], ['big', 'small'], ['newa', 'newz']],
            names=['letter', 'size', None])

        tm.assert_index_equal(result.index, expected) 
Example #23
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_datetimeindex(self):
        idx1 = pd.DatetimeIndex(
            ['2013-04-01 9:00', '2013-04-02 9:00', '2013-04-03 9:00'
             ] * 2, tz='Asia/Tokyo')
        idx2 = pd.date_range('2010/01/01', periods=6, freq='M',
                             tz='US/Eastern')
        idx = MultiIndex.from_arrays([idx1, idx2])

        expected1 = pd.DatetimeIndex(['2013-04-01 9:00', '2013-04-02 9:00',
                                      '2013-04-03 9:00'], tz='Asia/Tokyo')

        tm.assert_index_equal(idx.levels[0], expected1)
        tm.assert_index_equal(idx.levels[1], idx2)

        # from datetime combos
        # GH 7888
        date1 = datetime.date.today()
        date2 = datetime.datetime.today()
        date3 = Timestamp.today()

        for d1, d2 in itertools.product(
                [date1, date2, date3], [date1, date2, date3]):
            index = MultiIndex.from_product([[d1], [d2]])
            assert isinstance(index.levels[0], pd.DatetimeIndex)
            assert isinstance(index.levels[1], pd.DatetimeIndex) 
Example #24
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_reset_index_period(self):
        # GH 7746
        idx = MultiIndex.from_product(
            [pd.period_range('20130101', periods=3, freq='M'), list('abc')],
            names=['month', 'feature'])

        df = DataFrame(np.arange(9, dtype='int64').reshape(-1, 1),
                       index=idx, columns=['a'])
        expected = DataFrame({
            'month': ([pd.Period('2013-01', freq='M')] * 3 +
                      [pd.Period('2013-02', freq='M')] * 3 +
                      [pd.Period('2013-03', freq='M')] * 3),
            'feature': ['a', 'b', 'c'] * 3,
            'a': np.arange(9, dtype='int64')
        }, columns=['month', 'feature', 'a'])
        tm.assert_frame_equal(df.reset_index(), expected) 
Example #25
Source File: scipy_sparse.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _coo_to_sparse_series(A, dense_index=False):
    """ Convert a scipy.sparse.coo_matrix to a SparseSeries.
    Use the defaults given in the SparseSeries constructor.
    """
    s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
    s = s.sort_index()
    s = s.to_sparse()  # TODO: specify kind?
    if dense_index:
        # is there a better constructor method to use here?
        i = range(A.shape[0])
        j = range(A.shape[1])
        ind = MultiIndex.from_product([i, j])
        s = s.reindex(ind)
    return s 
Example #26
Source File: scipy_sparse.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _coo_to_sparse_series(A, dense_index=False):
    """ Convert a scipy.sparse.coo_matrix to a SparseSeries.
    Use the defaults given in the SparseSeries constructor.
    """
    s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
    s = s.sort_index()
    s = s.to_sparse()  # TODO: specify kind?
    if dense_index:
        # is there a better constructor method to use here?
        i = range(A.shape[0])
        j = range(A.shape[1])
        ind = MultiIndex.from_product([i, j])
        s = s.reindex(ind)
    return s 
Example #27
Source File: test_multilevel.py    From coffeegrindsize with MIT License 4 votes vote down vote up
def test_sort_index_and_reconstruction(self):

        # 15622
        # lexsortedness should be identical
        # across MultiIndex consruction methods

        df = DataFrame([[1, 1], [2, 2]], index=list('ab'))
        expected = DataFrame([[1, 1], [2, 2], [1, 1], [2, 2]],
                             index=MultiIndex.from_tuples([(0.5, 'a'),
                                                           (0.5, 'b'),
                                                           (0.8, 'a'),
                                                           (0.8, 'b')]))
        assert expected.index.is_lexsorted()

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex.from_product([[0.5, 0.8], list('ab')]))
        result = result.sort_index()
        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex(levels=[[0.5, 0.8], ['a', 'b']],
                             codes=[[0, 0, 1, 1], [0, 1, 0, 1]]))
        result = result.sort_index()
        assert result.index.is_lexsorted()

        tm.assert_frame_equal(result, expected)

        concatted = pd.concat([df, df], keys=[0.8, 0.5])
        result = concatted.sort_index()

        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        # 14015
        df = DataFrame([[1, 2], [6, 7]],
                       columns=MultiIndex.from_tuples(
                           [(0, '20160811 12:00:00'),
                            (0, '20160809 12:00:00')],
                           names=['l1', 'Date']))

        df.columns.set_levels(pd.to_datetime(df.columns.levels[1]),
                              level=1,
                              inplace=True)
        assert not df.columns.is_lexsorted()
        assert not df.columns.is_monotonic
        result = df.sort_index(axis=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic
        result = df.sort_index(axis=1, level=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic 
Example #28
Source File: test_multilevel.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def test_sort_index_and_reconstruction(self):

        # 15622
        # lexsortedness should be identical
        # across MultiIndex consruction methods

        df = DataFrame([[1, 1], [2, 2]], index=list('ab'))
        expected = DataFrame([[1, 1], [2, 2], [1, 1], [2, 2]],
                             index=MultiIndex.from_tuples([(0.5, 'a'),
                                                           (0.5, 'b'),
                                                           (0.8, 'a'),
                                                           (0.8, 'b')]))
        assert expected.index.is_lexsorted()

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex.from_product([[0.5, 0.8], list('ab')]))
        result = result.sort_index()
        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex(levels=[[0.5, 0.8], ['a', 'b']],
                             labels=[[0, 0, 1, 1], [0, 1, 0, 1]]))
        result = result.sort_index()
        assert result.index.is_lexsorted()

        tm.assert_frame_equal(result, expected)

        concatted = pd.concat([df, df], keys=[0.8, 0.5])
        result = concatted.sort_index()

        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        # 14015
        df = DataFrame([[1, 2], [6, 7]],
                       columns=MultiIndex.from_tuples(
                           [(0, '20160811 12:00:00'),
                            (0, '20160809 12:00:00')],
                           names=['l1', 'Date']))

        df.columns.set_levels(pd.to_datetime(df.columns.levels[1]),
                              level=1,
                              inplace=True)
        assert not df.columns.is_lexsorted()
        assert not df.columns.is_monotonic
        result = df.sort_index(axis=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic
        result = df.sort_index(axis=1, level=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic 
Example #29
Source File: test_multilevel.py    From recruit with Apache License 2.0 4 votes vote down vote up
def test_sort_index_and_reconstruction(self):

        # 15622
        # lexsortedness should be identical
        # across MultiIndex consruction methods

        df = DataFrame([[1, 1], [2, 2]], index=list('ab'))
        expected = DataFrame([[1, 1], [2, 2], [1, 1], [2, 2]],
                             index=MultiIndex.from_tuples([(0.5, 'a'),
                                                           (0.5, 'b'),
                                                           (0.8, 'a'),
                                                           (0.8, 'b')]))
        assert expected.index.is_lexsorted()

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex.from_product([[0.5, 0.8], list('ab')]))
        result = result.sort_index()
        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex(levels=[[0.5, 0.8], ['a', 'b']],
                             codes=[[0, 0, 1, 1], [0, 1, 0, 1]]))
        result = result.sort_index()
        assert result.index.is_lexsorted()

        tm.assert_frame_equal(result, expected)

        concatted = pd.concat([df, df], keys=[0.8, 0.5])
        result = concatted.sort_index()

        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        # 14015
        df = DataFrame([[1, 2], [6, 7]],
                       columns=MultiIndex.from_tuples(
                           [(0, '20160811 12:00:00'),
                            (0, '20160809 12:00:00')],
                           names=['l1', 'Date']))

        df.columns.set_levels(pd.to_datetime(df.columns.levels[1]),
                              level=1,
                              inplace=True)
        assert not df.columns.is_lexsorted()
        assert not df.columns.is_monotonic
        result = df.sort_index(axis=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic
        result = df.sort_index(axis=1, level=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic 
Example #30
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 4 votes vote down vote up
def test_sort_index_and_reconstruction(self):

        # 15622
        # lexsortedness should be identical
        # across MultiIndex consruction methods

        df = DataFrame([[1, 1], [2, 2]], index=list('ab'))
        expected = DataFrame([[1, 1], [2, 2], [1, 1], [2, 2]],
                             index=MultiIndex.from_tuples([(0.5, 'a'),
                                                           (0.5, 'b'),
                                                           (0.8, 'a'),
                                                           (0.8, 'b')]))
        assert expected.index.is_lexsorted()

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex.from_product([[0.5, 0.8], list('ab')]))
        result = result.sort_index()
        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        result = DataFrame(
            [[1, 1], [2, 2], [1, 1], [2, 2]],
            index=MultiIndex(levels=[[0.5, 0.8], ['a', 'b']],
                             labels=[[0, 0, 1, 1], [0, 1, 0, 1]]))
        result = result.sort_index()
        assert result.index.is_lexsorted()

        tm.assert_frame_equal(result, expected)

        concatted = pd.concat([df, df], keys=[0.8, 0.5])
        result = concatted.sort_index()

        assert result.index.is_lexsorted()
        assert result.index.is_monotonic

        tm.assert_frame_equal(result, expected)

        # 14015
        df = DataFrame([[1, 2], [6, 7]],
                       columns=MultiIndex.from_tuples(
                           [(0, '20160811 12:00:00'),
                            (0, '20160809 12:00:00')],
                           names=['l1', 'Date']))

        df.columns.set_levels(pd.to_datetime(df.columns.levels[1]),
                              level=1,
                              inplace=True)
        assert not df.columns.is_lexsorted()
        assert not df.columns.is_monotonic
        result = df.sort_index(axis=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic
        result = df.sort_index(axis=1, level=1)
        assert result.columns.is_lexsorted()
        assert result.columns.is_monotonic