4
4
import numpy as np
5
5
from pandas .compat import zip
6
6
7
- from pandas import (Series , Index , isna ,
8
- to_datetime , DatetimeIndex , Timestamp ,
9
- Interval , IntervalIndex , Categorical ,
7
+ from pandas import (Series , isna , to_datetime , DatetimeIndex ,
8
+ Timestamp , Interval , IntervalIndex , Categorical ,
10
9
cut , qcut , date_range )
11
10
import pandas .util .testing as tm
12
11
from pandas .api .types import CategoricalDtype as CDT
@@ -29,7 +28,8 @@ def test_bins(self):
29
28
result , bins = cut (data , 3 , retbins = True )
30
29
31
30
intervals = IntervalIndex .from_breaks (bins .round (3 ))
32
- expected = intervals .take ([0 , 0 , 0 , 1 , 2 , 0 ]).astype ('category' )
31
+ intervals = intervals .take ([0 , 0 , 0 , 1 , 2 , 0 ])
32
+ expected = Categorical (intervals , ordered = True )
33
33
tm .assert_categorical_equal (result , expected )
34
34
tm .assert_almost_equal (bins , np .array ([0.1905 , 3.36666667 ,
35
35
6.53333333 , 9.7 ]))
@@ -38,7 +38,8 @@ def test_right(self):
38
38
data = np .array ([.2 , 1.4 , 2.5 , 6.2 , 9.7 , 2.1 , 2.575 ])
39
39
result , bins = cut (data , 4 , right = True , retbins = True )
40
40
intervals = IntervalIndex .from_breaks (bins .round (3 ))
41
- expected = intervals .astype ('category' ).take ([0 , 0 , 0 , 2 , 3 , 0 , 0 ])
41
+ expected = Categorical (intervals , ordered = True )
42
+ expected = expected .take ([0 , 0 , 0 , 2 , 3 , 0 , 0 ])
42
43
tm .assert_categorical_equal (result , expected )
43
44
tm .assert_almost_equal (bins , np .array ([0.1905 , 2.575 , 4.95 ,
44
45
7.325 , 9.7 ]))
@@ -47,7 +48,8 @@ def test_noright(self):
47
48
data = np .array ([.2 , 1.4 , 2.5 , 6.2 , 9.7 , 2.1 , 2.575 ])
48
49
result , bins = cut (data , 4 , right = False , retbins = True )
49
50
intervals = IntervalIndex .from_breaks (bins .round (3 ), closed = 'left' )
50
- expected = intervals .take ([0 , 0 , 0 , 2 , 3 , 0 , 1 ]).astype ('category' )
51
+ intervals = intervals .take ([0 , 0 , 0 , 2 , 3 , 0 , 1 ])
52
+ expected = Categorical (intervals , ordered = True )
51
53
tm .assert_categorical_equal (result , expected )
52
54
tm .assert_almost_equal (bins , np .array ([0.2 , 2.575 , 4.95 ,
53
55
7.325 , 9.7095 ]))
@@ -56,7 +58,8 @@ def test_arraylike(self):
56
58
data = [.2 , 1.4 , 2.5 , 6.2 , 9.7 , 2.1 ]
57
59
result , bins = cut (data , 3 , retbins = True )
58
60
intervals = IntervalIndex .from_breaks (bins .round (3 ))
59
- expected = intervals .take ([0 , 0 , 0 , 1 , 2 , 0 ]).astype ('category' )
61
+ intervals = intervals .take ([0 , 0 , 0 , 1 , 2 , 0 ])
62
+ expected = Categorical (intervals , ordered = True )
60
63
tm .assert_categorical_equal (result , expected )
61
64
tm .assert_almost_equal (bins , np .array ([0.1905 , 3.36666667 ,
62
65
6.53333333 , 9.7 ]))
@@ -249,8 +252,8 @@ def test_qcut_nas(self):
249
252
250
253
def test_qcut_index (self ):
251
254
result = qcut ([0 , 2 ], 2 )
252
- expected = Index ( [Interval (- 0.001 , 1 ), Interval (1 , 2 )]). astype (
253
- 'category' )
255
+ intervals = [Interval (- 0.001 , 1 ), Interval (1 , 2 )]
256
+ expected = Categorical ( intervals , ordered = True )
254
257
tm .assert_categorical_equal (result , expected )
255
258
256
259
def test_round_frac (self ):
0 commit comments