@@ -2144,8 +2144,8 @@ def test_scalar(self):
21442144 def test_basic (self ):
21452145 data1 = [3 , 5 , 1 , 10 , 23 , 3 , 2 , 6 , 8 , 6 , 10 , 6 ]
21462146 vals = stats .mode (data1 )
2147- assert_equal (vals [0 ][ 0 ] , 6 )
2148- assert_equal (vals [1 ][ 0 ] , 3 )
2147+ assert_equal (vals [0 ], 6 )
2148+ assert_equal (vals [1 ], 3 )
21492149
21502150 def test_axes (self ):
21512151 data1 = [10 , 10 , 30 , 40 ]
@@ -2156,16 +2156,16 @@ def test_axes(self):
21562156 arr = np .array ([data1 , data2 , data3 , data4 , data5 ])
21572157
21582158 vals = stats .mode (arr , axis = None )
2159- assert_equal (vals [0 ], np .array ([ 30 ] ))
2160- assert_equal (vals [1 ], np .array ([ 8 ] ))
2159+ assert_equal (vals [0 ], np .array (30 ))
2160+ assert_equal (vals [1 ], np .array (8 ))
21612161
21622162 vals = stats .mode (arr , axis = 0 )
2163- assert_equal (vals [0 ], np .array ([[ 10 , 10 , 30 , 30 ] ]))
2164- assert_equal (vals [1 ], np .array ([[ 2 , 3 , 3 , 2 ] ]))
2163+ assert_equal (vals [0 ], np .array ([10 , 10 , 30 , 30 ]))
2164+ assert_equal (vals [1 ], np .array ([2 , 3 , 3 , 2 ]))
21652165
21662166 vals = stats .mode (arr , axis = 1 )
2167- assert_equal (vals [0 ], np .array ([[ 10 ], [ 10 ], [ 20 ], [ 30 ], [ 30 ] ]))
2168- assert_equal (vals [1 ], np .array ([[ 2 ], [ 4 ], [ 3 ], [ 4 ], [ 3 ] ]))
2167+ assert_equal (vals [0 ], np .array ([10 , 10 , 20 , 30 , 30 ]))
2168+ assert_equal (vals [1 ], np .array ([2 , 4 , 3 , 4 , 3 ]))
21692169
21702170 @pytest .mark .parametrize ('axis' , np .arange (- 4 , 0 ))
21712171 def test_negative_axes_gh_15375 (self , axis ):
@@ -2178,16 +2178,16 @@ def test_negative_axes_gh_15375(self, axis):
21782178 def test_strings (self ):
21792179 data1 = ['rain' , 'showers' , 'showers' ]
21802180 vals = stats .mode (data1 )
2181- assert_equal (vals [0 ][ 0 ] , 'showers' )
2182- assert_equal (vals [1 ][ 0 ] , 2 )
2181+ assert_equal (vals [0 ], 'showers' )
2182+ assert_equal (vals [1 ], 2 )
21832183
21842184 def test_mixed_objects (self ):
21852185 objects = [10 , True , np .nan , 'hello' , 10 ]
21862186 arr = np .empty ((5 ,), dtype = object )
21872187 arr [:] = objects
21882188 vals = stats .mode (arr )
2189- assert_equal (vals [0 ][ 0 ] , 10 )
2190- assert_equal (vals [1 ][ 0 ] , 2 )
2189+ assert_equal (vals [0 ], 10 )
2190+ assert_equal (vals [1 ], 2 )
21912191
21922192 def test_objects (self ):
21932193 # Python objects must be sortable (le + eq) and have ne defined
@@ -2215,8 +2215,8 @@ def __hash__(self):
22152215 assert_equal (np .unique (arr ).shape , (4 ,))
22162216 vals = stats .mode (arr )
22172217
2218- assert_equal (vals [0 ][ 0 ] , Point (2 ))
2219- assert_equal (vals [1 ][ 0 ] , 4 )
2218+ assert_equal (vals [0 ], Point (2 ))
2219+ assert_equal (vals [1 ], 4 )
22202220
22212221 def test_mode_result_attributes (self ):
22222222 data1 = [3 , 5 , 1 , 10 , 23 , 3 , 2 , 6 , 8 , 6 , 10 , 6 ]
@@ -2245,21 +2245,32 @@ def test_mode_nan(self):
22452245 ])
22462246 def test_smallest_equal (self , data ):
22472247 result = stats .mode (data , nan_policy = 'omit' )
2248- assert_equal (result [0 ][ 0 ] , 1 )
2248+ assert_equal (result [0 ], 1 )
22492249
22502250 def test_obj_arrays_ndim (self ):
22512251 # regression test for gh-9645: `mode` fails for object arrays w/ndim > 1
22522252 data = [['Oxidation' ], ['Oxidation' ], ['Polymerization' ], ['Reduction' ]]
22532253 ar = np .array (data , dtype = object )
22542254 m = stats .mode (ar , axis = 0 )
2255- assert np .all (m .mode == 'Oxidation' ) and m .mode .shape == (1 , 1 )
2256- assert np .all (m .count == 2 ) and m .count .shape == (1 , 1 )
2255+ assert np .all (m .mode == 'Oxidation' ) and m .mode .shape == (1 ,)
2256+ assert np .all (m .count == 2 ) and m .count .shape == (1 ,)
22572257
22582258 data1 = data + [[np .nan ]]
22592259 ar1 = np .array (data1 , dtype = object )
22602260 m = stats .mode (ar1 , axis = 0 )
2261- assert np .all (m .mode == 'Oxidation' ) and m .mode .shape == (1 , 1 )
2262- assert np .all (m .count == 2 ) and m .count .shape == (1 , 1 )
2261+ assert np .all (m .mode == 'Oxidation' ) and m .mode .shape == (1 ,)
2262+ assert np .all (m .count == 2 ) and m .count .shape == (1 ,)
2263+
2264+ @pytest .mark .parametrize ('axis' , np .arange (- 3 , 3 ))
2265+ @pytest .mark .parametrize ('dtype' , [np .float64 , 'object' ])
2266+ def test_mode_shape_gh_9955 (self , axis , dtype ):
2267+ rng = np .random .default_rng (984213899 )
2268+ a = rng .uniform (size = (3 , 4 , 5 )).astype (dtype )
2269+ res = stats .mode (a , axis = axis )
2270+ reference_shape = list (a .shape )
2271+ reference_shape .pop (axis )
2272+ np .testing .assert_array_equal (res .mode .shape , reference_shape )
2273+ np .testing .assert_array_equal (res .count .shape , reference_shape )
22632274
22642275
22652276class TestSEM :
0 commit comments