11import unittest
22
33import numpy as np
4- from pandas import Index
4+ from pandas import Index , isnull
55from pandas .util .testing import assert_almost_equal
66import pandas .util .testing as common
77import pandas ._tseries as lib
@@ -317,7 +317,7 @@ def test_group_add_bin():
317317 # bin-based group_add
318318 bins = np .array ([3 , 6 ], dtype = np .int32 )
319319 out = np .zeros ((3 , 1 ), np .float64 )
320- counts = np .empty (len (out ), dtype = np .int32 )
320+ counts = np .zeros (len (out ), dtype = np .int32 )
321321 lib .group_add_bin (out , counts , obj , bins )
322322
323323 assert_almost_equal (out , exp )
@@ -334,7 +334,7 @@ def test_group_mean_bin():
334334 # bin-based group_mean
335335 bins = np .array ([3 , 6 ], dtype = np .int32 )
336336 out = np .zeros ((3 , 1 ), np .float64 )
337- counts = np .empty (len (out ), dtype = np .int32 )
337+ counts = np .zeros (len (out ), dtype = np .int32 )
338338 lib .group_mean_bin (out , counts , obj , bins )
339339
340340 assert_almost_equal (out , exp )
@@ -351,12 +351,37 @@ def test_group_var_bin():
351351 # bin-based group_var
352352 bins = np .array ([3 , 6 ], dtype = np .int32 )
353353 out = np .zeros ((3 , 1 ), np .float64 )
354- counts = np .empty (len (out ), dtype = np .int32 )
354+ counts = np .zeros (len (out ), dtype = np .int32 )
355355
356356 lib .group_var_bin (out , counts , obj , bins )
357357
358358 assert_almost_equal (out , exp )
359359
360+ def test_group_ohlc ():
361+ obj = np .random .randn (20 )
362+
363+ bins = np .array ([6 , 12 ], dtype = np .int32 )
364+ out = np .zeros ((3 , 4 ), np .float64 )
365+ counts = np .zeros (len (out ), dtype = np .int32 )
366+
367+ lib .group_ohlc (out , counts , obj [:, None ], bins )
368+
369+ def _ohlc (group ):
370+ if isnull (group ).all ():
371+ return np .repeat (np .nan , 4 )
372+ return [group [0 ], group .min (), group .max (), group [- 1 ]]
373+
374+ expected = np .array ([_ohlc (obj [:6 ]), _ohlc (obj [6 :12 ]),
375+ _ohlc (obj [12 :])])
376+
377+ assert_almost_equal (out , expected )
378+ assert_almost_equal (counts , [6 , 6 , 8 ])
379+
380+ obj [:6 ] = np .nan
381+ lib .group_ohlc (out , counts , obj [:, None ], bins )
382+ expected [0 ] = np .nan
383+ assert_almost_equal (out , expected )
384+
360385class TestTypeInference (unittest .TestCase ):
361386
362387 def test_length_zero (self ):
0 commit comments