Python pandas.compat.lrange() Examples

The following are 30 code examples of pandas.compat.lrange(). 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.compat , or try the search function .
Example #1
Source File: test_drop.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_droplevel_list():
    index = MultiIndex(
        levels=[Index(lrange(4)), Index(lrange(4)), Index(lrange(4))],
        codes=[np.array([0, 0, 1, 2, 2, 2, 3, 3]), np.array(
            [0, 1, 0, 0, 0, 1, 0, 1]), np.array([1, 0, 1, 1, 0, 0, 1, 0])],
        names=['one', 'two', 'three'])

    dropped = index[:2].droplevel(['three', 'one'])
    expected = index[:2].droplevel(2).droplevel(0)
    assert dropped.equals(expected)

    dropped = index[:2].droplevel([])
    expected = index[:2]
    assert dropped.equals(expected)

    with pytest.raises(ValueError):
        index[:2].droplevel(['one', 'two', 'three'])

    with pytest.raises(KeyError):
        index[:2].droplevel(['one', 'four']) 
Example #2
Source File: test_internals.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_take(self):
        def assert_take_ok(mgr, axis, indexer):
            mat = mgr.as_array()
            taken = mgr.take(indexer, axis)
            tm.assert_numpy_array_equal(np.take(mat, indexer, axis),
                                        taken.as_array(), check_dtype=False)
            tm.assert_index_equal(mgr.axes[axis].take(indexer),
                                  taken.axes[axis])

        for mgr in self.MANAGERS:
            for ax in range(mgr.ndim):
                # take/fancy indexer
                assert_take_ok(mgr, ax, [])
                assert_take_ok(mgr, ax, [0, 0, 0])
                assert_take_ok(mgr, ax, lrange(mgr.shape[ax]))

                if mgr.shape[ax] >= 3:
                    assert_take_ok(mgr, ax, [0, 1, 2])
                    assert_take_ok(mgr, ax, [-1, -2, -3]) 
Example #3
Source File: test_algos.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_pad(self):
        old = Index([1, 5, 10])
        new = Index(lrange(12))

        filler = libalgos.pad["int64_t"](old.values, new.values)

        expect_filler = np.array([-1, 0, 0, 0, 0, 1,
                                  1, 1, 1, 1, 2, 2], dtype=np.int64)
        tm.assert_numpy_array_equal(filler, expect_filler)

        # corner case
        old = Index([5, 10])
        new = Index(lrange(5))
        filler = libalgos.pad["int64_t"](old.values, new.values)
        expect_filler = np.array([-1, -1, -1, -1, -1], dtype=np.int64)
        tm.assert_numpy_array_equal(filler, expect_filler) 
Example #4
Source File: test_algos.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_backfill(self):
        old = Index([1, 5, 10])
        new = Index(lrange(12))

        filler = libalgos.backfill["int64_t"](old.values, new.values)

        expect_filler = np.array([0, 0, 1, 1, 1, 1,
                                  2, 2, 2, 2, 2, -1], dtype=np.int64)
        tm.assert_numpy_array_equal(filler, expect_filler)

        # corner case
        old = Index([1, 4])
        new = Index(lrange(5, 10))
        filler = libalgos.backfill["int64_t"](old.values, new.values)

        expect_filler = np.array([-1, -1, -1, -1, -1], dtype=np.int64)
        tm.assert_numpy_array_equal(filler, expect_filler) 
Example #5
Source File: _converter.py    From recruit with Apache License 2.0 6 votes vote down vote up
def __call__(self):
        'Return the locations of the ticks.'
        # axis calls Locator.set_axis inside set_m<xxxx>_formatter
        _check_implicitly_registered()

        vi = tuple(self.axis.get_view_interval())
        if vi != self.plot_obj.view_interval:
            self.plot_obj.date_axis_info = None
        self.plot_obj.view_interval = vi
        vmin, vmax = vi
        if vmax < vmin:
            vmin, vmax = vmax, vmin
        if self.isdynamic:
            locs = self._get_default_locs(vmin, vmax)
        else:  # pragma: no cover
            base = self.base
            (d, m) = divmod(vmin, base)
            vmin = (d + 1) * base
            locs = lrange(vmin, vmax + 1, base)
        return locs 
