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

The following are 30 code examples of pandas.core.index.MultiIndex.from_tuples(). 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 recruit with Apache License 2.0 6 votes vote down vote up
def test_mixed_depth_pop(self):
        arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
                  ['', 'OD', 'OD', 'result1', 'result2', 'result1'],
                  ['', 'wx', 'wy', '', '', '']]

        tuples = sorted(zip(*arrays))
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(randn(4, 6), columns=index)

        df1 = df.copy()
        df2 = df.copy()
        result = df1.pop('a')
        expected = df2.pop(('a', '', ''))
        tm.assert_series_equal(expected, result, check_names=False)
        tm.assert_frame_equal(df1, df2)
        assert result.name == 'a'

        expected = df1['top']
        df1 = df1.drop(['top'], axis=1)
        result = df2.pop('top')
        tm.assert_frame_equal(expected, result)
        tm.assert_frame_equal(df1, df2) 
Example #2
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_alignment(self):
        x = Series(data=[1, 2, 3], index=MultiIndex.from_tuples([("A", 1), (
            "A", 2), ("B", 3)]))

        y = Series(data=[4, 5, 6], index=MultiIndex.from_tuples([("Z", 1), (
            "Z", 2), ("B", 3)]))

        res = x - y
        exp_index = x.index.union(y.index)
        exp = x.reindex(exp_index) - y.reindex(exp_index)
        tm.assert_series_equal(res, exp)

        # hit non-monotonic code path
        res = x[::-1] - y[::-1]
        exp_index = x.index.union(y.index)
        exp = x.reindex(exp_index) - y.reindex(exp_index)
        tm.assert_series_equal(res, exp) 
Example #3
Source File: test_analytics.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_sort_index_level(self):
        mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
        s = Series([1, 2], mi)
        backwards = s.iloc[[1, 0]]

        res = s.sort_index(level='A')
        assert_series_equal(backwards, res)

        res = s.sort_index(level=['A', 'B'])
        assert_series_equal(backwards, res)

        res = s.sort_index(level='A', sort_remaining=False)
        assert_series_equal(s, res)

        res = s.sort_index(level=['A', 'B'], sort_remaining=False)
        assert_series_equal(s, res) 
Example #4
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_sort_ascending_list(self):
        # GH: 16934

        # Set up a Series with a three level MultiIndex
        arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
                  [4, 3, 2, 1, 4, 3, 2, 1]]
        tuples = lzip(*arrays)
        mi = MultiIndex.from_tuples(tuples, names=['first', 'second', 'third'])
        s = Series(range(8), index=mi)

        # Sort with boolean ascending
        result = s.sort_index(level=['third', 'first'], ascending=False)
        expected = s.iloc[[4, 0, 5, 1, 6, 2, 7, 3]]
        tm.assert_series_equal(result, expected)

        # Sort with list of boolean ascending
        result = s.sort_index(level=['third', 'first'],
                              ascending=[False, True])
        expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
        tm.assert_series_equal(result, expected) 
Example #5
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_mixed_depth_get(self, unicode_strings):
        # If unicode_strings is True, the column labels in dataframe
        # construction will use unicode strings in Python 2 (pull request
        # #17099).

        arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
                  ['', 'OD', 'OD', 'result1', 'result2', 'result1'],
                  ['', 'wx', 'wy', '', '', '']]

        if unicode_strings:
            arrays = [[u(s) for s in arr] for arr in arrays]

        tuples = sorted(zip(*arrays))
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(np.random.randn(4, 6), columns=index)

        result = df['a']
        expected = df['a', '', ''].rename('a')
        tm.assert_series_equal(result, expected)

        result = df['routine1', 'result1']
        expected = df['routine1', 'result1', '']
        expected = expected.rename(('routine1', 'result1'))
        tm.assert_series_equal(result, expected) 
