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

The following are 30 code examples of pandas.core.index.MultiIndex.from_arrays(). 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: reshape.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def pivot(self, index=None, columns=None, values=None):
    """
    See DataFrame.pivot
    """
    if values is None:
        cols = [columns] if index is None else [index, columns]
        append = index is None
        indexed = self.set_index(cols, append=append)
        return indexed.unstack(columns)
    else:
        if index is None:
            index = self.index
        else:
            index = self[index]
        indexed = Series(self[values].values,
                         index=MultiIndex.from_arrays([index, self[columns]]))
        return indexed.unstack(columns) 
Example #2
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_std_var_pass_ddof(self):
        index = MultiIndex.from_arrays([np.arange(5).repeat(10), np.tile(
            np.arange(10), 5)])
        df = DataFrame(np.random.randn(len(index), 5), index=index)

        for meth in ['var', 'std']:
            ddof = 4
            alt = lambda x: getattr(x, meth)(ddof=ddof)

            result = getattr(df[0], meth)(level=0, ddof=ddof)
            expected = df[0].groupby(level=0).agg(alt)
            tm.assert_series_equal(result, expected)

            result = getattr(df, meth)(level=0, ddof=ddof)
            expected = df.groupby(level=0).agg(alt)
            tm.assert_frame_equal(result, expected) 
Example #3
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 #4
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_sort_index_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #5
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 #6
Source File: test_multilevel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_sort_index_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #7
Source File: test_analytics.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_count(self, datetime_series):
        assert datetime_series.count() == len(datetime_series)

        datetime_series[::2] = np.NaN

        assert datetime_series.count() == np.isfinite(datetime_series).sum()

        mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]])
        ts = Series(np.arange(len(mi)), index=mi)

        left = ts.count(level=1)
        right = Series([2, 3, 1], index=[1, 2, nan])
        assert_series_equal(left, right)

        ts.iloc[[0, 3, 5]] = nan
        assert_series_equal(ts.count(level=1), right - 1) 
Example #8
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_duplicated_drop_duplicates(self):
        # GH 4060
        idx = MultiIndex.from_arrays(([1, 2, 3, 1, 2, 3], [1, 1, 1, 1, 2, 2]))

        expected = np.array(
            [False, False, False, True, False, False], dtype=bool)
        duplicated = idx.duplicated()
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([1, 2, 3, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(), expected)

        expected = np.array([True, False, False, False, False, False])
        duplicated = idx.duplicated(keep='last')
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 1, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep='last'), expected)

        expected = np.array([True, False, False, True, False, False])
        duplicated = idx.duplicated(keep=False)
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 2, 3], [1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep=False), expected) 
Example #9
Source File: reshape.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def pivot(self, index=None, columns=None, values=None):
    """
    See DataFrame.pivot
    """
    if values is None:
        cols = [columns] if index is None else [index, columns]
        append = index is None
        indexed = self.set_index(cols, append=append)
    else:
        if index is None:
            index = self.index
        else:
            index = self[index]
        index = MultiIndex.from_arrays([index, self[columns]])

        if is_list_like(values) and not isinstance(values, tuple):
            # Exclude tuple because it is seen as a single column name
            indexed = self._constructor(self[values].values, index=index,
                                        columns=values)
        else:
            indexed = self._constructor_sliced(self[values].values,
                                               index=index)
    return indexed.unstack(columns) 
Example #10
Source File: groupby.py    From Computable with MIT License 6 votes vote down vote up
def _index_with_as_index(self, b):
        """
        Take boolean mask of index to be returned from apply, if as_index=True

        """
        # TODO perf, it feels like this should already be somewhere...
        from itertools import chain
        original = self.obj.index
        gp = self.grouper
        levels = chain((gp.levels[i][gp.labels[i][b]]
                        for i in range(len(gp.groupings))),
                       (original.get_level_values(i)[b]
                        for i in range(original.nlevels)))
        new = MultiIndex.from_arrays(list(levels))
        new.names = gp.names + original.names
        return new 