Example #6
Source File: test_analytics.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_argsort(self, datetime_series):
        self._check_accum_op('argsort', datetime_series, check_dtype=False)
        argsorted = datetime_series.argsort()
        assert issubclass(argsorted.dtype.type, np.integer)

        # GH 2967 (introduced bug in 0.11-dev I think)
        s = Series([Timestamp('201301%02d' % (i + 1)) for i in range(5)])
        assert s.dtype == 'datetime64[ns]'
        shifted = s.shift(-1)
        assert shifted.dtype == 'datetime64[ns]'
        assert isna(shifted[4])

        result = s.argsort()
        expected = Series(lrange(5), dtype='int64')
        assert_series_equal(result, expected)

        result = shifted.argsort()
        expected = Series(lrange(4) + [-1], dtype='int64')
        assert_series_equal(result, expected) 
Example #7
Source File: test_numeric.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_getitem_setitem_slice_bug():
    s = Series(lrange(10), lrange(10))
    result = s[-12:]
    assert_series_equal(result, s)

    result = s[-7:]
    assert_series_equal(result, s[3:])

    result = s[:-12]
    assert_series_equal(result, s[:0])

    s = Series(lrange(10), lrange(10))
    s[-12:] = 0
    assert (s == 0).all()

    s[:-12] = 5
    assert (s == 0).all() 
Example #8
Source File: test_analytics.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_truncate():
    major_axis = Index(lrange(4))
    minor_axis = Index(lrange(2))

    major_codes = np.array([0, 0, 1, 2, 3, 3])
    minor_codes = np.array([0, 1, 0, 1, 0, 1])

    index = MultiIndex(levels=[major_axis, minor_axis],
                       codes=[major_codes, minor_codes])

    result = index.truncate(before=1)
    assert 'foo' not in result.levels[0]
    assert 1 in result.levels[0]

    result = index.truncate(after=1)
    assert 2 not in result.levels[0]
    assert 1 in result.levels[0]

    result = index.truncate(before=1, after=2)
    assert len(result.levels[0]) == 2

    # after < before
    pytest.raises(ValueError, index.truncate, 3, 1) 
Example #9
Source File: test_sorting.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_unsortedindex():
    # GH 11897
    mi = pd.MultiIndex.from_tuples([('z', 'a'), ('x', 'a'), ('y', 'b'),
                                    ('x', 'b'), ('y', 'a'), ('z', 'b')],
                                   names=['one', 'two'])
    df = pd.DataFrame([[i, 10 * i] for i in lrange(6)], index=mi,
                      columns=['one', 'two'])

    # GH 16734: not sorted, but no real slicing
    result = df.loc(axis=0)['z', 'a']
    expected = df.iloc[0]
    tm.assert_series_equal(result, expected)

    with pytest.raises(UnsortedIndexError):
        df.loc(axis=0)['z', slice('a')]
    df.sort_index(inplace=True)
    assert len(df.loc(axis=0)['z', :]) == 2

    with pytest.raises(KeyError):
        df.loc(axis=0)['q', :] 
Example #10
Source File: test_integrity.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_consistency():
    # need to construct an overflow
    major_axis = lrange(70000)
    minor_axis = lrange(10)

    major_codes = np.arange(70000)
    minor_codes = np.repeat(lrange(10), 7000)

    # the fact that is works means it's consistent
    index = MultiIndex(levels=[major_axis, minor_axis],
                       codes=[major_codes, minor_codes])

    # inconsistent
    major_codes = np.array([0, 0, 1, 1, 1, 2, 2, 3, 3])
    minor_codes = np.array([0, 1, 0, 1, 1, 0, 1, 0, 1])
    index = MultiIndex(levels=[major_axis, minor_axis],
                       codes=[major_codes, minor_codes])

    assert index.is_unique is False 
Example #11
Source File: test_iloc.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_iloc():
    s = Series(np.random.randn(10), index=lrange(0, 20, 2))

    for i in range(len(s)):
        result = s.iloc[i]
        exp = s[s.index[i]]
        assert_almost_equal(result, exp)

    # pass a slice
    result = s.iloc[slice(1, 3)]
    expected = s.loc[2:4]
    assert_series_equal(result, expected)

    # test slice is a view
    result[:] = 0
    assert (s[1:3] == 0).all()

    # list of integers
    result = s.iloc[[0, 2, 3, 4, 5]]
    expected = s.reindex(s.index[[0, 2, 3, 4, 5]])
    assert_series_equal(result, expected) 
