def test_cast_bool_to_str(): DT = dt.Frame(P=[False, None, True, True, False, True]) assert DT.stypes == (dt.bool8, ) RES = DT[:, [dt.str32(f.P), dt.str64(f.P)]] assert RES.stypes == (dt.str32, dt.str64) ans = ["False", None, "True", "True", "False", "True"] assert RES.to_list() == [ans, ans]
def test_cast_obj_to_str(): src = [noop, "Hello!", ..., {}, dt, print, None] DT = dt.Frame(src) assert DT.stypes == (dt.obj64, ) RES = DT[:, [dt.str32(f[0]), dt.str64(f[0])]] frame_integrity_check(RES) ans = [str(x) for x in src] ans[-1] = None assert RES.to_list() == [ans, ans]
def test_cast_float_to_str(source_stype): DT = dt.Frame(J=[3.5, 7.049, -3.18, math.inf, math.nan, 1.0, -math.inf, 1e16, 0], stype=source_stype) assert DT.stypes == (source_stype,) RES = DT[:, [dt.str32(f.J), dt.str64(f.J)]] frame_integrity_check(RES) ans = ["3.5", "7.049", "-3.18", "inf", None, "1.0", "-inf", "1.0e+16", "0.0"] assert RES.to_list() == [ans, ans]
def test_cast_huge_to_str(): # Test that converting a huge column into str would properly overflow it # into str64 type. See issue #1695 # This test takes up to 2s to run (or up to 5s if doing an integrity check) DT = dt.repeat(dt.Frame(BIG=["ABCDEFGHIJ" * 100000]), 3000) assert DT.stypes == (dt.str32, ) RES = DT[:, dt.str32(f.BIG)] assert RES.stypes == (dt.str64, ) assert RES[-1, 0] == DT[0, 0]
def test_cast_int_to_str(source_stype): DT = dt.Frame([None, 0, -3, 189, 77, 14, None, 394831, -52939047130424957], stype=source_stype) assert DT.stypes == (source_stype, ) RES = DT[:, [dt.str32(f.C0), dt.str64(f.C0)]] frame_integrity_check(RES) assert RES.stypes == (dt.str32, dt.str64) assert RES.shape == (DT.nrows, 2) ans = [None if v is None else str(v) for v in DT.to_list()[0]] assert RES.to_list()[0] == ans
def test_cast_huge_to_str(): # Test that converting a huge column into str would properly overflow it # into str64 type. See issue #1695 # This test takes up to 8s to run n = 300000000 DT = dt.Frame(BIG=range(n)) RES = DT[:, dt.str32(f.BIG)] assert RES.stype == dt.str64 assert RES[-1, 0] == str(n - 1) assert RES[3194870, 0] == "3194870"
def test_cast_int_to_str(stype0): dt0 = dt.Frame( [None, 0, -3, 189, 77, 14, None, 394831, -52939047130424957], stype=stype0) dt1 = dt0[:, [dt.str32(f.C0), dt.str64(f.C0)]] dt1.internal.check() assert dt1.stypes == (dt.str32, dt.str64) assert dt1.shape == (dt0.nrows, 2) ans = [None if v is None else str(v) for v in dt0.topython()[0]] assert dt1.topython()[0] == ans
def test_cast_to_str(src): def to_str(x): if x is None: return None if isinstance(x, bool): return str(int(x)) # if isinstance(x, float) and math.isnan(x): return None return str(x) dt0 = dt.Frame(src) dt1 = dt0[:, [dt.str32(f[i]) for i in range(dt0.ncols)]] dt2 = dt0[:, [dt.str64(f[i]) for i in range(dt0.ncols)]] dt1.internal.check() dt2.internal.check() assert dt1.stypes == (dt.str32, ) * dt0.ncols assert dt2.stypes == (dt.str64, ) * dt0.ncols assert dt1.topython()[0] == [to_str(x) for x in src]