Example #11
Source File: parsers.py    From Computable with MIT License 6 votes vote down vote up
def _agg_index(self, index, try_parse_dates=True):
        arrays = []
        for i, arr in enumerate(index):

            if (try_parse_dates and self._should_parse_dates(i)):
                arr = self._date_conv(arr)

            col_na_values = self.na_values
            col_na_fvalues = self.na_fvalues

            if isinstance(self.na_values, dict):
                col_name = self.index_names[i]
                if col_name is not None:
                    col_na_values, col_na_fvalues = _get_na_values(
                        col_name, self.na_values, self.na_fvalues)

            arr, _ = self._convert_types(arr, col_na_values | col_na_fvalues)
            arrays.append(arr)

        index = MultiIndex.from_arrays(arrays, names=self.index_names)

        return index 
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_std_var_pass_ddof(self):
        index = MultiIndex.from_arrays([np.arange(5).repeat(10), np.tile(
            np.arange(10), 5)])
        df = DataFrame(np.random.randn(len(index), 5), index=index)

        for meth in ['var', 'std']:
            ddof = 4
            alt = lambda x: getattr(x, meth)(ddof=ddof)

            result = getattr(df[0], meth)(level=0, ddof=ddof)
            expected = df[0].groupby(level=0).agg(alt)
            tm.assert_series_equal(result, expected)

            result = getattr(df, meth)(level=0, ddof=ddof)
            expected = df.groupby(level=0).agg(alt)
            tm.assert_frame_equal(result, expected) 
Example #13
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 #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_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #15
Source File: pivot.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def pivot(data, index=None, columns=None, values=None):
    if values is None:
        cols = [columns] if index is None else [index, columns]
        append = index is None
        indexed = data.set_index(cols, append=append)
    else:
        if index is None:
            index = data.index
        else:
            index = data[index]
        index = MultiIndex.from_arrays([index, data[columns]])

        if is_list_like(values) and not isinstance(values, tuple):
            # Exclude tuple because it is seen as a single column name
            indexed = data._constructor(data[values].values, index=index,
                                        columns=values)
        else:
            indexed = data._constructor_sliced(data[values].values,
                                               index=index)
    return indexed.unstack(columns) 
Example #16
Source File: test_analytics.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_count(self, datetime_series):
        assert datetime_series.count() == len(datetime_series)

        datetime_series[::2] = np.NaN

        assert datetime_series.count() == np.isfinite(datetime_series).sum()

        mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]])
        ts = Series(np.arange(len(mi)), index=mi)

        left = ts.count(level=1)
        right = Series([2, 3, 1], index=[1, 2, nan])
        assert_series_equal(left, right)

        ts.iloc[[0, 3, 5]] = nan
        assert_series_equal(ts.count(level=1), right - 1) 
Example #17
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_std_var_pass_ddof(self):
        index = MultiIndex.from_arrays([np.arange(5).repeat(10), np.tile(
            np.arange(10), 5)])
        df = DataFrame(np.random.randn(len(index), 5), index=index)

        for meth in ['var', 'std']:
            ddof = 4
            alt = lambda x: getattr(x, meth)(ddof=ddof)

            result = getattr(df[0], meth)(level=0, ddof=ddof)
            expected = df[0].groupby(level=0).agg(alt)
            tm.assert_series_equal(result, expected)

            result = getattr(df, meth)(level=0, ddof=ddof)
            expected = df.groupby(level=0).agg(alt)
            tm.assert_frame_equal(result, expected) 
Example #18
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_duplicated_drop_duplicates(self):
        # GH 4060
        idx = MultiIndex.from_arrays(([1, 2, 3, 1, 2, 3], [1, 1, 1, 1, 2, 2]))

        expected = np.array(
            [False, False, False, True, False, False], dtype=bool)
        duplicated = idx.duplicated()
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([1, 2, 3, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(), expected)

        expected = np.array([True, False, False, False, False, False])
        duplicated = idx.duplicated(keep='last')
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 1, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep='last'), expected)

        expected = np.array([True, False, False, True, False, False])
        duplicated = idx.duplicated(keep=False)
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 2, 3], [1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep=False), expected) 
Example #19
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 #20
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_sort_index_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #21
Source File: test_analytics.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_count(self):
        assert self.ts.count() == len(self.ts)

        self.ts[::2] = np.NaN

        assert self.ts.count() == np.isfinite(self.ts).sum()

        mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]])
        ts = Series(np.arange(len(mi)), index=mi)

        left = ts.count(level=1)
        right = Series([2, 3, 1], index=[1, 2, nan])
        assert_series_equal(left, right)

        ts.iloc[[0, 3, 5]] = nan
        assert_series_equal(ts.count(level=1), right - 1) 
