@@ -1976,7 +1976,7 @@ def test_constructor_cast_failure(self):
19761976 df ['foo' ] = np .ones ((4 ,2 )).tolist ()
19771977
19781978 # this is not ok
1979- self .assertRaises (AssertionError , df .__setitem__ , tuple (['test' ]), np .ones ((4 ,2 )))
1979+ self .assertRaises (ValueError , df .__setitem__ , tuple (['test' ]), np .ones ((4 ,2 )))
19801980
19811981 # this is ok
19821982 df ['foo2' ] = np .ones ((4 ,2 )).tolist ()
@@ -2135,6 +2135,51 @@ def test_constructor_dict(self):
21352135 frame = DataFrame ({'A' : [], 'B' : []}, columns = ['A' , 'B' ])
21362136 self .assert_ (frame .index .equals (Index ([])))
21372137
2138+ def test_constructor_error_msgs (self ):
2139+
2140+ # mix dict and array, wrong size
2141+ try :
2142+ DataFrame ({'A' : {'a' : 'a' , 'b' : 'b' },
2143+ 'B' : ['a' , 'b' , 'c' ]})
2144+ except (Exception ), detail :
2145+ self .assert_ (type (detail ) == ValueError )
2146+ self .assert_ ("Mixing dicts with non-Series may lead to ambiguous ordering." in str (detail ))
2147+
2148+ # wrong size ndarray, GH 3105
2149+ from pandas import date_range
2150+ try :
2151+ DataFrame (np .arange (12 ).reshape ((4 , 3 )), columns = ['foo' , 'bar' , 'baz' ],
2152+ index = date_range ('2000-01-01' , periods = 3 ))
2153+ except (Exception ), detail :
2154+ self .assert_ (type (detail ) == ValueError )
2155+ self .assert_ (str (detail ).startswith ("Shape of passed values is (3, 4), indices imply (3, 3)" ))
2156+
2157+ # higher dim raise exception
2158+ try :
2159+ DataFrame (np .zeros ((3 , 3 , 3 )), columns = ['A' , 'B' , 'C' ], index = [1 ])
2160+ except (Exception ), detail :
2161+ self .assert_ (type (detail ) == ValueError )
2162+ self .assert_ ("Must pass 2-d input" in str (detail ))
2163+
2164+ # wrong size axis labels
2165+ try :
2166+ DataFrame (np .random .rand (2 ,3 ), columns = ['A' , 'B' , 'C' ], index = [1 ])
2167+ except (Exception ), detail :
2168+ self .assert_ (type (detail ) == ValueError )
2169+ self .assert_ (str (detail ).startswith ("Shape of passed values is (3, 2), indices imply (3, 1)" ))
2170+
2171+ try :
2172+ DataFrame (np .random .rand (2 ,3 ), columns = ['A' , 'B' ], index = [1 , 2 ])
2173+ except (Exception ), detail :
2174+ self .assert_ (type (detail ) == ValueError )
2175+ self .assert_ (str (detail ).startswith ("Shape of passed values is (3, 2), indices imply (2, 2)" ))
2176+
2177+ try :
2178+ DataFrame ({'a' : False , 'b' : True })
2179+ except (Exception ), detail :
2180+ self .assert_ (type (detail ) == ValueError )
2181+ self .assert_ ("If use all scalar values, must pass index" in str (detail ))
2182+
21382183 def test_constructor_subclass_dict (self ):
21392184 # Test for passing dict subclass to constructor
21402185 data = {'col1' : tm .TestSubDict ((x , 10.0 * x ) for x in xrange (10 )),
@@ -3545,7 +3590,7 @@ def test_from_records_bad_index_column(self):
35453590 assert (df1 .index .equals (Index (df .C )))
35463591
35473592 # should fail
3548- self .assertRaises (Exception , DataFrame .from_records , df , index = [2 ])
3593+ self .assertRaises (ValueError , DataFrame .from_records , df , index = [2 ])
35493594 self .assertRaises (KeyError , DataFrame .from_records , df , index = 2 )
35503595
35513596 def test_from_records_non_tuple (self ):
0 commit comments