1
1
import unittest
2
2
3
3
import numpy as np
4
- from pandas import Index
4
+ from pandas import Index , isnull
5
5
from pandas .util .testing import assert_almost_equal
6
6
import pandas .util .testing as common
7
7
import pandas ._tseries as lib
@@ -317,7 +317,7 @@ def test_group_add_bin():
317
317
# bin-based group_add
318
318
bins = np .array ([3 , 6 ], dtype = np .int32 )
319
319
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 )
321
321
lib .group_add_bin (out , counts , obj , bins )
322
322
323
323
assert_almost_equal (out , exp )
@@ -334,7 +334,7 @@ def test_group_mean_bin():
334
334
# bin-based group_mean
335
335
bins = np .array ([3 , 6 ], dtype = np .int32 )
336
336
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 )
338
338
lib .group_mean_bin (out , counts , obj , bins )
339
339
340
340
assert_almost_equal (out , exp )
@@ -351,12 +351,37 @@ def test_group_var_bin():
351
351
# bin-based group_var
352
352
bins = np .array ([3 , 6 ], dtype = np .int32 )
353
353
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 )
355
355
356
356
lib .group_var_bin (out , counts , obj , bins )
357
357
358
358
assert_almost_equal (out , exp )
359
359
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
+
360
385
class TestTypeInference (unittest .TestCase ):
361
386
362
387
def test_length_zero (self ):
0 commit comments