Example #22
Source File: reshape.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def pivot(self, index=None, columns=None, values=None):
    """
    See DataFrame.pivot
    """
    if values is None:
        cols = [columns] if index is None else [index, columns]
        append = index is None
        indexed = self.set_index(cols, append=append)
        return indexed.unstack(columns)
    else:
        if index is None:
            index = self.index
        else:
            index = self[index]
        indexed = Series(self[values].values,
                         index=MultiIndex.from_arrays([index, self[columns]]))
        return indexed.unstack(columns) 
Example #23
Source File: test_multilevel.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_std_var_pass_ddof(self):
        index = MultiIndex.from_arrays([np.arange(5).repeat(10), np.tile(
            np.arange(10), 5)])
        df = DataFrame(np.random.randn(len(index), 5), index=index)

        for meth in ['var', 'std']:
            ddof = 4
            alt = lambda x: getattr(x, meth)(ddof=ddof)

            result = getattr(df[0], meth)(level=0, ddof=ddof)
            expected = df[0].groupby(level=0).agg(alt)
            tm.assert_series_equal(result, expected)

            result = getattr(df, meth)(level=0, ddof=ddof)
            expected = df.groupby(level=0).agg(alt)
            tm.assert_frame_equal(result, expected) 
Example #24
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 #25
Source File: test_multilevel.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_sort_index_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #26
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_std_var_pass_ddof(self):
        index = MultiIndex.from_arrays([np.arange(5).repeat(10), np.tile(
            np.arange(10), 5)])
        df = DataFrame(np.random.randn(len(index), 5), index=index)

        for meth in ['var', 'std']:
            ddof = 4
            alt = lambda x: getattr(x, meth)(ddof=ddof)

            result = getattr(df[0], meth)(level=0, ddof=ddof)
            expected = df[0].groupby(level=0).agg(alt)
            tm.assert_series_equal(result, expected)

            result = getattr(df, meth)(level=0, ddof=ddof)
            expected = df.groupby(level=0).agg(alt)
            tm.assert_frame_equal(result, expected) 
Example #27
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_duplicated_drop_duplicates(self):
        # GH 4060
        idx = MultiIndex.from_arrays(([1, 2, 3, 1, 2, 3], [1, 1, 1, 1, 2, 2]))

        expected = np.array(
            [False, False, False, True, False, False], dtype=bool)
        duplicated = idx.duplicated()
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([1, 2, 3, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(), expected)

        expected = np.array([True, False, False, False, False, False])
        duplicated = idx.duplicated(keep='last')
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 1, 2, 3], [1, 1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep='last'), expected)

        expected = np.array([True, False, False, True, False, False])
        duplicated = idx.duplicated(keep=False)
        tm.assert_numpy_array_equal(duplicated, expected)
        assert duplicated.dtype == bool
        expected = MultiIndex.from_arrays(([2, 3, 2, 3], [1, 1, 2, 2]))
        tm.assert_index_equal(idx.drop_duplicates(keep=False), expected) 
Example #28
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 #29
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_sort_index_level_large_cardinality(self):

        # #2684 (int64)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int64)

        # it works!
        result = df.sort_index(level=0)
        assert result.index.lexsort_depth == 3

        # #2684 (int32)
        index = MultiIndex.from_arrays([np.arange(4000)] * 3)
        df = DataFrame(np.random.randn(4000), index=index, dtype=np.int32)

        # it works!
        result = df.sort_index(level=0)
        assert (result.dtypes.values == df.dtypes.values).all()
        assert result.index.lexsort_depth == 3 
Example #30
Source File: test_analytics.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_count(self):
        assert self.ts.count() == len(self.ts)

        self.ts[::2] = np.NaN

        assert self.ts.count() == np.isfinite(self.ts).sum()

        mi = MultiIndex.from_arrays([list('aabbcc'), [1, 2, 2, nan, 1, 2]])
        ts = Series(np.arange(len(mi)), index=mi)

        left = ts.count(level=1)
        right = Series([2, 3, 1], index=[1, 2, nan])
        assert_series_equal(left, right)

        ts.iloc[[0, 3, 5]] = nan
        assert_series_equal(ts.count(level=1), right - 1)