@@ -1976,7 +1976,7 @@ def test_constructor_cast_failure(self):
1976
1976
df ['foo' ] = np .ones ((4 ,2 )).tolist ()
1977
1977
1978
1978
# 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 )))
1980
1980
1981
1981
# this is ok
1982
1982
df ['foo2' ] = np .ones ((4 ,2 )).tolist ()
@@ -2135,6 +2135,51 @@ def test_constructor_dict(self):
2135
2135
frame = DataFrame ({'A' : [], 'B' : []}, columns = ['A' , 'B' ])
2136
2136
self .assert_ (frame .index .equals (Index ([])))
2137
2137
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
+
2138
2183
def test_constructor_subclass_dict (self ):
2139
2184
# Test for passing dict subclass to constructor
2140
2185
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):
3545
3590
assert (df1 .index .equals (Index (df .C )))
3546
3591
3547
3592
# should fail
3548
- self .assertRaises (Exception , DataFrame .from_records , df , index = [2 ])
3593
+ self .assertRaises (ValueError , DataFrame .from_records , df , index = [2 ])
3549
3594
self .assertRaises (KeyError , DataFrame .from_records , df , index = 2 )
3550
3595
3551
3596
def test_from_records_non_tuple (self ):
0 commit comments