Python pandas.RangeIndex() Examples

The following are 30 code examples for showing how to use pandas.RangeIndex(). These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module pandas , or try the search function .

Example 1
Project: wanggeService   Author: pchaos   File: hsgtcg.py    License: MIT License 6 votes vote down vote up
def getStockHdStatistics(cls, code, browser, retryCount=3):
        """ 抓取持股统计

        :param code: 股票代码
        :param browser: webdriver浏览器
        :return:
        """
        url = 'http://data.eastmoney.com/hsgtcg/StockHdStatistics.aspx?stock={}'.format(code)
        for i in range(retryCount):
            df = cls.scrap(url, browser)
            if len(df) > 0:
                # 修复持股数量
                df['hvol'] = df['hvol'].apply(lambda x: HSGTCG.hz2Num(x)).astype(float)
                df['hamount'] = df['hamount'].apply(lambda x: HSGTCG.hz2Num(x)).astype(float)
                df['close'] = df['close'].astype(float)
                df['tradedate'] = df['tradedate'].apply(lambda x: convertToDate(x)).astype(datetime.date)
                df = df[df['tradedate'].apply(lambda x: Stocktradedate.if_tradeday(x))]  # 删除不是交易日的数据。这是东方财富网页版的bug
                df.index = pd.RangeIndex(len(df.index))
                break
            else:
                pass

        return df 
Example 2
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 6 votes vote down vote up
def test_constructor_name(self):
        # GH12288
        orig = RangeIndex(10)
        orig.name = 'original'

        copy = RangeIndex(orig)
        copy.name = 'copy'

        assert orig.name == 'original'
        assert copy.name == 'copy'

        new = Index(copy)
        assert new.name == 'copy'

        new.name = 'new'
        assert orig.name == 'original'
        assert copy.name == 'copy'
        assert new.name == 'new' 
Example 3
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 6 votes vote down vote up
def test_delete(self):

        idx = RangeIndex(5, name='Foo')
        expected = idx[1:].astype(int)
        result = idx.delete(0)
        tm.assert_index_equal(result, expected)
        assert result.name == expected.name

        expected = idx[:-1].astype(int)
        result = idx.delete(-1)
        tm.assert_index_equal(result, expected)
        assert result.name == expected.name

        with pytest.raises((IndexError, ValueError)):
            # either depending on numpy version
            result = idx.delete(len(idx)) 
Example 4
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 6 votes vote down vote up
def test_explicit_conversions(self):

        # GH 8608
        # add/sub are overridden explicitly for Float/Int Index
        idx = RangeIndex(5)

        # float conversions
        arr = np.arange(5, dtype='int64') * 3.2
        expected = Float64Index(arr)
        fidx = idx * 3.2
        tm.assert_index_equal(fidx, expected)
        fidx = 3.2 * idx
        tm.assert_index_equal(fidx, expected)

        # interops with numpy arrays
        expected = Float64Index(arr)
        a = np.zeros(5, dtype='float64')
        result = fidx - a
        tm.assert_index_equal(result, expected)

        expected = Float64Index(-arr)
        a = np.zeros(5, dtype='float64')
        result = a - fidx
        tm.assert_index_equal(result, expected) 
Example 5
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 6 votes vote down vote up
def test_min_fitting_element(self):
        result = RangeIndex(0, 20, 2)._min_fitting_element(1)
        assert 2 == result

        result = RangeIndex(1, 6)._min_fitting_element(1)
        assert 1 == result

        result = RangeIndex(18, -2, -2)._min_fitting_element(1)
        assert 2 == result

        result = RangeIndex(5, 0, -1)._min_fitting_element(1)
        assert 1 == result

        big_num = 500000000000000000000000

        result = RangeIndex(5, big_num * 2, 1)._min_fitting_element(big_num)
        assert big_num == result 
Example 6
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 6 votes vote down vote up
def test_max_fitting_element(self):
        result = RangeIndex(0, 20, 2)._max_fitting_element(17)
        assert 16 == result

        result = RangeIndex(1, 6)._max_fitting_element(4)
        assert 4 == result

        result = RangeIndex(18, -2, -2)._max_fitting_element(17)
        assert 16 == result

        result = RangeIndex(5, 0, -1)._max_fitting_element(4)
        assert 4 == result

        big_num = 500000000000000000000000

        result = RangeIndex(5, big_num * 2, 1)._max_fitting_element(big_num)
        assert big_num == result 
