@@ -275,8 +275,50 @@ def test_constructor_dict(self):
275
275
with tm .assert_raises_regex (ValueError , msg ):
276
276
DataFrame ({'a' : 0.7 }, columns = ['a' ])
277
277
278
- with tm .assert_raises_regex (ValueError , msg ):
279
- DataFrame ({'a' : 0.7 }, columns = ['b' ])
278
+ # No reason to raise if item is not used:
279
+ result = DataFrame ({'a' : 0.7 }, columns = ['b' ])
280
+ expected = DataFrame (columns = ['b' ])
281
+ tm .assert_frame_equal (result , expected )
282
+
283
+ @pytest .mark .parametrize ("value" , [2 , np .nan , None , float ('nan' )])
284
+ def test_constructor_dict_nan_key (self , value ):
285
+ # GH 18455
286
+ cols = [1 , value , 3 ]
287
+ idx = ['a' , value ]
288
+ values = [[0 , 3 ], [1 , 4 ], [2 , 5 ]]
289
+ data = {cols [c ]: pd .Series (values [c ], index = idx ) for c in range (3 )}
290
+ result = pd .DataFrame (data ).sort_values (1 ).sort_values ('a' , axis = 1 )
291
+ expected = pd .DataFrame (np .arange (6 ).reshape (2 , 3 ),
292
+ index = idx , columns = cols )
293
+ tm .assert_frame_equal (result , expected )
294
+
295
+ result = pd .DataFrame (data , index = idx ).sort_values ('a' , axis = 1 )
296
+ tm .assert_frame_equal (result , expected )
297
+
298
+ result = pd .DataFrame (data , index = idx , columns = cols )
299
+ tm .assert_frame_equal (result , expected )
300
+
301
+ @pytest .mark .xfail (reason = 'GH 18485 comparison fails on MI with NaNs)' )
302
+ @pytest .mark .parametrize ("value" , [np .nan , None , float ('nan' )])
303
+ def test_constructor_dict_nan_tuple_key (self , value ):
304
+ # GH 18455
305
+ cols = Index ([(11 , 21 ), (value , 22 ), (13 , value )])
306
+ idx = Index ([('a' , value ), (value , 2 )])
307
+ values = [[0 , 3 ], [1 , 4 ], [2 , 5 ]]
308
+ data = {cols [c ]: pd .Series (values [c ], index = idx ) for c in range (3 )}
309
+ result = (DataFrame (data )
310
+ .sort_values ((11 , 21 ))
311
+ .sort_values (('a' , value ), axis = 1 ))
312
+ expected = pd .DataFrame (np .arange (6 ).reshape (2 , 3 ),
313
+ index = idx , columns = cols )
314
+ tm .assert_frame_equal (result , expected )
315
+
316
+ result = pd .DataFrame (data , index = idx ).sort_values (('a' , value ),
317
+ axis = 1 )
318
+ tm .assert_frame_equal (result , expected )
319
+
320
+ result = pd .DataFrame (data , index = idx , columns = cols )
321
+ tm .assert_frame_equal (result , expected )
280
322
281
323
def test_constructor_multi_index (self ):
282
324
# GH 4078
@@ -723,15 +765,15 @@ def test_constructor_corner(self):
723
765
724
766
# does not error but ends up float
725
767
df = DataFrame (index = lrange (10 ), columns = ['a' , 'b' ], dtype = int )
726
- assert df .values .dtype == np .object_
768
+ assert df .values .dtype == np .dtype ( 'float64' )
727
769
728
770
# #1783 empty dtype object
729
771
df = DataFrame ({}, columns = ['foo' , 'bar' ])
730
772
assert df .values .dtype == np .object_
731
773
732
774
df = DataFrame ({'b' : 1 }, index = lrange (10 ), columns = list ('abc' ),
733
775
dtype = int )
734
- assert df .values .dtype == np .object_
776
+ assert df .values .dtype == np .dtype ( 'float64' )
735
777
736
778
def test_constructor_scalar_inference (self ):
737
779
data = {'int' : 1 , 'bool' : True ,
0 commit comments