Example #6
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_mixed_depth_pop(self):
        arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
                  ['', 'OD', 'OD', 'result1', 'result2', 'result1'],
                  ['', 'wx', 'wy', '', '', '']]

        tuples = sorted(zip(*arrays))
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(randn(4, 6), columns=index)

        df1 = df.copy()
        df2 = df.copy()
        result = df1.pop('a')
        expected = df2.pop(('a', '', ''))
        tm.assert_series_equal(expected, result, check_names=False)
        tm.assert_frame_equal(df1, df2)
        assert result.name == 'a'

        expected = df1['top']
        df1 = df1.drop(['top'], axis=1)
        result = df2.pop('top')
        tm.assert_frame_equal(expected, result)
        tm.assert_frame_equal(df1, df2) 
Example #7
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_xs_level(self):
        result = self.frame.xs('two', level='second')
        expected = self.frame[self.frame.index.get_level_values(1) == 'two']
        expected.index = expected.index.droplevel(1)

        tm.assert_frame_equal(result, expected)

        index = MultiIndex.from_tuples([('x', 'y', 'z'), ('a', 'b', 'c'), (
            'p', 'q', 'r')])
        df = DataFrame(np.random.randn(3, 5), index=index)
        result = df.xs('c', level=2)
        expected = df[1:2]
        expected.index = expected.index.droplevel(2)
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = self.frame.xs('two', level='second')

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result) 
Example #8
Source File: test_multilevel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_sort_ascending_list(self):
        # GH: 16934

        # Set up a Series with a three level MultiIndex
        arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
                  [4, 3, 2, 1, 4, 3, 2, 1]]
        tuples = lzip(*arrays)
        mi = MultiIndex.from_tuples(tuples, names=['first', 'second', 'third'])
        s = Series(range(8), index=mi)

        # Sort with boolean ascending
        result = s.sort_index(level=['third', 'first'], ascending=False)
        expected = s.iloc[[4, 0, 5, 1, 6, 2, 7, 3]]
        tm.assert_series_equal(result, expected)

        # Sort with list of boolean ascending
        result = s.sort_index(level=['third', 'first'],
                              ascending=[False, True])
        expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
        tm.assert_series_equal(result, expected) 
Example #9
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_mixed_depth_pop(self):
        arrays = [['a', 'top', 'top', 'routine1', 'routine1', 'routine2'],
                  ['', 'OD', 'OD', 'result1', 'result2', 'result1'],
                  ['', 'wx', 'wy', '', '', '']]

        tuples = sorted(zip(*arrays))
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(randn(4, 6), columns=index)

        df1 = df.copy()
        df2 = df.copy()
        result = df1.pop('a')
        expected = df2.pop(('a', '', ''))
        tm.assert_series_equal(expected, result, check_names=False)
        tm.assert_frame_equal(df1, df2)
        assert result.name == 'a'

        expected = df1['top']
        df1 = df1.drop(['top'], axis=1)
        result = df2.pop('top')
        tm.assert_frame_equal(expected, result)
        tm.assert_frame_equal(df1, df2) 
Example #10
Source File: test_analytics.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_sort_index_level(self):
        mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
        s = Series([1, 2], mi)
        backwards = s.iloc[[1, 0]]

        res = s.sort_index(level='A')
        assert_series_equal(backwards, res)

        res = s.sort_index(level=['A', 'B'])
        assert_series_equal(backwards, res)

        res = s.sort_index(level='A', sort_remaining=False)
        assert_series_equal(s, res)

        res = s.sort_index(level=['A', 'B'], sort_remaining=False)
        assert_series_equal(s, res) 
Example #11
Source File: managers.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _transform_index(index, func, level=None):
    """
    Apply function to all values found in index.

    This includes transforming multiindex entries separately.
    Only apply function to one level of the MultiIndex if level is specified.

    """
    if isinstance(index, MultiIndex):
        if level is not None:
            items = [tuple(func(y) if i == level else y
                           for i, y in enumerate(x)) for x in index]
        else:
            items = [tuple(func(y) for y in x) for x in index]
        return MultiIndex.from_tuples(items, names=index.names)
    else:
        items = [func(x) for x in index]
        return Index(items, name=index.name, tupleize_cols=False) 
