/
test-create.py
1518 lines (1165 loc) · 49 KB
/
test-create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Copyright 2018-2021 H2O.ai
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#-------------------------------------------------------------------------------
#
# Test creating a datatable from various sources
#
#-------------------------------------------------------------------------------
import math
import os
import pytest
import random
import datatable as dt
from datatable import ltype, stype
from datatable.exceptions import DatatableWarning
from datatable.internal import frame_integrity_check
from tests import list_equals, assert_equals
#-------------------------------------------------------------------------------
# Test wrong parameters
#-------------------------------------------------------------------------------
def test_types_type():
msg = r"You can pass either parameter types or type to Frame\(\) " \
r"constructor, but not both at the same time"
with pytest.raises(TypeError, match=msg):
dt.Frame(types=[], type="float32")
def test_bad_type():
msg = r"Cannot create Type object from -1"
with pytest.raises(ValueError, match=msg):
dt.Frame(type=-1)
def test_bad_types():
msg = r"Argument types in Frame\(\) constructor should be a list of " \
r"types, instead received <class 'float'>"
with pytest.raises(TypeError, match=msg):
dt.Frame([], types=2.5)
def test_unknown_arg():
msg = r"Frame\(\) constructor got an unexpected keyword argument 'dtype'"
with pytest.raises(TypeError, match=msg):
dt.Frame([1], dtype="int32")
def test_unknown_args1():
msg = r"Frame\(\) constructor got 2 unexpected keyword arguments: " \
r"'A' and 'B'"
with pytest.raises(TypeError, match=msg):
dt.Frame([1], A=1, B=2)
def test_unknown_args2():
msg = r"Frame\(\) constructor got 3 unexpected keyword arguments: " \
r"'A', 'B' and 'C'"
with pytest.raises(TypeError, match=msg):
dt.Frame([1], A=1, B=2, C=3)
def test_unknown_args3():
msg = r"Frame\(\) constructor got 4 unexpected keyword arguments: " \
r"'A', 'B', ..., 'D'"
with pytest.raises(TypeError, match=msg):
dt.Frame([1], A=1, B=2, C=3, D=4)
def test_unknown_args4():
msg = r"Frame\(\) constructor got 5 unexpected keyword arguments: " \
r"'A', 'B', ..., 'E'"
with pytest.raises(TypeError, match=msg):
dt.Frame([1], A=1, B=2, C=3, D=4, E=5)
def test_types_dict():
msg = "When parameter types is a dictionary, column names must " \
"be explicitly specified"
with pytest.raises(TypeError, match=msg):
dt.Frame([1, 2, 3], stypes={"A": float})
def test_create_from_set():
msg = "Cannot create Frame from <class 'set'>"
with pytest.raises(TypeError, match=msg):
dt.Frame({1, 13, 15, -16, -10, 7, 9, 1})
def test_wrong_source():
msg = "Cannot create a column from <class 'int'>"
with pytest.raises(TypeError, match=msg):
dt.Frame(A=[1], B=2)
def test_wrong_source_heavy():
# Repeatedly try to fail the constructor; make sure that the internal state
# does not get corrupted in the process...
for _ in range(100):
try:
dt.Frame(A=[1], B=2)
except TypeError:
pass
def test_different_column_lengths():
msg = r"Column 1 has different number of rows \(3\) than the preceding " \
r"columns \(10\)"
with pytest.raises(ValueError, match=msg):
dt.Frame([range(10), [3, 4, 6]])
def test_from_frame_as_column():
DT = dt.Frame(A=[1, 2, 3], B=[7, 4, 1])
msg = "A column cannot be constructed from a Frame with 2 columns"
with pytest.raises(ValueError, match=msg):
dt.Frame(X=DT)
#-------------------------------------------------------------------------------
# Create empty Frame
#-------------------------------------------------------------------------------
def test_create_from_nothing():
d0 = dt.Frame()
frame_integrity_check(d0)
assert d0.shape == (0, 0)
assert d0.names == tuple()
assert d0.types == []
def test_create_from_none():
d0 = dt.Frame(None)
frame_integrity_check(d0)
assert d0.shape == (0, 0)
assert d0.names == tuple()
assert d0.types == []
def test_create_from_empty_list():
d0 = dt.Frame([])
frame_integrity_check(d0)
assert d0.shape == (0, 0)
assert d0.names == tuple()
assert d0.types == []
def test_create_from_empty_list_with_params():
d0 = dt.Frame([], names=[], types=[])
frame_integrity_check(d0)
assert d0.shape == (0, 0)
d1 = dt.Frame([], type=dt.int64)
frame_integrity_check(d1)
assert d1.shape == (0, 0)
def test_create_from_nothing_with_names():
d0 = dt.Frame(names=["A", "B"])
frame_integrity_check(d0)
assert d0.shape == (0, 2)
assert d0.names == ("A", "B")
def test_create_from_empty_list_bad():
with pytest.raises(ValueError) as e:
dt.Frame([], stypes=["int32", "str32"])
assert ("The types argument contains 2 elements, which is more than the "
"number of columns being created (0)" in str(e.value))
#-------------------------------------------------------------------------------
# Create from a primitive list
#-------------------------------------------------------------------------------
def test_create_from_list():
d0 = dt.Frame([1, 2, 3])
frame_integrity_check(d0)
assert d0.shape == (3, 1)
assert d0.names == ("C0", )
assert d0.ltypes == (ltype.int, )
def test_create_from_tuple():
d2 = dt.Frame((3, 5, 6, 0))
frame_integrity_check(d2)
assert d2.shape == (4, 1)
assert d2.ltypes == (ltype.int, )
def test_create_from_range():
d0 = dt.Frame(range(8))
frame_integrity_check(d0)
assert d0.shape == (8, 1)
assert d0.to_list() == [list(range(8))]
@pytest.mark.parametrize("r", [range(1, 3), range(2, 7, 3), range(-1, 4, 2),
range(5, 2, -1), range(278, -14, -5),
range(2, 2, 2), range(2, 3, 15), range(-3, -1),
range(4, 3, 3), range(2, 3, -2),
range(4, 6, -10), range(4, 8, -2),
range(4, 2, -2)])
def test_create_from_range2(r):
DT = dt.Frame(A=r)
frame_integrity_check(DT)
assert DT.ncols == 1
assert DT.to_list()[0] == list(r)
#-------------------------------------------------------------------------------
# Create from a list of lists
#-------------------------------------------------------------------------------
def test_create_from_list_of_lists():
d1 = dt.Frame([[1, 2], [True, False], [.3, -0]], names=list("ABC"))
frame_integrity_check(d1)
assert d1.shape == (2, 3)
assert d1.names == ("A", "B", "C")
assert d1.ltypes == (ltype.int, ltype.bool, ltype.real)
def test_create_from_list_of_lists_with_stypes_dict():
d0 = dt.Frame([[4], [9], [3]], names=("a", "b", "c"), stypes={"c": float})
frame_integrity_check(d0)
assert d0.names == ("a", "b", "c")
assert d0.ltypes == (ltype.int, ltype.int, ltype.real)
assert d0.to_list() == [[4], [9], [3.0]]
def test_create_from_list_of_lists_with_stypes_dict_bad():
with pytest.raises(TypeError) as e:
dt.Frame([[4], [9], [3]], stypes={"c": float})
assert ("When parameter types is a dictionary, column names must be "
"explicitly specified" == str(e.value))
def test_create_from_list_of_ranges():
d0 = dt.Frame([range(6), range(0, 12, 2)])
frame_integrity_check(d0)
assert d0.shape == (6, 2)
assert d0.to_list() == [list(range(6)), list(range(0, 12, 2))]
def test_create_from_empty_list_of_lists():
d6 = dt.Frame([[]])
frame_integrity_check(d6)
assert d6.shape == (0, 1)
assert d6.names == ("C0", )
assert d6.ltypes == (ltype.void, )
#-------------------------------------------------------------------------------
# Create from a dict
#-------------------------------------------------------------------------------
def test_create_from_dict():
d7 = dt.Frame({"A": [1, 5, 10],
"B": [True, False, None],
"C": ["alpha", "beta", "gamma"]})
assert d7.shape == (3, 3)
assert list_equals(d7.names, ("A", "B", "C"))
assert list_equals(d7.ltypes, (ltype.int, ltype.bool, ltype.str))
frame_integrity_check(d7)
def test_create_from_dict_error():
with pytest.raises(TypeError) as e:
dt.Frame({"A": range(3), "B": list('abc')}, names=("x", "y"))
assert ("Parameter names cannot be used when constructing a Frame from "
"a dictionary" == str(e.value))
#-------------------------------------------------------------------------------
# Create from keyword arguments
#-------------------------------------------------------------------------------
def test_create_from_kwargs0():
d0 = dt.Frame(varname=[1])
frame_integrity_check(d0)
assert d0.names == ("varname",)
assert d0.to_list() == [[1]]
def test_create_from_kwargs1():
d0 = dt.Frame(A=[1, 2, 3], B=[True, None, False], C=["a", "b", "c"])
frame_integrity_check(d0)
assert list_equals(d0.names, ("A", "B", "C"))
assert list_equals(d0.to_list(), [[1, 2, 3],
[True, None, False],
["a", "b", "c"]])
def test_create_from_kwargs2():
d0 = dt.Frame(x=range(4), y=[1, 3, 8, 0], stypes=[dt.int64, dt.float32])
frame_integrity_check(d0)
assert d0.shape == (4, 2)
assert list_equals(d0.names, ("x", "y"))
assert list_equals(d0.stypes, (dt.int64, dt.float32))
assert list_equals(d0.to_list(), [[0, 1, 2, 3], [1, 3, 8, 0]])
def test_create_from_kwargs_error():
with pytest.raises(TypeError) as e:
dt.Frame(A=range(3), B=list('abc'), names=("x", "y"))
assert ("Parameter names cannot be used when constructing a Frame from "
"varkwd arguments" == str(e.value))
#-------------------------------------------------------------------------------
# Create from a typed list
#-------------------------------------------------------------------------------
def test_create_from_typed_list():
DT = dt.Frame([1, 2, 3] / dt.int16)
assert DT.stype == dt.int16
assert DT.to_list() == [[1, 2, 3]]
frame_integrity_check(DT)
def test_create_from_typed_list2():
DT = dt.Frame([[1, 2, 3] / dt.int64,
[1, 2, 3] / dt.float64])
assert DT.stypes == (dt.int64, dt.float64)
assert DT.to_list() == [[1, 2, 3]] * 2
frame_integrity_check(DT)
def test_create_from_typed_list3():
DT = dt.Frame(A=[1, 2, 3] / dt.str32,
B=[1, 2, 3] / dt.bool8,
C=[1, 2, 3] / dt.float64)
assert_equals(DT, dt.Frame(A=["1", "2", "3"], B=[True, True, True],
C=[1.0, 2.0, 3.0]))
def test_create_from_typed_list4():
DT = dt.Frame({"A": [1, 2, 3] / dt.int8})
assert_equals(DT, dt.Frame([1, 2, 3], names=["A"], stypes=[dt.int8]))
#-------------------------------------------------------------------------------
# Create from a Frame (copy)
#-------------------------------------------------------------------------------
def test_create_from_frame():
d0 = dt.Frame(A=[1, 4, 3],
B=[False, True, False],
C=["str1", "str2", "str3"])
frame_integrity_check(d0)
d1 = dt.Frame(d0)
frame_integrity_check(d1)
assert_equals(d0, d1)
# Now check that d1 is a true copy of d0, rather than reference
del d1[:, "C"]
d1.nrows = 10
assert d1.nrows == 10
assert d1.ncols == 2
assert d0.nrows == 3
assert d0.ncols == 3
def test_create_from_frame2():
d0 = dt.Frame([range(5), [7] * 5, list("owbke")])
d1 = dt.Frame(d0, names=["A", "B", "C"])
frame_integrity_check(d1)
assert d1.to_list() == d0.to_list()
assert d1.stypes == d0.stypes
assert d1.names != d0.names
assert d1.names == ("A", "B", "C")
def test_create_from_frame_error():
d0 = dt.Frame([[3], [5.78], ["af"]], names=("A", "B", "C"))
frame_integrity_check(d0)
with pytest.raises(TypeError) as e1:
dt.Frame(d0, stypes=[stype.int64, stype.float64, stype.str64])
assert ("Parameter types is not allowed when making a copy of a "
"Frame" == str(e1.value))
with pytest.raises(TypeError) as e2:
dt.Frame(d0, stypes=[stype.str32])
assert str(e1.value) == str(e2.value)
def test_create_from_column_frames():
DT0 = dt.Frame(A=range(5), B=list("dfkjd"),
C=[False, True, True, None, True])
DT1 = dt.Frame(a=DT0["A"], b=DT0["B"], c=DT0["C"])
assert DT1.names == ("a", "b", "c")
assert DT1.stypes == DT0.stypes
assert DT1.to_list() == DT0.to_list()
def test_create_from_doublestar_expansion():
DT0 = dt.Frame(A=range(3), B=["df", "qe;r", None])
DT1 = dt.Frame(D=[7.99, -12.5, 0.1], E=[None]*3)
DT = dt.Frame(**DT0, **DT1)
assert_equals(DT, dt.cbind(DT0, DT1))
#-------------------------------------------------------------------------------
# Create from a string (fread)
#-------------------------------------------------------------------------------
def test_create_from_string():
d0 = dt.Frame("""
A,B,C,D
1,2,3,boo
0,5.5,,bar
,NaN,1000,""
""")
frame_integrity_check(d0)
assert d0.names == ("A", "B", "C", "D")
assert d0.ltypes == (dt.ltype.bool, dt.ltype.real, dt.ltype.int,
dt.ltype.str)
assert d0.to_list() == [[True, False, None], [2.0, 5.5, None],
[3, None, 1000], ["boo", "bar", ""]]
def test_cannot_create_from_multiple_files(tempfile):
file1 = tempfile + ".1.csv"
file2 = tempfile + ".2.csv"
file3 = tempfile + ".3.csv"
try:
with open(file1, "w") as o1, open(file2, "w") as o2, \
open(file3, "w") as o3:
o1.write("A,B\nfoo,2\n")
o2.write("3\n4\n5\n6\n")
o3.write("qw\n1\n2\n5\n")
ff = dt.iread(tempfile + ".*.csv")
assert len(list(ff)) == 3
msg = r"fread\(\) input contains multiple sources"
with pytest.raises(IOError, match=msg):
dt.Frame(tempfile + ".*.csv")
finally:
os.remove(file1)
os.remove(file2)
#-------------------------------------------------------------------------------
# Create from a list of tuples (as rows)
#-------------------------------------------------------------------------------
def test_create_from_list_of_tuples1():
d0 = dt.Frame([(1, 2.0, "foo"),
(3, 1.5, "zee"),
(9, 0.1, "xyx"),
(0, -10, None)])
frame_integrity_check(d0)
assert d0.shape == (4, 3)
assert d0.ltypes == (ltype.int, ltype.real, ltype.str)
assert d0.to_list() == [[1, 3, 9, 0],
[2.0, 1.5, 0.1, -10.0],
["foo", "zee", "xyx", None]]
def test_create_from_list_of_tuples2():
d0 = dt.Frame([(1, 3, 5)], names=["a", "b", "c"], stypes=[int, float, str])
frame_integrity_check(d0)
assert d0.shape == (1, 3)
assert d0.ltypes == (ltype.int, ltype.real, ltype.str)
assert d0.names == ("a", "b", "c")
assert d0.to_list() == [[1], [3.0], ["5"]]
def test_create_from_list_of_tuples_bad():
with pytest.raises(TypeError) as e:
dt.Frame([(1, 2, 3), (3, 4, 5), "4, 5, 6"])
assert ("The source is not a list of tuples: element 2 is a "
"<class 'str'>" == str(e.value))
with pytest.raises(ValueError) as e:
dt.Frame([(1, 2, 3), (4, 5), (5, 6, 7)])
assert ("Misshaped rows in Frame() constructor: row 1 contains 2 elements, "
"while the previous row had 3 elements" == str(e.value))
with pytest.raises(ValueError) as e:
dt.Frame([(1, 2, 3)], names=["a", "b"])
assert ("The names argument contains 2 elements, which is less than "
"the number of columns being created (3)" == str(e.value))
with pytest.raises(ValueError) as e:
dt.Frame([(1, 2, 3)], stypes=(stype.float32,) * 10)
assert ("The types argument contains 10 elements, which is more than "
"the number of columns being created (3)" == str(e.value))
def test_create_from_list_of_namedtuples():
from collections import namedtuple
Person = namedtuple("Person", ["name", "age", "sex"])
d0 = dt.Frame([Person("Grogg", 21, "M"),
Person("Alexx", 14, "M"),
Person("Fiona", 24, "F")])
frame_integrity_check(d0)
assert d0.shape == (3, 3)
assert d0.names == ("name", "age", "sex")
assert d0.ltypes == (ltype.str, ltype.int, ltype.str)
assert d0.to_list() == [["Grogg", "Alexx", "Fiona"],
[21, 14, 24], ["M", "M", "F"]]
def test_create_from_list_of_namedtuples_names_override():
from collections import namedtuple
abc = namedtuple("ABC", ["a", "b", "c"])
d0 = dt.Frame([abc(5, 6, 7), abc(3, 2, 1)], names=["x", "y", "z"])
frame_integrity_check(d0)
assert d0.shape == (2, 3)
assert d0.names == ("x", "y", "z")
assert d0.ltypes == (ltype.int,) * 3
assert d0.to_list() == [[5, 3], [6, 2], [7, 1]]
#-------------------------------------------------------------------------------
# Create from a list of dictionaries (as rows)
#-------------------------------------------------------------------------------
def test_create_from_list_of_dicts1():
d0 = dt.Frame([{"a": 5, "b": 7, "c": "Hey"},
{"a": 99},
{"a": -4, "c": "Yay", "d": 2.17},
{"d": 1e10}, {}])
frame_integrity_check(d0)
assert d0.shape == (5, 4)
assert d0.names == ("a", "b", "c", "d")
assert d0.ltypes == (ltype.int, ltype.int, ltype.str, ltype.real)
assert d0.to_list() == [[5, 99, -4, None, None],
[7, None, None, None, None],
["Hey", None, "Yay", None, None],
[None, None, 2.17, 1e10, None]]
def test_create_from_list_of_dicts2():
d0 = dt.Frame([{"foo": 11, "bar": 34}, {"argh": 17, "foo": 4}, {"_": 0}])
frame_integrity_check(d0)
assert d0.shape == (3, 4)
assert d0.names == ("foo", "bar", "argh", "_")
assert d0.to_list() == [[11, 4, None], [34, None, None],
[None, 17, None], [None, None, 0]]
def test_create_from_list_of_dicts_with_names1():
d0 = dt.Frame([{"a": 12, "b": 77797, "c": "Rose"},
{"a": 37},
{"a": 80, "c": "Lily", "d": 3.14159},
{"d": 1.7e10}, {}],
names=["c", "a", "d", "e"])
frame_integrity_check(d0)
assert d0.shape == (5, 4)
assert d0.names == ("c", "a", "d", "e")
assert d0.ltypes == (ltype.str, ltype.int, ltype.real, ltype.void)
assert d0.to_list() == [["Rose", None, "Lily", None, None],
[12, 37, 80, None, None],
[None, None, 3.14159, 1.7e10, None],
[None, None, None, None, None]]
def test_create_from_list_of_dicts_with_names2():
d0 = dt.Frame([{"a": 5}, {"b": 6}, {"c": 11}, {}], names=[])
frame_integrity_check(d0)
assert d0.shape == (0, 0)
def test_create_from_list_of_dicts_bad1():
with pytest.raises(TypeError) as e:
dt.Frame([{"a": 5}, {"b": 6}, None, {"c": 11}], names=[])
assert ("The source is not a list of dicts: element 2 is a "
"<class 'NoneType'>" == str(e.value))
with pytest.raises(TypeError) as e:
dt.Frame([{"a": 5}, {"b": 6}, None, {"c": 11}])
assert ("The source is not a list of dicts: element 2 is a "
"<class 'NoneType'>" == str(e.value))
def test_create_from_list_of_dicts_bad2():
with pytest.raises(TypeError) as e:
dt.Frame([{"a": 11}, {1: 4}])
assert ("Invalid data in Frame() constructor: row 1 dictionary contains "
"a key of type <class 'int'>, only string keys are allowed" ==
str(e.value))
def test_create_from_list_of_dicts_bad3():
with pytest.raises(TypeError) as e:
dt.Frame([{"a": 11}, {"b": 4}], stypes=[int, int])
assert ("If the Frame() source is a list of dicts, then either the names "
"list has to be provided explicitly, or stypes parameter has "
"to be a dictionary (or missing)" == str(e.value))
#-------------------------------------------------------------------------------
# Type auto-detection
#-------------------------------------------------------------------------------
def test_auto_bool8():
d0 = dt.Frame([True, False, None])
frame_integrity_check(d0)
assert d0.stypes == (stype.bool8,)
def test_auto_int8_1():
src = [0, 1, 0, None, 0]
DT = dt.Frame(src)
frame_integrity_check(DT)
assert DT.stype == dt.int8
assert DT.to_list() == [src]
def test_auto_int8_2():
src = [1, 0, False, True, None, 1, True]
DT = dt.Frame(src)
frame_integrity_check(DT)
assert DT.stype == dt.int8
assert DT.to_list() == [[1, 0, 0, 1, None, 1, 1]]
def test_auto_int8_3(numpy):
i8 = numpy.int8
src = [i8(3), i8(5), None, 1, True, False, i8(-1)]
DT = dt.Frame(src)
frame_integrity_check(DT)
assert DT.stype == dt.int8
assert DT.to_list() == [[3, 5, None, 1, 1, 0, -1]]
def test_auto_int16_1(numpy):
i16 = numpy.int16
src = [i16(50), i16(2303), None, i16(-45)]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int16
assert d0.to_list() == [[50, 2303, None, -45]]
def test_auto_int16_2(numpy):
i16 = numpy.int16
src = [i16(1), i16(0)]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int16
assert d0.to_list() == [[1, 0]]
def test_auto_int16_3(numpy):
i8 = numpy.int8
i16 = numpy.int16
src = [i16(1), i16(0), 1, 0, i8(1), i8(0), True, False, None]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int16
assert d0.to_list() == [[1, 0, 1, 0, 1, 0, 1, 0, None]]
def test_auto_int32_1():
src = [0, 1, 2]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int32
assert d0.to_list() == [[0, 1, 2]]
def test_auto_int32_2():
src = [None, 0, 1, 44, 9548, 428570247, -12]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int32
assert d0.to_list()[0] == src
def test_auto_int32_3(np):
src = [None, 0, np.int8(0), np.int16(0), np.int32(0)]
DT = dt.Frame(src)
assert_equals(DT, dt.Frame([None, 0, 0, 0, 0] / dt.int32))
def test_auto_int64():
src = [None, 0, 1, 44, 9548, 145928450, 2245982454589145, 333, 2]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.int64
assert d0.to_list()[0] == src
def test_auto_float64():
src = [5, 12, 5.2, None, 11, 0.998, -1]
d0 = dt.Frame(src)
frame_integrity_check(d0)
assert d0.stype == dt.float64
assert d0.to_list()[0] == src
def test_auto_str32_1():
d0 = dt.Frame(["start", None, "end"])
frame_integrity_check(d0)
assert d0.stypes == (stype.str32,)
def test_auto_str32_2():
DT = dt.Frame([None, "1a", "12", "fini"])
frame_integrity_check(DT)
assert DT.stype == stype.str32
assert DT.to_list() == [[None, "1a", "12", "fini"]]
def test_auto_str32_3():
DT = dt.Frame([True, 5.75, 9, "hi", 4, False])
frame_integrity_check(DT)
assert DT.stype == stype.str32
assert DT.to_list() == [["True", "5.75", "9", "hi", "4", "False"]]
def test_auto_str64():
hello = 'hello' * 50000
d0 = dt.Frame([hello] * 10000)
frame_integrity_check(d0)
assert d0.stypes == (stype.str64,)
def test_create_large_string_column():
s = 'a' * (1 << 20)
DT = dt.Frame([s] * (1 << 12))
frame_integrity_check(DT)
assert DT.stype == dt.str64
assert DT[-1, 0] == s
def test_no_auto_object_column():
src = [3, "ha!", dt, [0]]
msg = r"Cannot create column from a python list"
with pytest.raises(TypeError, match=msg):
dt.Frame(src)
# Requesting 'object' stype explicitly should create a valid frame
DT = dt.Frame(src, stype=object)
assert DT.shape == (4, 1)
assert DT.types == [dt.Type.obj64]
assert DT.to_list() == [src]
#-------------------------------------------------------------------------------
# types= argument
#-------------------------------------------------------------------------------
def test_create_from_nones():
d0 = dt.Frame([None, None, None])
frame_integrity_check(d0)
assert d0.stypes == (stype.void, )
assert d0.shape == (3, 1)
def test_create_as_int8():
d0 = dt.Frame([1, None, -1, 1000, 2.7, "123", "boo"], type=stype.int8)
frame_integrity_check(d0)
assert d0.stypes == (stype.int8, )
assert d0.shape == (7, 1)
assert d0.to_list() == [[1, None, -1, -24, 2, 123, None]]
def test_create_as_int16():
d0 = dt.Frame([1e50, 1000, None, "27", "?", True], type=stype.int16)
frame_integrity_check(d0)
assert d0.stypes == (stype.int16, )
assert d0.shape == (6, 1)
# int(1e50) = 2407412430484045 * 2**115, which is ≡0 (mod 2**16)
assert d0.to_list() == [[0, 1000, None, 27, None, 1]]
def test_create_as_int32():
d0 = dt.Frame([1, 2, 5, 3.14, (1, 2)], type=stype.int32)
frame_integrity_check(d0)
assert d0.stypes == (stype.int32, )
assert d0.shape == (5, 1)
assert d0.to_list() == [[1, 2, 5, 3, None]]
def test_create_as_float32():
d0 = dt.Frame([1, 5, 2.6, "7.7777", -1.2e+50, 1.3e-50],
stype=stype.float32)
frame_integrity_check(d0)
assert d0.stypes == (stype.float32, )
assert d0.shape == (6, 1)
# Apparently, float "inf" converts into double "inf" when cast. Good!
assert list_equals(d0.to_list(), [[1, 5, 2.6, 7.7777, -math.inf, 0]])
def test_create_as_float64():
d0 = dt.Frame([[1, 2, 3, 4, 5, None],
[2.7, "3.1", False, "foo", 10**1000, -12**321]],
stype=float)
frame_integrity_check(d0)
assert d0.stypes == (stype.float64, stype.float64)
assert d0.shape == (6, 2)
assert d0.to_list() == [[1.0, 2.0, 3.0, 4.0, 5.0, None],
[2.7, 3.1, 0, None, math.inf, -math.inf]]
def test_create_as_str32():
d0 = dt.Frame([1, 2.7, "foo", None, (3, 4)], stype=stype.str32)
frame_integrity_check(d0)
assert d0.stypes == (stype.str32, )
assert d0.shape == (5, 1)
assert d0.to_list() == [["1", "2.7", "foo", None, "(3, 4)"]]
def test_create_as_str64():
d0 = dt.Frame(range(10), type=dt.Type.str64)
frame_integrity_check(d0)
assert d0.stypes == (stype.str64, )
assert d0.shape == (10, 1)
assert d0.to_list() == [[str(n) for n in range(10)]]
@pytest.mark.parametrize("st", [stype.int8, stype.int16, stype.int64,
stype.float32, stype.float64, stype.str32])
def test_create_range_as_stype(st):
d0 = dt.Frame(range(10), stype=st)
frame_integrity_check(d0)
assert d0.stypes == (st,)
if st == stype.str32:
assert d0.to_list()[0] == [str(x) for x in range(10)]
else:
assert d0.to_list()[0] == list(range(10))
@pytest.mark.parametrize('t', [stype.float64, dt.Type.float64, float, 'double',
'float64'])
def test_create_from_various_types(t):
DT = dt.Frame(range(5), type=t)
frame_integrity_check(DT)
assert DT.type == dt.Type.float64
assert_equals(DT, dt.Frame([0.0, 1.0, 2.0, 3.0, 4.0]))
def test_create_from_numpy_dtype1(np):
DT = dt.Frame(range(5), type=np.dtype('float64'))
assert_equals(DT, dt.Frame([0.0, 1.0, 2.0, 3.0, 4.0]))
def test_create_from_numpy_dtype2(np):
DT = dt.Frame(range(5), type=np.float64)
assert_equals(DT, dt.Frame([0.0, 1.0, 2.0, 3.0, 4.0]))
def test_create_from_arrow_type(pa):
DT = dt.Frame(range(5), type=pa.float64())
assert_equals(DT, dt.Frame([0.0, 1.0, 2.0, 3.0, 4.0]))
#-------------------------------------------------------------------------------
# Create with names
#-------------------------------------------------------------------------------
def test_create_names0():
d1 = dt.Frame(range(10), names=["xyz"])
d2 = dt.Frame(range(10), names=("xyz",))
frame_integrity_check(d1)
frame_integrity_check(d2)
assert d1.shape == d2.shape == (10, 1)
assert d1.names == d2.names == ("xyz",)
def test_create_names_bad1():
with pytest.raises(ValueError) as e:
dt.Frame(range(10), names=["a", "b"])
assert ("The names argument contains 2 elements, which is more than the "
"number of columns being created (1)" == str(e.value))
def test_create_names_bad2():
with pytest.raises(TypeError) as e:
dt.Frame([[1], [2], [3]], names="xyz")
assert ("Argument names in Frame() constructor should be a list of "
"strings, instead received <class 'str'>" == str(e.value))
def test_create_names_bad3():
with pytest.raises(TypeError) as e:
dt.Frame(range(5), names={"x": 1})
assert ("Argument names in Frame() constructor should be a list of "
"strings, instead received <class 'dict'>" == str(e.value))
def test_create_names_bad4():
with pytest.raises(TypeError) as e:
dt.Frame(range(5), names=[3])
assert ("Invalid names list: element 0 is not a string"
in str(e.value))
#-------------------------------------------------------------------------------
# Create from Pandas
#-------------------------------------------------------------------------------
def test_create_from_pandas(pandas):
src = {"A": [2, 5, 8], "B": ["e", "r", "qq"]}
p = pandas.DataFrame(src)
d = dt.Frame(p)
frame_integrity_check(d)
assert d.shape == (3, 2)
assert d.to_dict() == src
def test_create_from_pandas2(pandas, numpy):
p = pandas.DataFrame(numpy.ones((3, 5)))
d = dt.Frame(p)
frame_integrity_check(d)
assert d.shape == (3, 5)
assert d.names == ("0", "1", "2", "3", "4")
def test_create_from_pandas_series(pandas):
src = [1, 5, 9, -12, 0, 123]
p = pandas.Series(src)
d = dt.Frame(p)
frame_integrity_check(d)
assert d.shape == (len(src), 1)
assert d.to_list() == [src]
def test_create_from_pandas_with_names(pandas):
p = pandas.DataFrame({"A": [2, 5, 8], "B": ["e", "r", "qq"]})
d = dt.Frame(p, names=["miniature", "miniscule"])
frame_integrity_check(d)
assert d.shape == (3, 2)
assert list_equals(d.names, ("miniature", "miniscule"))
def test_create_from_pandas_series_with_names(pandas):
p = pandas.Series([10000, 5, 19, -12])
d = dt.Frame(p, names=["ha!"])
frame_integrity_check(d)
assert d.shape == (4, 1)
assert d.names == ("ha!", )
assert d.to_list() == [[10000, 5, 19, -12]]
def test_create_from_pandas_float16_series(pandas):
src = [1.5, 2.6, 7.8]
p = pandas.Series(src, dtype="float16")
d = dt.Frame(p)
frame_integrity_check(d)
assert d.stypes == (stype.float32, )
assert d.shape == (3, 1)
# The precision of `float16`s is too low for `list_equals()` method.
res = d.to_list()[0]
assert all(abs(src[i] - res[i]) < 1e-3 for i in range(3))
def test_create_from_pandas_float16_dataframe(pandas):
p = pandas.DataFrame([[1, 3, 5], [7, 8, 9]], dtype="float16")
d = dt.Frame(p)
frame_integrity_check(d)
assert d.stypes == (stype.float32,) * 3
assert d.shape == (2, 3)
def test_create_from_pandas_issue1235(pandas):
df = dt.fread("A\n" + "\U00010000" * 50).to_pandas()
table = dt.Frame(df)
frame_integrity_check(table)
assert table.shape == (1, 1)
assert table[0, 0] == "\U00010000" * 50