Example #12
Source File: test_timezones.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_series_append_aware_naive(self):
        rng1 = date_range('1/1/2011 01:00', periods=1, freq='H')
        rng2 = date_range('1/1/2011 02:00', periods=1, freq='H',
                          tz='US/Eastern')
        ser1 = Series(np.random.randn(len(rng1)), index=rng1)
        ser2 = Series(np.random.randn(len(rng2)), index=rng2)
        ts_result = ser1.append(ser2)

        expected = ser1.index.astype(object).append(ser2.index.astype(object))
        assert ts_result.index.equals(expected)

        # mixed
        rng1 = date_range('1/1/2011 01:00', periods=1, freq='H')
        rng2 = lrange(100)
        ser1 = Series(np.random.randn(len(rng1)), index=rng1)
        ser2 = Series(np.random.randn(len(rng2)), index=rng2)
        ts_result = ser1.append(ser2)

        expected = ser1.index.astype(object).append(ser2.index)
        assert ts_result.index.equals(expected) 
Example #13
Source File: test_frame.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_bar_edge(self):
        df = DataFrame({'A': [3] * 5, 'B': lrange(5)}, index=lrange(5))

        self._check_bar_alignment(df, kind='bar', stacked=True, align='edge')
        self._check_bar_alignment(df, kind='bar', stacked=True, width=0.9,
                                  align='edge')
        self._check_bar_alignment(df, kind='barh', stacked=True, align='edge')
        self._check_bar_alignment(df, kind='barh', stacked=True, width=0.9,
                                  align='edge')

        self._check_bar_alignment(df, kind='bar', stacked=False, align='edge')
        self._check_bar_alignment(df, kind='bar', stacked=False, width=0.9,
                                  align='edge')
        self._check_bar_alignment(df, kind='barh', stacked=False, align='edge')
        self._check_bar_alignment(df, kind='barh', stacked=False, width=0.9,
                                  align='edge')

        self._check_bar_alignment(df, kind='bar', subplots=True, align='edge')
        self._check_bar_alignment(df, kind='bar', subplots=True, width=0.9,
                                  align='edge')
        self._check_bar_alignment(df, kind='barh', subplots=True, align='edge')
        self._check_bar_alignment(df, kind='barh', subplots=True, width=0.9,
                                  align='edge') 
Example #14
Source File: test_repr.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_name_printing(self):
        # Test small Series.
        s = Series([0, 1, 2])

        s.name = "test"
        assert "Name: test" in repr(s)

        s.name = None
        assert "Name:" not in repr(s)

        # Test big Series (diff code path).
        s = Series(lrange(0, 1000))

        s.name = "test"
        assert "Name: test" in repr(s)

        s.name = None
        assert "Name:" not in repr(s)

        s = Series(index=date_range('20010101', '20020101'), name='test')
        assert "Name: test" in repr(s) 
Example #15
Source File: test_dtypes.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_astype_datetime(self):
        s = Series(iNaT, dtype='M8[ns]', index=lrange(5))

        s = s.astype('O')
        assert s.dtype == np.object_

        s = Series([datetime(2001, 1, 2, 0, 0)])

        s = s.astype('O')
        assert s.dtype == np.object_

        s = Series([datetime(2001, 1, 2, 0, 0) for i in range(3)])

        s[1] = np.nan
        assert s.dtype == 'M8[ns]'

        s = s.astype('O')
        assert s.dtype == np.object_ 
Example #16
Source File: test_ix.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_frame_setitem_ix(self, multiindex_dataframe_random_data):
        frame = multiindex_dataframe_random_data
        frame.loc[('bar', 'two'), 'B'] = 5
        assert frame.loc[('bar', 'two'), 'B'] == 5

        # with integer labels
        df = frame.copy()
        df.columns = lrange(3)
        df.loc[('bar', 'two'), 1] = 7
        assert df.loc[('bar', 'two'), 1] == 7

        with catch_warnings(record=True):
            simplefilter("ignore", DeprecationWarning)
            df = frame.copy()
            df.columns = lrange(3)
            df.ix[('bar', 'two'), 1] = 7
        assert df.loc[('bar', 'two'), 1] == 7 