Example 7
Project: recruit   Author: Frank-qlu   File: test_common.py    License: Apache License 2.0 6 votes vote down vote up
def test_duplicated(self, indices, keep):
        if not len(indices) or isinstance(indices, (MultiIndex, RangeIndex)):
            # MultiIndex tested separately in:
            # tests/indexes/multi/test_unique_and_duplicates
            pytest.skip('Skip check for empty Index, MultiIndex, RangeIndex')

        holder = type(indices)

        idx = holder(indices)
        if idx.has_duplicates:
            # We are testing the duplicated-method here, so we need to know
            # exactly which indices are duplicate and how (for the result).
            # This is not possible if "idx" has duplicates already, which we
            # therefore remove. This is seemingly circular, as drop_duplicates
            # invokes duplicated, but in the end, it all works out because we
            # cross-check with Series.duplicated, which is tested separately.
            idx = idx.drop_duplicates()

        n, k = len(idx), 10
        duplicated_selection = np.random.choice(n, k * n)
        expected = pd.Series(duplicated_selection).duplicated(keep=keep).values
        idx = holder(idx.values[duplicated_selection])

        result = idx.duplicated(keep=keep)
        tm.assert_numpy_array_equal(result, expected) 
Example 8
Project: recruit   Author: Frank-qlu   File: test_sorting.py    License: Apache License 2.0 6 votes vote down vote up
def test_numpy_argsort(idx):
    result = np.argsort(idx)
    expected = idx.argsort()
    tm.assert_numpy_array_equal(result, expected)

    # these are the only two types that perform
    # pandas compatibility input validation - the
    # rest already perform separate (or no) such
    # validation via their 'values' attribute as
    # defined in pandas.core.indexes/base.py - they
    # cannot be changed at the moment due to
    # backwards compatibility concerns
    if isinstance(type(idx), (CategoricalIndex, RangeIndex)):
        msg = "the 'axis' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            np.argsort(idx, axis=1)

        msg = "the 'kind' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            np.argsort(idx, kind='mergesort')

        msg = "the 'order' parameter is not supported"
        with pytest.raises(ValueError, match=msg):
            np.argsort(idx, order=('a', 'b')) 
