@@ -871,7 +871,7 @@ def __len__(self, n):
871871 # GH 4297
872872 # support Array
873873 import array
874- result = DataFrame . from_items ([( 'A' , array .array ('i' , range (10 )))] )
874+ result = DataFrame ({ 'A' : array .array ('i' , range (10 ))} )
875875 expected = DataFrame ({'A' : list (range (10 ))})
876876 tm .assert_frame_equal (result , expected , check_dtype = False )
877877
@@ -1175,44 +1175,55 @@ def test_constructor_manager_resize(self):
11751175
11761176 def test_constructor_from_items (self ):
11771177 items = [(c , self .frame [c ]) for c in self .frame .columns ]
1178- recons = DataFrame .from_items (items )
1178+ with tm .assert_produces_warning (FutureWarning ,
1179+ check_stacklevel = False ):
1180+ recons = DataFrame .from_items (items )
11791181 tm .assert_frame_equal (recons , self .frame )
11801182
11811183 # pass some columns
1182- recons = DataFrame .from_items (items , columns = ['C' , 'B' , 'A' ])
1184+ with tm .assert_produces_warning (FutureWarning ,
1185+ check_stacklevel = False ):
1186+ recons = DataFrame .from_items (items , columns = ['C' , 'B' , 'A' ])
11831187 tm .assert_frame_equal (recons , self .frame .loc [:, ['C' , 'B' , 'A' ]])
11841188
11851189 # orient='index'
11861190
11871191 row_items = [(idx , self .mixed_frame .xs (idx ))
11881192 for idx in self .mixed_frame .index ]
1189-
1190- recons = DataFrame .from_items (row_items ,
1191- columns = self .mixed_frame .columns ,
1192- orient = 'index' )
1193+ with tm .assert_produces_warning (FutureWarning ,
1194+ check_stacklevel = False ):
1195+ recons = DataFrame .from_items (row_items ,
1196+ columns = self .mixed_frame .columns ,
1197+ orient = 'index' )
11931198 tm .assert_frame_equal (recons , self .mixed_frame )
11941199 assert recons ['A' ].dtype == np .float64
11951200
11961201 with tm .assert_raises_regex (TypeError ,
11971202 "Must pass columns with "
11981203 "orient='index'" ):
1199- DataFrame .from_items (row_items , orient = 'index' )
1204+ with tm .assert_produces_warning (FutureWarning ,
1205+ check_stacklevel = False ):
1206+ DataFrame .from_items (row_items , orient = 'index' )
12001207
12011208 # orient='index', but thar be tuples
12021209 arr = construct_1d_object_array_from_listlike (
12031210 [('bar' , 'baz' )] * len (self .mixed_frame ))
12041211 self .mixed_frame ['foo' ] = arr
12051212 row_items = [(idx , list (self .mixed_frame .xs (idx )))
12061213 for idx in self .mixed_frame .index ]
1207- recons = DataFrame .from_items (row_items ,
1208- columns = self .mixed_frame .columns ,
1209- orient = 'index' )
1214+ with tm .assert_produces_warning (FutureWarning ,
1215+ check_stacklevel = False ):
1216+ recons = DataFrame .from_items (row_items ,
1217+ columns = self .mixed_frame .columns ,
1218+ orient = 'index' )
12101219 tm .assert_frame_equal (recons , self .mixed_frame )
12111220 assert isinstance (recons ['foo' ][0 ], tuple )
12121221
1213- rs = DataFrame .from_items ([('A' , [1 , 2 , 3 ]), ('B' , [4 , 5 , 6 ])],
1214- orient = 'index' ,
1215- columns = ['one' , 'two' , 'three' ])
1222+ with tm .assert_produces_warning (FutureWarning ,
1223+ check_stacklevel = False ):
1224+ rs = DataFrame .from_items ([('A' , [1 , 2 , 3 ]), ('B' , [4 , 5 , 6 ])],
1225+ orient = 'index' ,
1226+ columns = ['one' , 'two' , 'three' ])
12161227 xp = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], index = ['A' , 'B' ],
12171228 columns = ['one' , 'two' , 'three' ])
12181229 tm .assert_frame_equal (rs , xp )
@@ -1222,12 +1233,28 @@ def test_constructor_from_items_scalars(self):
12221233 with tm .assert_raises_regex (ValueError ,
12231234 r'The value in each \(key, value\) '
12241235 'pair must be an array, Series, or dict' ):
1225- DataFrame .from_items ([('A' , 1 ), ('B' , 4 )])
1236+ with tm .assert_produces_warning (FutureWarning ,
1237+ check_stacklevel = False ):
1238+ DataFrame .from_items ([('A' , 1 ), ('B' , 4 )])
12261239
12271240 with tm .assert_raises_regex (ValueError ,
12281241 r'The value in each \(key, value\) '
12291242 'pair must be an array, Series, or dict' ):
1230- DataFrame .from_items ([('A' , 1 ), ('B' , 2 )], columns = ['col1' ],
1243+ with tm .assert_produces_warning (FutureWarning ,
1244+ check_stacklevel = False ):
1245+ DataFrame .from_items ([('A' , 1 ), ('B' , 2 )], columns = ['col1' ],
1246+ orient = 'index' )
1247+
1248+ def test_from_items_deprecation (self ):
1249+ # GH 17320
1250+ with tm .assert_produces_warning (FutureWarning ,
1251+ check_stacklevel = False ):
1252+ DataFrame .from_items ([('A' , [1 , 2 , 3 ]), ('B' , [4 , 5 , 6 ])])
1253+
1254+ with tm .assert_produces_warning (FutureWarning ,
1255+ check_stacklevel = False ):
1256+ DataFrame .from_items ([('A' , [1 , 2 , 3 ]), ('B' , [4 , 5 , 6 ])],
1257+ columns = ['col1' , 'col2' , 'col3' ],
12311258 orient = 'index' )
12321259
12331260 def test_constructor_mix_series_nonseries (self ):
@@ -1256,13 +1283,13 @@ def test_constructor_column_duplicates(self):
12561283
12571284 tm .assert_frame_equal (df , edf )
12581285
1259- idf = DataFrame .from_items (
1260- [('a' , [8 ]), ('a' , [5 ])], columns = ['a' , 'a' ])
1286+ idf = DataFrame .from_records ([(8 , 5 )],
1287+ columns = ['a' , 'a' ])
1288+
12611289 tm .assert_frame_equal (idf , edf )
12621290
1263- pytest .raises (ValueError , DataFrame .from_items ,
1264- [('a' , [8 ]), ('a' , [5 ]), ('b' , [6 ])],
1265- columns = ['b' , 'a' , 'a' ])
1291+ pytest .raises (ValueError , DataFrame .from_dict ,
1292+ OrderedDict ([('b' , 8 ), ('a' , 5 ), ('a' , 6 )]))
12661293
12671294 def test_constructor_empty_with_string_dtype (self ):
12681295 # GH 9428
0 commit comments