Example #12
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_ascending_list(self):
        # GH: 16934

        # Set up a Series with a three level MultiIndex
        arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
                  [4, 3, 2, 1, 4, 3, 2, 1]]
        tuples = lzip(*arrays)
        mi = MultiIndex.from_tuples(tuples, names=['first', 'second', 'third'])
        s = Series(range(8), index=mi)

        # Sort with boolean ascending
        result = s.sort_index(level=['third', 'first'], ascending=False)
        expected = s.iloc[[4, 0, 5, 1, 6, 2, 7, 3]]
        tm.assert_series_equal(result, expected)

        # Sort with list of boolean ascending
        result = s.sort_index(level=['third', 'first'],
                              ascending=[False, True])
        expected = s.iloc[[0, 4, 1, 5, 2, 6, 3, 7]]
        tm.assert_series_equal(result, expected) 
Example #13
Source File: test_analytics.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_sort_index_level(self):
        mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
        s = Series([1, 2], mi)
        backwards = s.iloc[[1, 0]]

        res = s.sort_index(level='A')
        assert_series_equal(backwards, res)

        res = s.sort_index(level=['A', 'B'])
        assert_series_equal(backwards, res)

        res = s.sort_index(level='A', sort_remaining=False)
        assert_series_equal(s, res)

        res = s.sort_index(level=['A', 'B'], sort_remaining=False)
        assert_series_equal(s, res) 
Example #14
Source File: managers.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def _transform_index(index, func, level=None):
    """
    Apply function to all values found in index.

    This includes transforming multiindex entries separately.
    Only apply function to one level of the MultiIndex if level is specified.

    """
    if isinstance(index, MultiIndex):
        if level is not None:
            items = [tuple(func(y) if i == level else y
                           for i, y in enumerate(x)) for x in index]
        else:
            items = [tuple(func(y) for y in x) for x in index]
        return MultiIndex.from_tuples(items, names=index.names)
    else:
        items = [func(x) for x in index]
        return Index(items, name=index.name, tupleize_cols=False) 
Example #15
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_iloc_mi(self):
        # GH 13797
        # Test if iloc can handle integer locations in MultiIndexed DataFrame

        data = [['str00', 'str01'], ['str10', 'str11'], ['str20', 'srt21'],
                ['str30', 'str31'], ['str40', 'str41']]

        mi = MultiIndex.from_tuples(
            [('CC', 'A'), ('CC', 'B'), ('CC', 'B'), ('BB', 'a'), ('BB', 'b')])

        expected = DataFrame(data)
        df_mi = DataFrame(data, index=mi)

        result = DataFrame([[df_mi.iloc[r, c] for c in range(2)]
                            for r in range(5)])

        tm.assert_frame_equal(result, expected) 
Example #16
Source File: groupby.py    From Computable with MIT License 6 votes vote down vote up
def _wrap_applied_output(self, keys, values, not_indexed_same=False):
        if len(keys) == 0:
            return Series([])

        def _get_index():
            if self.grouper.nkeys > 1:
                index = MultiIndex.from_tuples(keys, names=self.grouper.names)
            else:
                index = Index(keys, name=self.grouper.names[0])
            return index

        if isinstance(values[0], dict):
            # GH #823
            index = _get_index()
            return DataFrame(values, index=index).stack()

        if isinstance(values[0], (Series, dict)):
            return self._concat_objects(keys, values,
                                        not_indexed_same=not_indexed_same)
        elif isinstance(values[0], DataFrame):
            # possible that Series -> DataFrame by applied function
            return self._concat_objects(keys, values,
                                        not_indexed_same=not_indexed_same)
        else:
            return Series(values, index=_get_index()) 
Example #17
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_duplicate_groupby_issues(self):
        idx_tp = [('600809', '20061231'), ('600809', '20070331'),
                  ('600809', '20070630'), ('600809', '20070331')]
        dt = ['demo', 'demo', 'demo', 'demo']

        idx = MultiIndex.from_tuples(idx_tp, names=['STK_ID', 'RPT_Date'])
        s = Series(dt, index=idx)

        result = s.groupby(s.index).first()
        assert len(result) == 3 
