Exemplo n.º 1
0
 def test_zero_step_raises(self):
     with pytest.raises(ValueError):
         BlockPlacement(slice(1, 1, 0))
     with pytest.raises(ValueError):
         BlockPlacement(slice(1, 2, 0))
Exemplo n.º 2
0
 def assert_unbounded_slice_error(slc):
     with pytest.raises(ValueError, match="unbounded slice"):
         BlockPlacement(slc)
Exemplo n.º 3
0
 def test_slice_iter(self):
     assert list(BlockPlacement(slice(0, 3))) == [0, 1, 2]
     assert list(BlockPlacement(slice(0, 0))) == []
     assert list(BlockPlacement(slice(3, 0))) == []
Exemplo n.º 4
0
 def test_blockplacement_add_int_raises(self, val):
     msg = "iadd causes length change"
     with pytest.raises(ValueError, match=msg):
         BlockPlacement(val).add(-10)
Exemplo n.º 5
0
 def test_blockplacement_add(self):
     bpl = BlockPlacement(slice(0, 5))
     assert bpl.add(1).as_slice == slice(1, 6, 1)
     assert bpl.add(np.arange(5)).as_slice == slice(0, 10, 2)
     assert list(bpl.add(np.arange(5, 0, -1))) == [5, 5, 5, 5, 5]
Exemplo n.º 6
0
def create_block(typestr, placement, item_shape=None, num_offset=0):
    """
    Supported typestr:

        * float, f8, f4, f2
        * int, i8, i4, i2, i1
        * uint, u8, u4, u2, u1
        * complex, c16, c8
        * bool
        * object, string, O
        * datetime, dt, M8[ns], M8[ns, tz]
        * timedelta, td, m8[ns]
        * sparse (SparseArray with fill_value=0.0)
        * sparse_na (SparseArray with fill_value=np.nan)
        * category, category2

    """
    placement = BlockPlacement(placement)
    num_items = len(placement)

    if item_shape is None:
        item_shape = (N, )

    shape = (num_items, ) + item_shape

    mat = get_numeric_mat(shape)

    if typestr in (
            "float",
            "f8",
            "f4",
            "f2",
            "int",
            "i8",
            "i4",
            "i2",
            "i1",
            "uint",
            "u8",
            "u4",
            "u2",
            "u1",
    ):
        values = mat.astype(typestr) + num_offset
    elif typestr in ("complex", "c16", "c8"):
        values = 1.0j * (mat.astype(typestr) + num_offset)
    elif typestr in ("object", "string", "O"):
        values = np.reshape(["A%d" % i for i in mat.ravel() + num_offset],
                            shape)
    elif typestr in ("b", "bool"):
        values = np.ones(shape, dtype=np.bool_)
    elif typestr in ("datetime", "dt", "M8[ns]"):
        values = (mat * 1e9).astype("M8[ns]")
    elif typestr.startswith("M8[ns"):
        # datetime with tz
        m = re.search(r"M8\[ns,\s*(\w+\/?\w*)\]", typestr)
        assert m is not None, "incompatible typestr -> {0}".format(typestr)
        tz = m.groups()[0]
        assert num_items == 1, "must have only 1 num items for a tz-aware"
        values = DatetimeIndex(np.arange(N) * 1e9, tz=tz)
    elif typestr in ("timedelta", "td", "m8[ns]"):
        values = (mat * 1).astype("m8[ns]")
    elif typestr in ("category", ):
        values = Categorical([1, 1, 2, 2, 3, 3, 3, 3, 4, 4])
    elif typestr in ("category2", ):
        values = Categorical(
            ["a", "a", "a", "a", "b", "b", "c", "c", "c", "d"])
    elif typestr in ("sparse", "sparse_na"):
        # FIXME: doesn't support num_rows != 10
        assert shape[-1] == 10
        assert all(s == 1 for s in shape[:-1])
        if typestr.endswith("_na"):
            fill_value = np.nan
        else:
            fill_value = 0.0
        values = SparseArray(
            [fill_value, fill_value, 1, 2, 3, fill_value, 4, 5, fill_value, 6],
            fill_value=fill_value,
        )
        arr = values.sp_values.view()
        arr += num_offset - 1
    else:
        raise ValueError('Unsupported typestr: "%s"' % typestr)

    return make_block(values, placement=placement, ndim=len(shape))
Exemplo n.º 7
0
 def test_array_to_slice_conversion(self, arr, slc):
     assert BlockPlacement(arr).as_slice == slc
Exemplo n.º 8
0
 def assert_not_slice_like(arr):
     assert not BlockPlacement(arr).is_slice_like
Exemplo n.º 9
0
 def test_unbounded_slice_raises(self, slc):
     msg = "unbounded slice"
     with pytest.raises(ValueError, match=msg):
         BlockPlacement(slc)
Exemplo n.º 10
0
 def test_not_slice_like_slices(self, slc):
     assert not BlockPlacement(slc).is_slice_like
