@@ -871,7 +871,7 @@ def __len__(self, n):
871
871
# GH 4297
872
872
# support Array
873
873
import array
874
- result = DataFrame . from_items ([( 'A' , array .array ('i' , range (10 )))] )
874
+ result = DataFrame ({ 'A' : array .array ('i' , range (10 ))} )
875
875
expected = DataFrame ({'A' : list (range (10 ))})
876
876
tm .assert_frame_equal (result , expected , check_dtype = False )
877
877
@@ -1175,44 +1175,55 @@ def test_constructor_manager_resize(self):
1175
1175
1176
1176
def test_constructor_from_items (self ):
1177
1177
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 )
1179
1181
tm .assert_frame_equal (recons , self .frame )
1180
1182
1181
1183
# 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' ])
1183
1187
tm .assert_frame_equal (recons , self .frame .loc [:, ['C' , 'B' , 'A' ]])
1184
1188
1185
1189
# orient='index'
1186
1190
1187
1191
row_items = [(idx , self .mixed_frame .xs (idx ))
1188
1192
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' )
1193
1198
tm .assert_frame_equal (recons , self .mixed_frame )
1194
1199
assert recons ['A' ].dtype == np .float64
1195
1200
1196
1201
with tm .assert_raises_regex (TypeError ,
1197
1202
"Must pass columns with "
1198
1203
"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' )
1200
1207
1201
1208
# orient='index', but thar be tuples
1202
1209
arr = construct_1d_object_array_from_listlike (
1203
1210
[('bar' , 'baz' )] * len (self .mixed_frame ))
1204
1211
self .mixed_frame ['foo' ] = arr
1205
1212
row_items = [(idx , list (self .mixed_frame .xs (idx )))
1206
1213
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' )
1210
1219
tm .assert_frame_equal (recons , self .mixed_frame )
1211
1220
assert isinstance (recons ['foo' ][0 ], tuple )
1212
1221
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' ])
1216
1227
xp = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], index = ['A' , 'B' ],
1217
1228
columns = ['one' , 'two' , 'three' ])
1218
1229
tm .assert_frame_equal (rs , xp )
@@ -1222,12 +1233,28 @@ def test_constructor_from_items_scalars(self):
1222
1233
with tm .assert_raises_regex (ValueError ,
1223
1234
r'The value in each \(key, value\) '
1224
1235
'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 )])
1226
1239
1227
1240
with tm .assert_raises_regex (ValueError ,
1228
1241
r'The value in each \(key, value\) '
1229
1242
'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' ],
1231
1258
orient = 'index' )
1232
1259
1233
1260
def test_constructor_mix_series_nonseries (self ):
@@ -1256,13 +1283,13 @@ def test_constructor_column_duplicates(self):
1256
1283
1257
1284
tm .assert_frame_equal (df , edf )
1258
1285
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
+
1261
1289
tm .assert_frame_equal (idf , edf )
1262
1290
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 )]))
1266
1293
1267
1294
def test_constructor_empty_with_string_dtype (self ):
1268
1295
# GH 9428
0 commit comments