Example #17
Source File: test_loc.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_loc_getitem_setitem_integer_slice_keyerrors():
    s = Series(np.random.randn(10), index=lrange(0, 20, 2))

    # this is OK
    cp = s.copy()
    cp.iloc[4:10] = 0
    assert (cp.iloc[4:10] == 0).all()

    # so is this
    cp = s.copy()
    cp.iloc[3:11] = 0
    assert (cp.iloc[3:11] == 0).values.all()

    result = s.iloc[2:6]
    result2 = s.loc[3:11]
    expected = s.reindex([4, 6, 8, 10])

    assert_series_equal(result, expected)
    assert_series_equal(result2, expected)

    # non-monotonic, raise KeyError
    s2 = s.iloc[lrange(5) + lrange(5, 10)[::-1]]
    with pytest.raises(KeyError, match=r"^3L?$"):
        s2.loc[3:11]
    with pytest.raises(KeyError, match=r"^3L?$"):
        s2.loc[3:11] = 0 
Example #18
Source File: test_indexing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_getitem_unordered_dup():
    obj = Series(lrange(5), index=['c', 'a', 'a', 'b', 'b'])
    assert is_scalar(obj['c'])
    assert obj['c'] == 0 
Example #19
Source File: test_indexing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_basic_getitem_with_labels(test_data):
    indices = test_data.ts.index[[5, 10, 15]]

    result = test_data.ts[indices]
    expected = test_data.ts.reindex(indices)
    assert_series_equal(result, expected)

    result = test_data.ts[indices[0]:indices[2]]
    expected = test_data.ts.loc[indices[0]:indices[2]]
    assert_series_equal(result, expected)

    # integer indexes, be careful
    s = Series(np.random.randn(10), index=lrange(0, 20, 2))
    inds = [0, 2, 5, 7, 8]
    arr_inds = np.array([0, 2, 5, 7, 8])
    with tm.assert_produces_warning(FutureWarning,
                                    check_stacklevel=False):
        result = s[inds]
    expected = s.reindex(inds)
    assert_series_equal(result, expected)

    with tm.assert_produces_warning(FutureWarning,
                                    check_stacklevel=False):
        result = s[arr_inds]
    expected = s.reindex(arr_inds)
    assert_series_equal(result, expected)

    # GH12089
    # with tz for values
    s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"),
               index=['a', 'b', 'c'])
    expected = Timestamp('2011-01-01', tz='US/Eastern')
    result = s.loc['a']
    assert result == expected
    result = s.iloc[0]
    assert result == expected
    result = s['a']
    assert result == expected 
Example #20
Source File: test_apply.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_map_type_inference(self):
        s = Series(lrange(3))
        s2 = s.map(lambda x: np.where(x == 0, 0, 1))
        assert issubclass(s2.dtype.type, np.integer) 
Example #21
Source File: test_nth.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_first_last_nth_dtypes(df_mixed_floats):

    df = df_mixed_floats.copy()
    df['E'] = True
    df['F'] = 1

    # tests for first / last / nth
    grouped = df.groupby('A')
    first = grouped.first()
    expected = df.loc[[1, 0], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(first, expected)

    last = grouped.last()
    expected = df.loc[[5, 7], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(last, expected)

    nth = grouped.nth(1)
    expected = df.loc[[3, 2], ['B', 'C', 'D', 'E', 'F']]
    expected.index = Index(['bar', 'foo'], name='A')
    expected = expected.sort_index()
    assert_frame_equal(nth, expected)

    # GH 2763, first/last shifting dtypes
    idx = lrange(10)
    idx.append(9)
    s = Series(data=lrange(11), index=idx, name='IntCol')
    assert s.dtype == 'int64'
    f = s.groupby(level=0).first()
    assert f.dtype == 'int64' 
Example #22
Source File: test_apply.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_apply_chunk_view():
    # Low level tinkering could be unsafe, make sure not
    df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3],
                    'value': compat.lrange(9)})

    result = df.groupby('key', group_keys=False).apply(lambda x: x[:2])
    expected = df.take([0, 1, 3, 4, 6, 7])
    tm.assert_frame_equal(result, expected) 
Example #23
Source File: test_timeseries.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_groupby_count_dateparseerror(self):
        dr = date_range(start='1/1/2012', freq='5min', periods=10)

        # BAD Example, datetimes first
        s = Series(np.arange(10), index=[dr, lrange(10)])
        grouped = s.groupby(lambda x: x[1] % 2 == 0)
        result = grouped.count()

        s = Series(np.arange(10), index=[lrange(10), dr])
        grouped = s.groupby(lambda x: x[0] % 2 == 0)
        expected = grouped.count()

        assert_series_equal(result, expected) 