Example #18
Source File: parsers.py    From Computable with MIT License 5 votes vote down vote up
def _maybe_make_multi_index_columns(self, columns, col_names=None):
        # possibly create a column mi here
        if (not self.tupleize_cols and len(columns) and
                not isinstance(columns, MultiIndex) and
                all([isinstance(c, tuple) for c in columns])):
            columns = MultiIndex.from_tuples(columns, names=col_names)
        return columns 
Example #19
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_frame_dict_constructor_empty_series(self):
        s1 = Series([
            1, 2, 3, 4
        ], index=MultiIndex.from_tuples([(1, 2), (1, 3), (2, 2), (2, 4)]))
        s2 = Series([
            1, 2, 3, 4
        ], index=MultiIndex.from_tuples([(1, 2), (1, 3), (3, 2), (3, 4)]))
        s3 = Series()

        # it works!
        DataFrame({'foo': s1, 'bar': s2, 'baz': s3})
        DataFrame.from_dict({'foo': s1, 'baz': s3, 'bar': s2}) 
Example #20
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_repeat(self):
        # GH 9361
        # fixed by # GH 7891
        m_idx = MultiIndex.from_tuples([(1, 2), (3, 4), (5, 6), (7, 8)])
        data = ['a', 'b', 'c', 'd']
        m_df = Series(data, index=m_idx)
        assert m_df.repeat(3).shape == (3 * len(data), ) 
Example #21
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_multiindex_set_index(self):
        # segfault in #3308
        d = {'t1': [2, 2.5, 3], 't2': [4, 5, 6]}
        df = DataFrame(d)
        tuples = [(0, 1), (0, 2), (1, 2)]
        df['tuples'] = tuples

        index = MultiIndex.from_tuples(df['tuples'])
        # it works!
        df.set_index(index) 
Example #22
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_sort_non_lexsorted(self):
        # degenerate case where we sort but don't
        # have a satisfying result :<
        # GH 15797
        idx = MultiIndex([['A', 'B', 'C'],
                          ['c', 'b', 'a']],
                         [[0, 1, 2, 0, 1, 2],
                          [0, 2, 1, 1, 0, 2]])

        df = DataFrame({'col': range(len(idx))},
                       index=idx,
                       dtype='int64')
        assert df.index.is_lexsorted() is False
        assert df.index.is_monotonic is False

        sorted = df.sort_index()
        assert sorted.index.is_lexsorted() is True
        assert sorted.index.is_monotonic is True

        expected = DataFrame(
            {'col': [1, 4, 5, 2]},
            index=MultiIndex.from_tuples([('B', 'a'), ('B', 'c'),
                                          ('C', 'a'), ('C', 'b')]),
            dtype='int64')
        result = sorted.loc[pd.IndexSlice['B':'C', 'a':'c'], :]
        tm.assert_frame_equal(result, expected) 
Example #23
Source File: test_multilevel.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_groupby_level_no_obs(self):
        # #1697
        midx = MultiIndex.from_tuples([('f1', 's1'), ('f1', 's2'), (
            'f2', 's1'), ('f2', 's2'), ('f3', 's1'), ('f3', 's2')])
        df = DataFrame(
            [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]], columns=midx)
        df1 = df.loc(axis=1)[df.columns.map(
            lambda u: u[0] in ['f2', 'f3'])]

        grouped = df1.groupby(axis=1, level=0)
        result = grouped.sum()
        assert (result.columns == ['f2', 'f3']).all() 
Example #24
Source File: groupby.py    From Computable with MIT License 5 votes vote down vote up
def _decide_output_index(self, output, labels):
        if len(output) == len(labels):
            output_keys = labels
        else:
            output_keys = sorted(output)
            try:
                output_keys.sort()
            except Exception:  # pragma: no cover
                pass

            if isinstance(labels, MultiIndex):
                output_keys = MultiIndex.from_tuples(output_keys,
                                                     names=labels.names)

        return output_keys 