Exemplo n.º 11
0
    def test_slice_canonize_negative_stop(self):
        # GH#37524 negative stop is OK with negative step and positive start
        slc = slice(3, -1, -2)

        bp = BlockPlacement(slc)
        assert bp.indexer == slice(3, None, -2)
Exemplo n.º 12
0
 def test_zero_step_raises(self, slc):
     msg = "slice step cannot be zero"
     with pytest.raises(ValueError, match=msg):
         BlockPlacement(slc)
Exemplo n.º 13
0
 def test_slice_len(self, slc, expected):
     assert len(BlockPlacement(slc)) == expected
Exemplo n.º 14
0
 def assert_not_slice_like(slc):
     assert not BlockPlacement(slc).is_slice_like
Exemplo n.º 15
0
 def test_not_slice_like_arrays(self, arr):
     assert not BlockPlacement(arr).is_slice_like
Exemplo n.º 16
0
 def assert_as_slice_equals(arr, slc):
     assert BlockPlacement(arr).as_slice == slc
Exemplo n.º 17
0
 def test_slice_iter(self, slc, expected):
     assert list(BlockPlacement(slc)) == expected
Exemplo n.º 18
0
 def assert_as_array_equals(slc, asarray):
     tm.assert_numpy_array_equal(
         BlockPlacement(slc).as_array,
         np.asarray(asarray, dtype=np.int64))
Exemplo n.º 19
0
 def test_slice_to_array_conversion(self, slc, arr):
     tm.assert_numpy_array_equal(
         BlockPlacement(slc).as_array, np.asarray(arr, dtype=np.intp)
     )
Exemplo n.º 20
0
 def assert_add_equals(val, inc, result):
     assert list(BlockPlacement(val).add(inc)) == result
Exemplo n.º 21
0
 def test_blockplacement_add_int(self, val, inc, expected):
     assert list(BlockPlacement(val).add(inc)) == expected
Exemplo n.º 22
0
 def test_blockplacement_add(self):
     bpl = BlockPlacement(slice(0, 5))
     assert bpl.add(1).as_slice == slice(1, 6, 1)
     assert bpl.add(np.arange(5)).as_slice == slice(0, 10, 2)
     assert list(bpl.add(np.arange(5, 0, -1))) == [5, 5, 5, 5, 5]
Exemplo n.º 23
0
def create_block(typestr, placement, item_shape=None, num_offset=0):
    """
    Supported typestr:

        * float, f8, f4, f2
        * int, i8, i4, i2, i1
        * uint, u8, u4, u2, u1
        * complex, c16, c8
        * bool
        * object, string, O
        * datetime, dt, M8[ns], M8[ns, tz]
        * timedelta, td, m8[ns]
        * sparse (SparseArray with fill_value=0.0)
        * sparse_na (SparseArray with fill_value=np.nan)
        * category, category2

    """
    placement = BlockPlacement(placement)
    num_items = len(placement)

    if item_shape is None:
        item_shape = (N, )

    shape = (num_items, ) + item_shape

    mat = get_numeric_mat(shape)

    if typestr in ('float', 'f8', 'f4', 'f2', 'int', 'i8', 'i4', 'i2', 'i1',
                   'uint', 'u8', 'u4', 'u2', 'u1'):
        values = mat.astype(typestr) + num_offset
    elif typestr in ('complex', 'c16', 'c8'):
        values = 1.j * (mat.astype(typestr) + num_offset)
    elif typestr in ('object', 'string', 'O'):
        values = np.reshape(['A%d' % i for i in mat.ravel() + num_offset],
                            shape)
    elif typestr in ('b', 'bool', ):
        values = np.ones(shape, dtype=np.bool_)
    elif typestr in ('datetime', 'dt', 'M8[ns]'):
        values = (mat * 1e9).astype('M8[ns]')
    elif typestr.startswith('M8[ns'):
        # datetime with tz
        m = re.search(r'M8\[ns,\s*(\w+\/?\w*)\]', typestr)
        assert m is not None, "incompatible typestr -> {0}".format(typestr)
        tz = m.groups()[0]
        assert num_items == 1, "must have only 1 num items for a tz-aware"
        values = DatetimeIndex(np.arange(N) * 1e9, tz=tz)
    elif typestr in ('timedelta', 'td', 'm8[ns]'):
        values = (mat * 1).astype('m8[ns]')
    elif typestr in ('category', ):
        values = Categorical([1, 1, 2, 2, 3, 3, 3, 3, 4, 4])
    elif typestr in ('category2', ):
        values = Categorical(['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd'
                              ])
    elif typestr in ('sparse', 'sparse_na'):
        # FIXME: doesn't support num_rows != 10
        assert shape[-1] == 10
        assert all(s == 1 for s in shape[:-1])
        if typestr.endswith('_na'):
            fill_value = np.nan
        else:
            fill_value = 0.0
        values = SparseArray([fill_value, fill_value, 1, 2, 3, fill_value,
                              4, 5, fill_value, 6], fill_value=fill_value)
        arr = values.sp_values.view()
        arr += (num_offset - 1)
    else:
        raise ValueError('Unsupported typestr: "%s"' % typestr)

    return make_block(values, placement=placement, ndim=len(shape))