Example #24
Source File: _core.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _get_xticks(self, convert_period=False):
        index = self.data.index
        is_datetype = index.inferred_type in ('datetime', 'date',
                                              'datetime64', 'time')

        if self.use_index:
            if convert_period and isinstance(index, ABCPeriodIndex):
                self.data = self.data.reindex(index=index.sort_values())
                x = self.data.index.to_timestamp()._mpl_repr()
            elif index.is_numeric():
                """
                Matplotlib supports numeric values or datetime objects as
                xaxis values. Taking LBYL approach here, by the time
                matplotlib raises exception when using non numeric/datetime
                values for xaxis, several actions are already taken by plt.
                """
                x = index._mpl_repr()
            elif is_datetype:
                self.data = self.data[notna(self.data.index)]
                self.data = self.data.sort_index()
                x = self.data.index._mpl_repr()
            else:
                self._need_to_set_index = True
                x = lrange(len(index))
        else:
            x = lrange(len(index))

        return x 
Example #25
Source File: test_alter_axes.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_set_index_makes_timeseries(self):
        idx = tm.makeDateIndex(10)

        s = Series(lrange(10))
        s.index = idx
        assert s.index.is_all_dates 
Example #26
Source File: test_loc.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_loc_non_unique_memory_error(self):

        # GH 4280
        # non_unique index with a large selection triggers a memory error

        columns = list('ABCDEFG')

        def gen_test(l, l2):
            return pd.concat([
                DataFrame(np.random.randn(l, len(columns)),
                          index=lrange(l), columns=columns),
                DataFrame(np.ones((l2, len(columns))),
                          index=[0] * l2, columns=columns)])

        def gen_expected(df, mask):
            len_mask = len(mask)
            return pd.concat([df.take([0]),
                              DataFrame(np.ones((len_mask, len(columns))),
                                        index=[0] * len_mask,
                                        columns=columns),
                              df.take(mask[1:])])

        df = gen_test(900, 100)
        assert df.index.is_unique is False

        mask = np.arange(100)
        result = df.loc[mask]
        expected = gen_expected(df, mask)
        tm.assert_frame_equal(result, expected)

        df = gen_test(900000, 100000)
        assert df.index.is_unique is False

        mask = np.arange(100000)
        result = df.loc[mask]
        expected = gen_expected(df, mask)
        tm.assert_frame_equal(result, expected) 
Example #27
Source File: common.py    From recruit with Apache License 2.0 5 votes vote down vote up
def generate_indices(self, f, values=False):
        """ generate the indices
        if values is True , use the axis values
        is False, use the range
        """

        axes = f.axes
        if values:
            axes = [lrange(len(a)) for a in axes]

        return itertools.product(*axes) 
Example #28
Source File: test_apply.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_apply_no_name_column_conflict():
    df = DataFrame({'name': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2],
                    'name2': [0, 0, 0, 1, 1, 1, 0, 0, 1, 1],
                    'value': compat.lrange(10)[::-1]})

    # it works! #2605
    grouped = df.groupby(['name', 'name2'])
    grouped.apply(lambda x: x.sort_values('value', inplace=True)) 
Example #29
Source File: test_xs.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_xs_integer_key():
    # see gh-2107
    dates = lrange(20111201, 20111205)
    ids = 'abcde'
    index = MultiIndex.from_tuples(
        [x for x in cart_product(dates, ids)],
        names=['date', 'secid'])
    df = DataFrame(
        np.random.randn(len(index), 3), index, ['X', 'Y', 'Z'])

    result = df.xs(20111201, level='date')
    expected = df.loc[20111201, :]
    tm.assert_frame_equal(result, expected) 
Example #30
Source File: test_indexing.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_setitem_ambiguous_keyerror():
    s = Series(lrange(10), index=lrange(0, 20, 2))

    # equivalent of an append
    s2 = s.copy()
    s2[1] = 5
    expected = s.append(Series([5], index=[1]))
    assert_series_equal(s2, expected)

    s2 = s.copy()
    s2.loc[1] = 5
    expected = s.append(Series([5], index=[1]))
    assert_series_equal(s2, expected)