Example #25
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_sort_non_lexsorted(self):
        # degenerate case where we sort but don't
        # have a satisfying result :<
        # GH 15797
        idx = MultiIndex([['A', 'B', 'C'],
                          ['c', 'b', 'a']],
                         [[0, 1, 2, 0, 1, 2],
                          [0, 2, 1, 1, 0, 2]])

        df = DataFrame({'col': range(len(idx))},
                       index=idx,
                       dtype='int64')
        assert df.index.is_lexsorted() is False
        assert df.index.is_monotonic is False

        sorted = df.sort_index()
        assert sorted.index.is_lexsorted() is True
        assert sorted.index.is_monotonic is True

        expected = DataFrame(
            {'col': [1, 4, 5, 2]},
            index=MultiIndex.from_tuples([('B', 'a'), ('B', 'c'),
                                          ('C', 'a'), ('C', 'b')]),
            dtype='int64')
        result = sorted.loc[pd.IndexSlice['B':'C', 'a':'c'], :]
        tm.assert_frame_equal(result, expected) 
Example #26
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_series_getitem_not_sorted(self):
        arrays = [['bar', 'bar', 'baz', 'baz', 'qux', 'qux', 'foo', 'foo'],
                  ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
        tuples = lzip(*arrays)
        index = MultiIndex.from_tuples(tuples)
        s = Series(randn(8), index=index)

        arrays = [np.array(x) for x in zip(*index.values)]

        result = s['qux']
        result2 = s.loc['qux']
        expected = s[arrays[0] == 'qux']
        expected.index = expected.index.droplevel(0)
        tm.assert_series_equal(result, expected)
        tm.assert_series_equal(result2, expected) 
Example #27
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_getitem_multilevel_index_tuple_not_sorted(self):
        index_columns = list("abc")
        df = DataFrame([[0, 1, 0, "x"], [0, 0, 1, "y"]],
                       columns=index_columns + ["data"])
        df = df.set_index(index_columns)
        query_index = df.index[:1]
        rs = df.loc[query_index, "data"]

        xp_idx = MultiIndex.from_tuples([(0, 1, 0)], names=['a', 'b', 'c'])
        xp = Series(['x'], index=xp_idx, name='data')
        tm.assert_series_equal(rs, xp) 
Example #28
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_sorting_repr_8017(self):

        np.random.seed(0)
        data = np.random.randn(3, 4)

        for gen, extra in [([1., 3., 2., 5.], 4.), ([1, 3, 2, 5], 4),
                           ([Timestamp('20130101'), Timestamp('20130103'),
                             Timestamp('20130102'), Timestamp('20130105')],
                            Timestamp('20130104')),
                           (['1one', '3one', '2one', '5one'], '4one')]:
            columns = MultiIndex.from_tuples([('red', i) for i in gen])
            df = DataFrame(data, index=list('def'), columns=columns)
            df2 = pd.concat([df,
                             DataFrame('world', index=list('def'),
                                       columns=MultiIndex.from_tuples(
                                           [('red', extra)]))], axis=1)

            # check that the repr is good
            # make sure that we have a correct sparsified repr
            # e.g. only 1 header of read
            assert str(df2).splitlines()[0].split() == ['red']

            # GH 8017
            # sorting fails after columns added

            # construct single-dtype then sort
            result = df.copy().sort_index(axis=1)
            expected = df.iloc[:, [0, 2, 1, 3]]
            tm.assert_frame_equal(result, expected)

            result = df2.sort_index(axis=1)
            expected = df2.iloc[:, [0, 2, 1, 4, 3]]
            tm.assert_frame_equal(result, expected)

            # setitem then sort
            result = df.copy()
            result[('red', extra)] = 'world'

            result = result.sort_index(axis=1)
            tm.assert_frame_equal(result, expected) 
Example #29
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_repeat(self):
        # GH 9361
        # fixed by # GH 7891
        m_idx = MultiIndex.from_tuples([(1, 2), (3, 4), (5, 6), (7, 8)])
        data = ['a', 'b', 'c', 'd']
        m_df = Series(data, index=m_idx)
        assert m_df.repeat(3).shape == (3 * len(data), ) 
Example #30
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_multiindex_set_index(self):
        # segfault in #3308
        d = {'t1': [2, 2.5, 3], 't2': [4, 5, 6]}
        df = DataFrame(d)
        tuples = [(0, 1), (0, 2), (1, 2)]
        df['tuples'] = tuples

        index = MultiIndex.from_tuples(df['tuples'])
        # it works!
        df.set_index(index)