Best Python code snippet using pandera_python
test_coretypes.py
Source:test_coretypes.py  
...49    with pytest.raises(TypeError):50        DataShape('5 * int32')51class TestToNumpyDtype(object):52    def test_simple(self):53        assert to_numpy_dtype(dshape('2 * int32')) == np.int3254        assert (to_numpy_dtype(dshape('2 * {x: int32, y: int32}')) ==55                np.dtype([('x', '<i4'), ('y', '<i4')]))56    def test_datetime(self):57        assert to_numpy_dtype(dshape('2 * datetime')) == np.dtype('M8[us]')58    def test_date(self):59        assert to_numpy_dtype(dshape('2 * date')) == np.dtype('M8[D]')60    def test_string(self):61        assert to_numpy_dtype(dshape('2 * string')) == np.dtype('O')62    def test_dimensions(self):63        return to_numpy_dtype(dshape('var * int32')) == np.int3264    def test_timedelta(self):65        assert to_numpy_dtype(dshape('2 * timedelta')) == np.dtype('m8[us]')66        assert to_numpy_dtype(dshape("2 * timedelta[unit='s']")) == \67            np.dtype('m8[s]')68    def test_decimal(self):69        assert to_numpy_dtype(dshape('decimal[18,0]')) == np.int6470        assert to_numpy_dtype(dshape('decimal[7,2]')) == np.float6471        assert to_numpy_dtype(dshape('decimal[4]')) == np.int1672        with pytest.raises(TypeError):73            to_numpy_dtype(dshape('decimal[21]'))74def test_timedelta_eval_repr():75    assert eval(repr(dshape('timedelta'))) == dshape('timedelta')76    assert eval(repr(dshape('timedelta[unit="ms"]'))) == \77        dshape('timedelta[unit="ms"]')78def test_timedelta_bad_unit():79    with pytest.raises(ValueError):80        dshape('timedelta[unit="foo"]')81def test_timedelta_nano():82    dshape('timedelta[unit="ns"]').measure.unit == 'ns'83def test_timedelta_aliases():84    for alias in _unit_aliases:85        a = alias + 's'86        assert (dshape('timedelta[unit=%r]' % a) ==87                dshape('timedelta[unit=%r]' % _unit_aliases[alias]))88class TestFromNumPyDtype(object):89    def test_int32(self):90        assert from_numpy((2,), 'int32') == dshape('2 * int32')91        assert from_numpy((2,), 'i4') == dshape('2 * int32')92    def test_struct(self):93        dtype = np.dtype([('x', '<i4'), ('y', '<i4')])94        result = from_numpy((2,), dtype)95        assert result == dshape('2 * {x: int32, y: int32}')96    def test_datetime(self):97        keys = 'h', 'm', 's', 'ms', 'us', 'ns', 'ps', 'fs', 'as'98        for k in keys:99            assert from_numpy((2,),100                              np.dtype('M8[%s]' % k)) == dshape('2 * datetime')101    def test_date(self):102        for d in ('D', 'M', 'Y', 'W'):103            assert from_numpy((2,),104                              np.dtype('M8[%s]' % d)) == dshape('2 * date')105    def test_timedelta(self):106        for d in _units:107            assert from_numpy((2,),108                              np.dtype('m8[%s]' % d)) == \109                dshape('2 * timedelta[unit=%r]' % d)110    def test_ascii_string(self):111        assert (from_numpy((2,), np.dtype('S7')) ==112                dshape('2 * string[7, "ascii"]'))113    def test_string(self):114        assert (from_numpy((2,), np.dtype('U7')) ==115                dshape('2 * string[7, "U32"]'))116    def test_string_from_CType_classmethod(self):117        assert CType.from_numpy_dtype(np.dtype('S7')) == String(7, 'A')118def test_eq():119    assert dshape('int') == dshape('int')120    assert dshape('int') != 'apple'121def test_serializable():122    ds = dshape('''{id: int64,123                    name: string,124                    amount: float32,125                    arr: 3 * (int32, string)}''')126    ds2 = pickle.loads(pickle.dumps(ds))127    assert str(ds) == str(ds2)128def test_subshape():129    ds = dshape('5 * 3 * float32')130    assert ds.subshape[2:] == dshape('3 * 3 * float32')131    ds = dshape('5 * 3 * float32')132    assert ds.subshape[::2] == dshape('3 * 3 * float32')133def test_negative_slicing():134    ds = dshape('10 * int')135    assert ds.subshape[-3:] == dshape('3 * int')136def test_newaxis_slicing():137    ds = dshape('10 * int')138    assert ds.subshape[None, :] == dshape('1 * 10 * int')139    assert ds.subshape[:, None] == dshape('10 * 1 * int')140def test_datashape_coerces_ints():141    assert DataShape(5, 'int32')[0] == Fixed(5)142    assert DataShape(5, 'int32')[1] == int32143def test_shape():144    assert dshape('5 * 3 * float32').shape == (5, 3)145    assert dshape('float32').shape == ()146    assert dshape('float32').measure.shape == ()147    assert dshape('?float32').measure.shape == ()148def test_option_sanitizes_strings():149    assert Option('float32').ty == dshape('float32').measure150def test_option_passes_itemsize():151    assert (dshape('?float32').measure.itemsize ==152            dshape('float32').measure.itemsize)153class TestComplexFieldNames(object):154    """155    The tests in this class should verify that the datashape parser can handle156    field names that contain strange characters like spaces, quotes, and157    backslashes158    The idea is that any given input datashape should be recoverable once we159    have created the actual dshape object.160    This test suite is by no means complete, but it does handle some of the161    more common special cases (common special? oxymoron?)162    """163    def test_spaces_01(self):164        space_dshape = "{'Unique Key': ?int64}"165        assert space_dshape == str(dshape(space_dshape))166    def test_spaces_02(self):167        big_space_dshape = """{ 'Unique Key' : ?int64, 'Created Date' : string,168'Closed Date' : string, Agency : string, 'Agency Name' : string,169'Complaint Type' : string, Descriptor : string, 'Location Type' : string,170'Incident Zip' : ?int64, 'Incident Address' : ?string, 'Street Name' : ?string,171'Cross Street 1' : ?string, 'Cross Street 2' : ?string,172'Intersection Street 1' : ?string, 'Intersection Street 2' : ?string,173'Address Type' : string, City : string, Landmark : string,174'Facility Type' : string, Status : string, 'Due Date' : string,175'Resolution Action Updated Date' : string, 'Community Board' : string,176Borough : string, 'X Coordinate (State Plane)' : ?int64,177'Y Coordinate (State Plane)' : ?int64, 'Park Facility Name' : string,178'Park Borough' : string, 'School Name' : string, 'School Number' : string,179'School Region' : string, 'School Code' : string,180'School Phone Number' : string, 'School Address' : string,181'School City' : string, 'School State' : string, 'School Zip' : string,182'School Not Found' : string, 'School or Citywide Complaint' : string,183'Vehicle Type' : string, 'Taxi Company Borough' : string,184'Taxi Pick Up Location' : string, 'Bridge Highway Name' : string,185'Bridge Highway Direction' : string, 'Road Ramp' : string,186'Bridge Highway Segment' : string, 'Garage Lot Name' : string,187'Ferry Direction' : string, 'Ferry Terminal Name' : string,188Latitude : ?float64, Longitude : ?float64, Location : string }"""189        ds1 = dshape(big_space_dshape)190        ds2 = dshape(str(ds1))191        assert str(ds1) == str(ds2)192    def test_single_quotes_01(self):193        quotes_dshape = """{ 'field \\' with \\' quotes' : string }"""194        ds1 = dshape(quotes_dshape)195        ds2 = dshape(str(ds1))196        assert str(ds1) == str(ds2)197    def test_double_quotes_01(self):198        quotes_dshape = """{ 'doublequote \" field \"' : int64 }"""199        ds1 = dshape(quotes_dshape)200        ds2 = dshape(str(ds1))201        assert str(ds1) == str(ds2)202    def test_multi_quotes_01(self):203        quotes_dshape = """{204            'field \\' with \\' quotes' : string,205            'doublequote \" field \"' : int64206        }"""207        ds1 = dshape(quotes_dshape)208        ds2 = dshape(str(ds1))209        assert str(ds1) == str(ds2)210    def test_mixed_quotes_01(self):211        quotes_dshape = """{212            'field \" with \\' quotes' : string,213            'doublequote \" field \\'' : int64214        }"""215        ds1 = dshape(quotes_dshape)216        ds2 = dshape(str(ds1))217        assert str(ds1) == str(ds2)218    def test_bad_02(self):219        bad_dshape = """{ Unique Key : int64}"""220        with pytest.raises(error.DataShapeSyntaxError):221            dshape(bad_dshape)222    def test_bad_backslashes_01(self):223        """backslashes aren't allowed in datashapes according to the definitions224        in lexer.py as of 2014-10-02. This is probably an oversight that should225        be fixed.226        """227        backslash_dshape = """{ 'field with \\\\   backslashes' : int64 }"""228        with pytest.raises(error.DataShapeSyntaxError):229            dshape(backslash_dshape)230def test_record_string():231    s = '{name_with_underscores: int32}'232    assert s.replace(' ', '') == str(dshape(s)).replace(' ', '')233def test_record_with_unicode_name_as_numpy_dtype():234    r = Record([(unicode('a'), 'int32')])235    assert r.to_numpy_dtype() == np.dtype([('a', 'i4')])236@pytest.mark.xfail(237    sys.version_info < (2, 7),238    reason='OrderedDict not supported before 2.7',239)240def test_record_from_OrderedDict():241    r = Record(OrderedDict([('a', 'int32'), ('b', 'float64')]))242    assert r.to_numpy_dtype() == np.dtype([('a', 'i4'), ('b', 'f8')])243def test_tuple_datashape_to_numpy_dtype():244    ds = dshape('5 * (int32, float32)')245    assert to_numpy_dtype(ds) == [('f0', 'i4'), ('f1', 'f4')]246def test_option_date_to_numpy():247    assert Option(Date()).to_numpy_dtype() == np.dtype('datetime64[D]')248def test_option_datetime_to_numpy():249    assert Option(DateTime()).to_numpy_dtype() == np.dtype('datetime64[us]')250@pytest.mark.parametrize('unit',251                         ['Y', 'M', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'])252def test_option_timedelta_to_numpy(unit):253    assert (Option(TimeDelta(unit=unit)).to_numpy_dtype() ==254            np.dtype('timedelta64[%s]' % unit))255def unique(x):256    seen = set()257    for el in x:258        if el not in seen:259            yield el260            seen.add(el)261@pytest.mark.parametrize('data', [list('aaaabbbccc'), [-1, 2, 3, 4, 4, 3, 5]])262def test_categorical(data):263    c = Categorical(tuple(unique(data)))264    assert list(unique(c.categories)) == list(unique(data))265    assert str(c) == 'categorical[[%s], type=%s, ordered=False]' % (266        ', '.join(map(repr, c.categories)), c.type267    )268    assert (269        repr(c) == 'Categorical(categories=[%s], type=%r, ordered=False)' % (270            ', '.join(map(repr, c.categories)), c.type271        )272    )273    assert (274        dshape("categorical[[%s], type=%s, ordered=False]" % (275            ', '.join(map(repr, c.categories)), c.type276        )) == DataShape(c)277    )278    assert (279        dshape("categorical[[%s], type=%s, ordered=False]" % (280            ', '.join(map(repr, c.categories)), c.type281        )) == DataShape(c)282    )283def test_long_categorical_repr():284    cats = list('abcdefghijklmnopqrstuvwxyz')285    c = Categorical(cats, ordered=True)286    assert str(c) == 'categorical[[%s, ...], type=%s, ordered=True]' % (287        ', '.join(map(repr, cats[:10])),288        c.type289    )290def test_duplicate_field_names_fails():291    fields = [('a', 'int32'), ('b', 'string'), ('a', 'float32')]292    with pytest.raises(ValueError):293        Record(fields)294@pytest.mark.parametrize('ds',295                         ['int64',296                          'var * float64',297                          '10 * var * int16',298                          '{a: int32, b: ?string}',299                          'var * {a: int32, b: ?string}',300                          '10 * {a: ?int32, b: var * {c: string[30]}}',301                          '{"weird col": 3 * var * 2 * ?{a: int8, b: ?uint8}}',302                          'var * {"func-y": (A) -> var * {a: 10 * float64}}',303                          'decimal[18]',304                          'var * {amount: ?decimal[9,2]}'])305def test_repr_of_eval_is_dshape(ds):306    assert eval(repr(dshape(ds))) == dshape(ds)307def test_complex_with_real_component_fails():308    with pytest.raises(TypeError):309        dshape('complex[int64]')310def test_already_registered_type():311    with pytest.raises(TypeError):312        Type.register('int64', int64)313def test_multiplication_of_dshapes():314    with pytest.raises(TypeError):315        int32 * 10316def test_ellipsis_with_typevar_repr():317    assert str(Ellipsis(typevar=TypeVar('T'))) == 'T...'318    assert repr(Ellipsis(typevar=TypeVar('T'))) == 'Ellipsis(\'T...\')'319def test_null_datashape_string():320    assert str(null) == 'null'321@pytest.mark.xfail(raises=TypeError, reason="Not yet implemented")322def test_time_with_tz_not_a_string():323    assert Time(tz=datetime.tzinfo())324@pytest.mark.xfail(raises=TypeError, reason="Not yet implemented")325def test_datetime_with_tz_not_a_string():326    assert DateTime(tz=datetime.tzinfo())327@pytest.mark.parametrize('unit', _units)328def test_timedelta_repr(unit):329    assert repr(TimeDelta(unit=unit)) == 'TimeDelta(unit=%r)' % unit330@pytest.mark.parametrize('unit', _units)331def test_timedelta_str(unit):332    assert str(TimeDelta(unit=unit)) == 'timedelta[unit=%r]' % unit333def test_unit_construction():334    with pytest.raises(TypeError):335        Units(1)336    with pytest.raises(TypeError):337        Units('kg', 1)338def test_unit_str():339    assert str(Units('kg')) == "units['kg']"340    assert (str(Units('counts/time', DataShape(uint32))) ==341            "units['counts/time', uint32]")342def test_bytes_str():343    assert str(Bytes()) == 'bytes'344def test_unsupported_string_encoding():345    with pytest.raises(ValueError):346        String(1, 'asdfasdf')347def test_all_dims_before_last():348    with pytest.raises(TypeError):349        DataShape(uint32, var, uint32)350def test_datashape_parameter_count():351    with pytest.raises(ValueError):352        DataShape()353def test_named_datashape():354    assert str(DataShape(uint32, name='myuint')) == 'myuint'355def test_subarray_invalid_index():356    with pytest.raises(IndexError):357        dshape('1 * 2 * 3 * int32').subarray(42)358def test_record_subshape_integer_index():359    ds = DataShape(Record([('a', 'int32')]))360    assert ds.subshape[0] == int32361def test_slice_subshape_negative_step():362    ds = 30 * Record([('a', 'int32')])363    assert ds.subshape[:-1] == 29 * Record([('a', 'int32')])364def test_slice_subshape_negative_start():365    ds = var * Record([('a', 'int32')])366    assert ds.subshape[-1:] == var * Record([('a', 'int32')])367def test_slice_subshape_bad_types():368    ds = var * Record([('a', 'int32')])369    with pytest.raises(TypeError):370        assert ds.subshape[1.0]371@pytest.mark.parametrize(['base', 'expected'],372                         zip([timedelta_, date_, datetime_],373                             ['timedelta64[us]', 'datetime64[D]',374                              'datetime64[us]']))375def test_option_to_numpy_dtype(base, expected):376    assert Option(base).to_numpy_dtype() == np.dtype(expected)377@pytest.mark.xfail(raises=TypeError,378                   reason=('NumPy has no notion of missing for types other '379                           'than timedelta, datetime, and date'))380@pytest.mark.parametrize('base', [int32, float64, Record([('a', uint32)])])381def test_option_to_numpy_dtype_fails(base):382    Option(base).to_numpy_dtype()383@pytest.mark.xfail(raises=NotImplementedError,384                   reason='DataShape does not know about void types (yet?)')385def test_from_numpy_dtype_fails():386    x = np.zeros(2, np.dtype([('a', 'int32')]))387    CType.from_numpy_dtype(x[0].dtype)388def test_ctype_alignment():389    assert int32.alignment == 4390def test_fixed_dimensions_must_be_positive():391    with pytest.raises(ValueError):392        Fixed(-1)393def test_fixed_comparison():394    assert Fixed(1) != 'a'395def test_typevar_must_be_upper_case():396    with pytest.raises(ValueError):397        TypeVar('t')398def test_typevar_repr():399    assert repr(TypeVar('T')) == "TypeVar(symbol='T')"400def test_funcproto_attrs():401    f = dshape('(int32, ?float64) -> {a: ?string}').measure402    assert f.restype == DataShape(Record([('a', Option(String()))]))403    assert f.argtypes == (DataShape(int32), DataShape(Option(float64)))404def test_tuple_str():405    assert str(Tuple([Option(int32), float64])) == '(?int32, float64)'406def test_to_numpy_fails():407    ds = var * int32408    with pytest.raises(TypeError):409        to_numpy(ds)410    with pytest.raises(TypeError):411        to_numpy(Option(int32))412def test_map():413    fk = Map(int32, Record([('a', int32)]))414    assert fk.key == int32415    assert fk.value == Record([('a', int32)])416    assert fk.value.dict == {'a': int32}417    assert fk.value.fields == (('a', int32),)418    with pytest.raises(TypeError):419        fk.to_numpy_dtype()420def test_map_parse():421    result = dshape("var * {b: map[int32, {a: int64}]}")422    recmeasure = Map(dshape(int32), DataShape(Record([('a', int64)])))423    assert result == DataShape(var, Record([('b', recmeasure)]))424def test_decimal_attributes():425    d1 = Decimal(18)426    assert d1.precision == 18 and d1.scale == 0427    d2 = Decimal(4, 0)428    assert d2.precision == 4 and d2.scale == 0429    d3 = Decimal(11, 2)430    assert d3.precision == 11 and d3.scale == 2431    with pytest.raises(TypeError):432        Decimal()433def test_record_literal():...test_nns.py
Source:test_nns.py  
...80@pytest.mark.parametrize("dtype", list_float_dtypes())81@pytest.mark.parametrize("device", list_devices())82def test_knn_setup(benchmark, size, dim, dtype, device):83    nns_opt = dict(knn=1, radius=0.01)84    np_a = np.array(np.random.rand(size, dim), dtype=to_numpy_dtype(dtype))85    a = o3c.Tensor(np_a, dtype=dtype, device=device)86    benchmark(NNSOps.knn_setup, a, nns_opt)87@pytest.mark.parametrize("size", list_sizes())88@pytest.mark.parametrize("dim", list_dimensions())89@pytest.mark.parametrize("dtype", list_float_dtypes())90@pytest.mark.parametrize("device", list_devices())91def test_knn_search(benchmark, size, dim, dtype, device):92    nns_opt = dict(knn=1, radius=0.01)93    np_a = np.array(np.random.rand(size, dim), dtype=to_numpy_dtype(dtype))94    np_b = np.array(np.random.rand(size, dim), dtype=to_numpy_dtype(dtype))95    a = o3c.Tensor(np_a, dtype=dtype, device=device)96    b = o3c.Tensor(np_b, dtype=dtype, device=device)97    index = NNSOps.knn_setup(a, nns_opt)98    benchmark(NNSOps.knn_search, index, b, nns_opt)99@pytest.mark.parametrize("size", list_sizes())100@pytest.mark.parametrize("dtype", list_float_dtypes())101@pytest.mark.parametrize("device", list_devices())102def test_radius_setup(benchmark, size, dtype, device):103    nns_opt = dict(knn=1, radius=0.01)104    np_a = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))105    a = o3c.Tensor(np_a, dtype=dtype, device=device)106    benchmark(NNSOps.radius_setup, a, nns_opt)107@pytest.mark.parametrize("size", list_sizes())108@pytest.mark.parametrize("dtype", list_float_dtypes())109@pytest.mark.parametrize("device", list_devices())110def test_radius_search(benchmark, size, dtype, device):111    nns_opt = dict(knn=1, radius=0.01)112    np_a = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))113    np_b = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))114    a = o3c.Tensor(np_a, dtype=dtype, device=device)115    b = o3c.Tensor(np_b, dtype=dtype, device=device)116    index = NNSOps.radius_setup(a, nns_opt)117    benchmark(NNSOps.radius_search, index, b, nns_opt)118@pytest.mark.parametrize("size", list_sizes())119@pytest.mark.parametrize("dtype", list_float_dtypes())120@pytest.mark.parametrize("device", list_devices())121def test_hybrid_setup(benchmark, size, dtype, device):122    nns_opt = dict(knn=1, radius=0.01)123    np_a = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))124    a = o3c.Tensor(np_a, dtype=dtype, device=device)125    benchmark(NNSOps.hybrid_setup, a, nns_opt)126@pytest.mark.parametrize("size", list_sizes())127@pytest.mark.parametrize("dtype", list_float_dtypes())128@pytest.mark.parametrize("device", list_devices())129def test_hybrid_search(benchmark, size, dtype, device):130    nns_opt = dict(knn=1, radius=0.01)131    np_a = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))132    np_b = np.array(np.random.rand(size, 3), dtype=to_numpy_dtype(dtype))133    a = o3c.Tensor(np_a, dtype=dtype, device=device)134    b = o3c.Tensor(np_b, dtype=dtype, device=device)135    index = NNSOps.hybrid_setup(a, nns_opt)...test_types.py
Source:test_types.py  
...8    def test_ints(self):9        types = ['int8', 'int16', 'int32', 'int64']10        for type_ in types:11            a = blaze.array(np.arange(3), dshape=type_)12            dtype = to_numpy_dtype(a.dshape)13            self.assertEqual(dtype, np.dtype(type_))14            self.assertEqual(ddesc_as_py(a.ddesc), [0, 1, 2])15    def test_uints(self):16        types = ['uint8', 'uint16', 'uint32', 'uint64']17        for type_ in types:18            a = blaze.array(np.arange(3), dshape=type_)19            dtype = to_numpy_dtype(a.dshape)20            self.assertEqual(dtype, np.dtype(type_))21            self.assertEqual(ddesc_as_py(a.ddesc), [0, 1, 2])22    def test_floats(self):23        #types = ['float16', 'float32', 'float64']24        types = ['float32', 'float64']25        for type_ in types:26            a = blaze.array(np.arange(3), dshape=type_)27            dtype = to_numpy_dtype(a.dshape)28            self.assertEqual(dtype, np.dtype(type_))29            if type_ != 'float16':30                # ddesc_as_py does not support this yet31                self.assertEqual(ddesc_as_py(a.ddesc), [0, 1, 2])32    def test_complex(self):33        types = ['complex64', 'complex128']34        for type_ in types:35            a = blaze.array(np.arange(3), dshape=type_)36            dtype = to_numpy_dtype(a.dshape)37            self.assertEqual(dtype, np.dtype(type_))38            # ddesc_as_py does not support complexes yet.....Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