Example 9
Project: psst   Author: power-system-simulation-toolbox   File: descriptors.py    License: MIT License 6 votes vote down vote up
def setattributeindex(self, instance, value):
        bus_name = instance.bus.index
        instance.branch['F_BUS'] = instance.branch['F_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
        instance.branch['T_BUS'] = instance.branch['T_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
        instance.gen['GEN_BUS'] = instance.gen['GEN_BUS'].apply(lambda x: value[bus_name.get_loc(x)])

        try:
            instance.load.columns = [v for b, v in zip(instance.bus_name.isin(instance.load.columns), value) if b == True]
        except ValueError:
            instance.load.columns = value
        except AttributeError:
            instance.load = pd.DataFrame(0, index=range(0, 1), columns=value, dtype='float')

        instance.bus.index = value

        if isinstance(instance.bus_name, pd.RangeIndex) or isinstance(instance.bus_name, pd.Int64Index):
            logger.debug('Forcing string types for all bus names')
            instance.bus_name = ['Bus{}'.format(b) for b in instance.bus_name] 
Example 10
Project: mars   Author: mars-project   File: string_.py    License: Apache License 2.0 6 votes vote down vote up
def call(cls, op, inp):
        method_kwargs = op.method_kwargs
        if method_kwargs.get('expand', False) is False:
            return super().call(op, inp)
        n = method_kwargs.get('n', -1)
        # does not support if expand and n == -1
        if n == -1:  # pragma: no cover
            raise NotImplementedError('`n` needs to be specified when expand=True')

        op.output_types = [OutputType.dataframe]
        columns = pd.RangeIndex(n + 1)
        columns_value = parse_index(columns, store_data=True)
        dtypes = pd.Series([inp.dtype] * len(columns), index=columns)
        return op.new_dataframe([inp], shape=(inp.shape[0], len(columns)),
                                dtypes=dtypes, columns_value=columns_value,
                                index_value=inp.index_value) 
Example 11
Project: mars   Author: mars-project   File: drop_duplicates.py    License: Apache License 2.0 6 votes vote down vote up
def __call__(self, inp, inplace=False):
        self._output_types = inp.op.output_types
        params = inp.params
        if self._ignore_index:
            params['index_value'] = parse_index(pd.RangeIndex(-1))
        else:
            params['index_value'] = gen_unknown_index_value(
                params['index_value'], self._keep, self._subset, type(self).__name__)
        shape_list = list(params['shape'])
        shape_list[0] = np.nan
        params['shape'] = tuple(shape_list)

        ret = self.new_tileable([inp], kws=[params])
        if inplace:
            inp.data = ret.data
        return ret 
Example 12
Project: mars   Author: mars-project   File: transform.py    License: Apache License 2.0 6 votes vote down vote up
def _infer_df_func_returns(self, in_dtypes, dtypes):
        if self.output_types[0] == OutputType.dataframe:
            empty_df = build_empty_df(in_dtypes, index=pd.RangeIndex(2))
            with np.errstate(all='ignore'):
                if self.call_agg:
                    infer_df = empty_df.agg(self._func, axis=self._axis, *self.args, **self.kwds)
                else:
                    infer_df = empty_df.transform(self._func, axis=self._axis, *self.args, **self.kwds)
        else:
            empty_df = build_empty_series(in_dtypes[1], index=pd.RangeIndex(2), name=in_dtypes[0])
            with np.errstate(all='ignore'):
                if self.call_agg:
                    infer_df = empty_df.agg(self._func, args=self.args, **self.kwds)
                else:
                    infer_df = empty_df.transform(self._func, convert_dtype=self.convert_dtype,
                                                  args=self.args, **self.kwds)

        if isinstance(infer_df, pd.DataFrame):
            new_dtypes = dtypes or infer_df.dtypes
            self.output_types = [OutputType.dataframe]
        else:
            new_dtypes = dtypes or (infer_df.name, infer_df.dtype)
            self.output_types = [OutputType.series]

        return new_dtypes 
Example 13
Project: mars   Author: mars-project   File: melt.py    License: Apache License 2.0 6 votes vote down vote up
def tile(cls, op: 'DataFrameMelt'):
        inp = op.inputs[0]
        out = op.outputs[0]

        inp = inp.rechunk({1: (inp.shape[1],)})._inplace_tile()

        chunks = []
        for c in inp.chunks:
            new_op = op.copy().reset_key()
            chunks.append(new_op.new_chunk(
                [c], index=c.index,  shape=(np.nan, out.shape[1]), dtypes=out.dtypes,
                index_value=parse_index(pd.RangeIndex(-1), c.key, c.index_value.key),
                columns_value=out.columns_value))

        chunks = standardize_range_index(chunks)
        new_op = op.copy().reset_key()
        return new_op.new_tileables(
            [inp], chunks=chunks, nsplits=((np.nan,) * inp.chunk_shape[0], (out.shape[1],)), **out.params) 
Example 14
Project: mars   Author: mars-project   File: reset_index.py    License: Apache License 2.0 6 votes vote down vote up
def _call_dataframe(self, a):
        if self.drop:
            shape = a.shape
            columns_value = a.columns_value
            dtypes = a.dtypes
            range_value = -1 if np.isnan(a.shape[0]) else a.shape[0]
            index_value = parse_index(pd.RangeIndex(range_value))
        else:
            empty_df = build_empty_df(a.dtypes)
            empty_df.index = a.index_value.to_pandas()[:0]
            empty_df = empty_df.reset_index(level=self.level, col_level=self.col_level, col_fill=self.col_fill)
            shape = (a.shape[0], len(empty_df.columns))
            columns_value = parse_index(empty_df.columns, store_data=True)
            dtypes = empty_df.dtypes
            index_value = self._get_out_index(empty_df, shape)
        return self.new_dataframe([a], shape=shape, columns_value=columns_value,
                                  index_value=index_value, dtypes=dtypes) 
Example 15
Project: mars   Author: mars-project   File: test_datasource_execution.py    License: Apache License 2.0 6 votes vote down vote up
def testFromRecordsExecution(self):
        dtype = np.dtype([('x', 'int'), ('y', 'double'), ('z', '<U16')])

        ndarr = np.ones((10,), dtype=dtype)
        pdf_expected = pd.DataFrame.from_records(ndarr, index=pd.RangeIndex(10))

        # from structured array of mars
        tensor = mt.ones((10,), dtype=dtype, chunk_size=3)
        df1 = from_records(tensor)
        df1_result = self.executor.execute_dataframe(df1, concat=True)[0]
        pd.testing.assert_frame_equal(df1_result, pdf_expected)

        # from structured array of numpy
        df2 = from_records(ndarr)
        df2_result = self.executor.execute_dataframe(df2, concat=True)[0]
        pd.testing.assert_frame_equal(df2_result, pdf_expected) 
Example 16
Project: mars   Author: mars-project   File: from_tensor.py    License: Apache License 2.0 6 votes vote down vote up
def __call__(self, input_tensor, index, name):
        inputs = [input_tensor]
        if index is not None:
            if not isinstance(index, pd.Index):
                if isinstance(index, INDEX_TYPE):
                    self._index = index
                    index_value = index.index_value
                    inputs.append(index)
                elif isinstance(index, (Base, Entity)):
                    self._index = index
                    index = astensor(index)
                    if index.ndim != 1:
                        raise ValueError('index should be 1-d, got {}-d'.format(index.ndim))
                    index_value = parse_index(pd.Index([], dtype=index.dtype), index, type(self).__name__)
                    inputs.append(index)
                else:
                    index = pd.Index(index)
                    index_value = parse_index(index, store_data=True)
            else:
                index_value = parse_index(index, store_data=True)
        else:
            index_value = parse_index(pd.RangeIndex(start=0, stop=input_tensor.shape[0]))
        return self.new_series(inputs, shape=input_tensor.shape, dtype=self.dtype,
                               index_value=index_value, name=name) 
Example 17
Project: mars   Author: mars-project   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def infer_index_value(left_index_value, right_index_value):
    from .core import IndexValue

    if isinstance(left_index_value.value, IndexValue.RangeIndex) and \
            isinstance(right_index_value.value, IndexValue.RangeIndex):
        if left_index_value.value.slice == right_index_value.value.slice:
            return left_index_value
        return parse_index(pd.Int64Index([]), left_index_value, right_index_value)

    # when left index and right index is identical, and both of them are elements unique,
    # we can infer that the out index should be identical also
    if left_index_value.is_unique and right_index_value.is_unique and \
            left_index_value.key == right_index_value.key:
        return left_index_value

    left_index = left_index_value.to_pandas()
    right_index = right_index_value.to_pandas()
    out_index = pd.Index([], dtype=find_common_type([left_index.dtype, right_index.dtype]))
    return parse_index(out_index, left_index_value, right_index_value) 
Example 18
Project: mars   Author: mars-project   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def filter_index_value(index_value, min_max, store_data=False):
    from .core import IndexValue

    min_val, min_val_close, max_val, max_val_close = min_max

    pd_index = index_value.to_pandas()

    if isinstance(index_value.value, IndexValue.RangeIndex):
        pd_filtered_index = _filter_range_index(pd_index, min_val, min_val_close,
                                                max_val, max_val_close)
        return parse_index(pd_filtered_index, store_data=store_data)

    if min_val_close:
        f = pd_index >= min_val
    else:
        f = pd_index > min_val
    if max_val_close:
        f = f & (pd_index <= max_val)
    else:
        f = f & (pd_index < max_val)

    return parse_index(pd_index[f], store_data=store_data) 
Example 19
Project: mars   Author: mars-project   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def indexing_index_value(index_value, indexes, store_data=False):
    pd_index = index_value.to_pandas()
    if not index_value.has_value():
        new_index_value = parse_index(pd_index, indexes, store_data=store_data)
        new_index_value._index_value._min_val = index_value.min_val
        new_index_value._index_value._min_val_close = index_value.min_val_close
        new_index_value._index_value._max_val = index_value.max_val
        new_index_value._index_value._max_val_close = index_value.max_val_close
        return new_index_value
    else:
        if isinstance(indexes, Integral):
            return parse_index(pd_index[[indexes]], store_data=store_data)
        elif isinstance(indexes, Entity):
            if isinstance(pd_index, pd.RangeIndex):
                return parse_index(
                    pd.RangeIndex(-1), indexes, index_value, store_data=False)
            else:
                return parse_index(
                    type(pd_index)([]), indexes, index_value, store_data=False)
        if isinstance(indexes, tuple):
            return parse_index(pd_index[list(indexes)], store_data=store_data)
        else:
            return parse_index(pd_index[indexes], store_data=store_data) 
Example 20
Project: mars   Author: mars-project   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def convert_labels_into_positions(pandas_index, labels):
    """
    Convert labels into positions

    :param pandas_index: pandas Index
    :param labels: labels
    :return: positions
    """
    result = []
    for label in labels:
        loc = pandas_index.get_loc(label)
        if isinstance(loc, (int, np.integer)):
            result.append(loc)
        else:
            # slice or boolean array
            result.extend(
                pd.RangeIndex(len(pandas_index))[loc].tolist())
    return np.asarray(result) 
Example 21
Project: wanggeService   Author: pchaos   File: rps.py    License: MIT License 5 votes vote down vote up
def updateSaved(cls, qssaved):
        with transaction.atomic():
            for val in qssaved:
                # 更新日期在原来保存区间的数据,需要单个更新
                val.index = pd.RangeIndex(len(val.index))
                for ind in val.index:
                    v = val.iloc[ind]
                    rps = cls.objects.get(tradedate=val['tradedate'][0], code_id=v['code_id'])
                    rps.rps120 = v['rps120']
                    rps.rps250 = v['rps250']
                    rps.save() 
Example 22
Project: wanggeService   Author: pchaos   File: rps.py    License: MIT License 5 votes vote down vote up
def caculateRPS(df, nlist=[120, 250]):
        """ 计算n日rps

        :param df: dataframe
        :param nlist: n日list
        :return:
        """
        orgincolumns = [c for c in df.columns]
        assert len(df) > 0, 'df必须不为空'
        dfd = df
        for n in nlist:
            dfd.reset_index(inplace=True)
            # dfd.index = pd.RangeIndex(len(dfd.index))
            rpsname = 'rps{}'.format(str(n))
            rpsn = dfd[[rpsname, 'code_id']]
            rpsn = rpsn[rpsn[rpsname].apply(lambda x: float(x) > -99)]  # 处理Nan数据
            rpsn = rpsn.sort_values(by=[rpsname])
            # rpsn.reset_index(inplace=True)
            rpsn.index = pd.RangeIndex(len(rpsn.index))
            rpsn['a'] = np.round(100 * (rpsn.index - rpsn.index.min()) / (rpsn.index.max() - rpsn.index.min()), 2)
            rpsn.set_index('code_id', inplace=True)
            dfd.set_index('code_id', inplace=True)
            if (rpsname not in list(dfd.columns)):
                # 结果集中没有rpsname列名,则增加空列
                dfd[rpsname] = pd.np.nan
            dfd.loc[:, (rpsname)] = rpsn['a']
        # NaN 设置成 -1
        dfd['rps250'] = dfd['rps250'].apply(lambda x: x if (float(x) > -99) else -1)
        dfd.reset_index(inplace=True)
        return dfd[orgincolumns] 
Example 23
Project: recruit   Author: Frank-qlu   File: test_external_block.py    License: Apache License 2.0 5 votes vote down vote up
def test_custom_repr():
    values = np.arange(3, dtype='int64')

    # series
    block = CustomBlock(values, placement=slice(0, 3))

    s = pd.Series(SingleBlockManager(block, pd.RangeIndex(3)))
    assert repr(s) == '0    Val: 0\n1    Val: 1\n2    Val: 2\ndtype: int64'

    # dataframe
    block = CustomBlock(values, placement=slice(0, 1))
    blk_mgr = BlockManager([block], [['col'], range(3)])
    df = pd.DataFrame(blk_mgr)
    assert repr(df) == '      col\n0  Val: 0\n1  Val: 1\n2  Val: 2' 
Example 24
Project: recruit   Author: Frank-qlu   File: test_external_block.py    License: Apache License 2.0 5 votes vote down vote up
def test_concat_series():
    # GH17728
    values = np.arange(3, dtype='int64')
    block = CustomBlock(values, placement=slice(0, 3))
    s = pd.Series(block, pd.RangeIndex(3), fastpath=True)

    res = pd.concat([s, s])
    assert isinstance(res._data.blocks[0], CustomBlock) 
Example 25
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def setup_method(self, method):
        self.indices = dict(index=RangeIndex(0, 20, 2, name='foo'),
                            index_dec=RangeIndex(18, -1, -2, name='bar'))
        self.setup_indices() 
Example 26
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def create_index(self):
        return RangeIndex(5) 
Example 27
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def test_constructor(self):
        index = RangeIndex(5)
        expected = np.arange(5, dtype=np.int64)
        assert isinstance(index, RangeIndex)
        assert index._start == 0
        assert index._stop == 5
        assert index._step == 1
        assert index.name is None
        tm.assert_index_equal(Index(expected), index)

        index = RangeIndex(1, 5)
        expected = np.arange(1, 5, dtype=np.int64)
        assert isinstance(index, RangeIndex)
        assert index._start == 1
        tm.assert_index_equal(Index(expected), index)

        index = RangeIndex(1, 5, 2)
        expected = np.arange(1, 5, 2, dtype=np.int64)
        assert isinstance(index, RangeIndex)
        assert index._step == 2
        tm.assert_index_equal(Index(expected), index)

        for index in [RangeIndex(0), RangeIndex(start=0), RangeIndex(stop=0),
                      RangeIndex(0, 0)]:
            expected = np.empty(0, dtype=np.int64)
            assert isinstance(index, RangeIndex)
            assert index._start == 0
            assert index._stop == 0
            assert index._step == 1
            tm.assert_index_equal(Index(expected), index)

        for index in [RangeIndex(0, name='Foo'),
                      RangeIndex(start=0, name='Foo'),
                      RangeIndex(stop=0, name='Foo'),
                      RangeIndex(0, 0, name='Foo')]:
            assert isinstance(index, RangeIndex)
            assert index.name == 'Foo'

        # we don't allow on a bare Index
        with pytest.raises(TypeError):
            Index(0, 1000) 
Example 28
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def test_constructor_same(self):

        # pass thru w and w/o copy
        index = RangeIndex(1, 5, 2)
        result = RangeIndex(index, copy=False)
        assert result.identical(index)

        result = RangeIndex(index, copy=True)
        tm.assert_index_equal(result, index, exact=True)

        result = RangeIndex(index)
        tm.assert_index_equal(result, index, exact=True)

        with pytest.raises(TypeError):
            RangeIndex(index, dtype='float64') 
Example 29
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def test_constructor_range(self):

        with pytest.raises(TypeError):
            RangeIndex(range(1, 5, 2))

        result = RangeIndex.from_range(range(1, 5, 2))
        expected = RangeIndex(1, 5, 2)
        tm.assert_index_equal(result, expected, exact=True)

        result = RangeIndex.from_range(range(5, 6))
        expected = RangeIndex(5, 6, 1)
        tm.assert_index_equal(result, expected, exact=True)

        # an invalid range
        result = RangeIndex.from_range(range(5, 1))
        expected = RangeIndex(0, 0, 1)
        tm.assert_index_equal(result, expected, exact=True)

        result = RangeIndex.from_range(range(5))
        expected = RangeIndex(0, 5, 1)
        tm.assert_index_equal(result, expected, exact=True)

        result = Index(range(1, 5, 2))
        expected = RangeIndex(1, 5, 2)
        tm.assert_index_equal(result, expected, exact=True)

        with pytest.raises(TypeError):
            Index(range(1, 5, 2), dtype='float64') 
Example 30
Project: recruit   Author: Frank-qlu   File: test_range.py    License: Apache License 2.0 5 votes vote down vote up
def test_constructor_corner(self):
        arr = np.array([1, 2, 3, 4], dtype=object)
        index = RangeIndex(1, 5)
        assert index.values.dtype == np.int64
        tm.assert_index_equal(index, Index(arr))

        # non-int raise Exception
        with pytest.raises(TypeError):
            RangeIndex('1', '10', '1')
        with pytest.raises(TypeError):
            RangeIndex(1.1, 10.2, 1.3)

        # invalid passed type
        with pytest.raises(TypeError):
            RangeIndex(1, 5, dtype='float64')