forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_categorical.py
1537 lines (1227 loc) · 58.2 KB
/
test_categorical.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=E1101,E1103,W0232
from datetime import datetime
from pandas.compat import range, lrange, u
import re
import numpy as np
import pandas as pd
from pandas import (Categorical, Index, Series, DataFrame, PeriodIndex,
Timestamp, _np_version_under1p7)
import pandas.core.common as com
import pandas.compat as compat
import pandas.util.testing as tm
class TestCategorical(tm.TestCase):
_multiprocess_can_split_ = True
def setUp(self):
self.factor = Categorical.from_array(['a', 'b', 'b', 'a',
'a', 'c', 'c', 'c'])
def test_getitem(self):
self.assertEqual(self.factor[0], 'a')
self.assertEqual(self.factor[-1], 'c')
subf = self.factor[[0, 1, 2]]
tm.assert_almost_equal(subf._codes, [0, 1, 1])
subf = self.factor[np.asarray(self.factor) == 'c']
tm.assert_almost_equal(subf._codes, [2, 2, 2])
def test_constructor_unsortable(self):
# it works!
arr = np.array([1, 2, 3, datetime.now()], dtype='O')
factor = Categorical.from_array(arr)
self.assertFalse(factor.ordered)
def test_constructor(self):
# There are multiple ways to call a constructor
# old style: two arrays, one a pointer to the labels
# old style is now only available with compat=True
exp_arr = np.array(["a", "b", "c", "a", "b", "c"])
with tm.assert_produces_warning(FutureWarning):
c_old = Categorical([0,1,2,0,1,2], levels=["a","b","c"], compat=True)
self.assert_numpy_array_equal(c_old.__array__(), exp_arr)
# the next one are from the old docs
with tm.assert_produces_warning(FutureWarning):
c_old2 = Categorical([0, 1, 2, 0, 1, 2], [1, 2, 3], compat=True)
self.assert_numpy_array_equal(c_old2.__array__(), np.array([1, 2, 3, 1, 2, 3]))
with tm.assert_produces_warning(FutureWarning):
c_old3 = Categorical([0,1,2,0,1,2], ['a', 'b', 'c'], compat=True)
self.assert_numpy_array_equal(c_old3.__array__(), np.array(['a', 'b', 'c', 'a', 'b', 'c']))
with tm.assert_produces_warning(FutureWarning):
cat = pd.Categorical([1,2], levels=[1,2,3], compat=True)
self.assert_numpy_array_equal(cat.__array__(), np.array([2,3]))
with tm.assert_produces_warning(None):
cat = pd.Categorical([1,2], levels=[1,2,3], compat=False)
self.assert_numpy_array_equal(cat.__array__(), np.array([1,2]))
# new style
c1 = Categorical(exp_arr)
self.assert_numpy_array_equal(c1.__array__(), exp_arr)
c2 = Categorical(exp_arr, levels=["a","b","c"])
self.assert_numpy_array_equal(c2.__array__(), exp_arr)
c2 = Categorical(exp_arr, levels=["c","b","a"])
self.assert_numpy_array_equal(c2.__array__(), exp_arr)
# Categorical as input
c1 = Categorical(["a", "b", "c", "a"])
c2 = Categorical(c1)
self.assertTrue(c1.equals(c2))
c1 = Categorical(["a", "b", "c", "a"], levels=["a","b","c","d"])
c2 = Categorical(c1)
self.assertTrue(c1.equals(c2))
c1 = Categorical(["a", "b", "c", "a"], levels=["a","c","b"])
c2 = Categorical(c1)
self.assertTrue(c1.equals(c2))
c1 = Categorical(["a", "b", "c", "a"], levels=["a","c","b"])
c2 = Categorical(c1, levels=["a","b","c"])
self.assert_numpy_array_equal(c1.__array__(), c2.__array__())
self.assert_numpy_array_equal(c2.levels, np.array(["a","b","c"]))
# Series of dtype category
c1 = Categorical(["a", "b", "c", "a"], levels=["a","b","c","d"])
c2 = Categorical(Series(c1))
self.assertTrue(c1.equals(c2))
c1 = Categorical(["a", "b", "c", "a"], levels=["a","c","b"])
c2 = Categorical(Series(c1))
self.assertTrue(c1.equals(c2))
# Series
c1 = Categorical(["a", "b", "c", "a"])
c2 = Categorical(Series(["a", "b", "c", "a"]))
self.assertTrue(c1.equals(c2))
c1 = Categorical(["a", "b", "c", "a"], levels=["a","b","c","d"])
c2 = Categorical(Series(["a", "b", "c", "a"]), levels=["a","b","c","d"])
self.assertTrue(c1.equals(c2))
# This should result in integer levels, not float!
cat = pd.Categorical([1,2,3,np.nan], levels=[1,2,3])
self.assertTrue(com.is_integer_dtype(cat.levels))
def test_from_codes(self):
# too few levels
def f():
Categorical.from_codes([1,2], [1,2])
self.assertRaises(ValueError, f)
# no int codes
def f():
Categorical.from_codes(["a"], [1,2])
self.assertRaises(ValueError, f)
# no unique levels
def f():
Categorical.from_codes([0,1,2], ["a","a","b"])
self.assertRaises(ValueError, f)
# too negative
def f():
Categorical.from_codes([-2,1,2], ["a","b","c"])
self.assertRaises(ValueError, f)
exp = Categorical(["a","b","c"])
res = Categorical.from_codes([0,1,2], ["a","b","c"])
self.assertTrue(exp.equals(res))
# Not available in earlier numpy versions
if hasattr(np.random, "choice"):
codes = np.random.choice([0,1], 5, p=[0.9,0.1])
pd.Categorical.from_codes(codes, levels=["train", "test"])
def test_comparisons(self):
result = self.factor[self.factor == 'a']
expected = self.factor[np.asarray(self.factor) == 'a']
self.assertTrue(result.equals(expected))
result = self.factor[self.factor != 'a']
expected = self.factor[np.asarray(self.factor) != 'a']
self.assertTrue(result.equals(expected))
result = self.factor[self.factor < 'c']
expected = self.factor[np.asarray(self.factor) < 'c']
self.assertTrue(result.equals(expected))
result = self.factor[self.factor > 'a']
expected = self.factor[np.asarray(self.factor) > 'a']
self.assertTrue(result.equals(expected))
result = self.factor[self.factor >= 'b']
expected = self.factor[np.asarray(self.factor) >= 'b']
self.assertTrue(result.equals(expected))
result = self.factor[self.factor <= 'b']
expected = self.factor[np.asarray(self.factor) <= 'b']
self.assertTrue(result.equals(expected))
n = len(self.factor)
other = self.factor[np.random.permutation(n)]
result = self.factor == other
expected = np.asarray(self.factor) == np.asarray(other)
self.assert_numpy_array_equal(result, expected)
result = self.factor == 'd'
expected = np.repeat(False, len(self.factor))
self.assert_numpy_array_equal(result, expected)
def test_na_flags_int_levels(self):
# #1457
levels = lrange(10)
labels = np.random.randint(0, 10, 20)
labels[::5] = -1
cat = Categorical(labels, levels, fastpath=True)
repr(cat)
self.assert_numpy_array_equal(com.isnull(cat), labels == -1)
def test_levels_none(self):
factor = Categorical(['a', 'b', 'b', 'a',
'a', 'c', 'c', 'c'])
self.assertTrue(factor.equals(self.factor))
def test_describe(self):
# string type
desc = self.factor.describe()
expected = DataFrame.from_dict(dict(counts=[3, 2, 3],
freqs=[3/8., 2/8., 3/8.],
levels=['a', 'b', 'c'])
).set_index('levels')
tm.assert_frame_equal(desc, expected)
# check an integer one
desc = Categorical([1,2,3,1,2,3,3,2,1,1,1]).describe()
expected = DataFrame.from_dict(dict(counts=[5, 3, 3],
freqs=[5/11., 3/11., 3/11.],
levels=[1,2,3]
)
).set_index('levels')
tm.assert_frame_equal(desc, expected)
def test_print(self):
expected = [" a", " b", " b", " a", " a", " c", " c", " c",
"Levels (3, object): [a < b < c]"]
expected = "\n".join(expected)
actual = repr(self.factor)
self.assertEqual(actual, expected)
def test_big_print(self):
factor = Categorical(np.array([0,1,2,0,1,2]*100), ['a', 'b', 'c'],
name='cat', fastpath=True)
expected = [" a", " b", " c", " a", " b", " c", " a", " b", " c",
" a", " b", " c", " a", "...", " c", " a", " b", " c",
" a", " b", " c", " a", " b", " c", " a", " b", " c",
"Name: cat, Length: 600",
"Levels (3, object): [a, b, c]"]
expected = "\n".join(expected)
actual = repr(factor)
self.assertEqual(expected, actual)
def test_empty_print(self):
factor = Categorical([], ["a","b","c"], name="cat")
expected = ("Categorical([], Name: cat, Levels (3, object): [a < b < c]")
# hack because array_repr changed in numpy > 1.6.x
actual = repr(factor)
self.assertEqual(actual, expected)
factor = Categorical([], ["a","b","c"])
expected = ("Categorical([], Levels (3, object): [a < b < c]")
actual = repr(factor)
self.assertEqual(expected, actual)
factor = Categorical([], [])
expected = ("Categorical([], Levels (0, object): []")
self.assertEqual(expected, repr(factor))
def test_periodindex(self):
idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
'2014-03', '2014-03'], freq='M')
cat1 = Categorical.from_array(idx1)
str(cat1)
exp_arr = np.array([0, 0, 1, 1, 2, 2],dtype='int64')
exp_idx = PeriodIndex(['2014-01', '2014-02', '2014-03'], freq='M')
self.assert_numpy_array_equal(cat1._codes, exp_arr)
self.assertTrue(cat1.levels.equals(exp_idx))
idx2 = PeriodIndex(['2014-03', '2014-03', '2014-02', '2014-01',
'2014-03', '2014-01'], freq='M')
cat2 = Categorical.from_array(idx2)
str(cat2)
exp_arr = np.array([2, 2, 1, 0, 2, 0],dtype='int64')
exp_idx2 = PeriodIndex(['2014-01', '2014-02', '2014-03'], freq='M')
self.assert_numpy_array_equal(cat2._codes, exp_arr)
self.assertTrue(cat2.levels.equals(exp_idx2))
idx3 = PeriodIndex(['2013-12', '2013-11', '2013-10', '2013-09',
'2013-08', '2013-07', '2013-05'], freq='M')
cat3 = Categorical.from_array(idx3)
exp_arr = np.array([6, 5, 4, 3, 2, 1, 0],dtype='int64')
exp_idx = PeriodIndex(['2013-05', '2013-07', '2013-08', '2013-09',
'2013-10', '2013-11', '2013-12'], freq='M')
self.assert_numpy_array_equal(cat3._codes, exp_arr)
self.assertTrue(cat3.levels.equals(exp_idx))
def test_level_assigments(self):
s = pd.Categorical(["a","b","c","a"])
exp = np.array([1,2,3,1])
s.levels = [1,2,3]
self.assert_numpy_array_equal(s.__array__(), exp)
self.assert_numpy_array_equal(s.levels, np.array([1,2,3]))
# lengthen
s.levels = [1,2,3,4]
# does nothing to the values but only the the levels
self.assert_numpy_array_equal(s.__array__(), exp)
self.assert_numpy_array_equal(s.levels, np.array([1,2,3,4]))
# shorten
exp2 = np.array([1,2,np.nan,1])
s.levels = [1,2]
self.assert_numpy_array_equivalent(s.__array__(), exp2) # doesn't work with nan :-(
self.assertTrue(np.isnan(s.__array__()[2]))
self.assert_numpy_array_equal(s.levels, np.array([1,2]))
def test_reorder_levels(self):
cat = Categorical(["a","b","c","a"], ordered=True)
exp_levels = np.array(["c","b","a"])
exp_values = np.array(["a","b","c","a"])
cat.reorder_levels(["c","b","a"])
self.assert_numpy_array_equal(cat.levels, exp_levels)
self.assert_numpy_array_equal(cat.__array__(), exp_values)
# not all "old" included in "new"
def f():
cat.reorder_levels(["a"])
self.assertRaises(ValueError, f)
# still not all "old" in "new"
def f():
cat.reorder_levels(["a","b","d"])
self.assertRaises(ValueError, f)
# This works: all "old" included in "new"
cat.reorder_levels(["a","b","c","d"])
exp_levels = np.array(["a","b","c","d"])
self.assert_numpy_array_equal(cat.levels, exp_levels)
# internals...
c = Categorical([1,2,3,4,1], levels=[1,2,3,4])
self.assert_numpy_array_equal(c._codes, np.array([0,1,2,3,0]))
self.assert_numpy_array_equal(c.levels , np.array([1,2,3,4] ))
self.assert_numpy_array_equal(c.get_values() , np.array([1,2,3,4,1] ))
c.reorder_levels([4,3,2,1]) # all "pointers" to '4' must be changed from 3 to 0,...
self.assert_numpy_array_equal(c._codes , np.array([3,2,1,0,3])) # positions are changed
self.assert_numpy_array_equal(c.levels , np.array([4,3,2,1])) # levels are now in new order
self.assert_numpy_array_equal(c.get_values() , np.array([1,2,3,4,1])) # output is the same
self.assertTrue(c.min(), 4)
self.assertTrue(c.max(), 1)
def f():
c.reorder_levels([4,3,2,10])
self.assertRaises(ValueError, f)
def test_remove_unused_levels(self):
c = Categorical(["a","b","c","d","a"], levels=["a","b","c","d","e"])
self.assert_numpy_array_equal(c.levels , np.array(["a","b","c","d","e"]))
c.remove_unused_levels()
self.assert_numpy_array_equal(c.levels , np.array(["a","b","c","d"]))
def test_nan_handling(self):
# Nans are represented as -1 in codes
c = Categorical(["a","b",np.nan,"a"])
self.assert_numpy_array_equal(c.levels , np.array(["a","b"]))
self.assert_numpy_array_equal(c._codes , np.array([0,1,-1,0]))
# If levels have nan included, the code should point to that instead
c = Categorical(["a","b",np.nan,"a"], levels=["a","b",np.nan])
self.assert_numpy_array_equal(c.levels , np.array(["a","b",np.nan],dtype=np.object_))
self.assert_numpy_array_equal(c._codes , np.array([0,1,2,0]))
# Changing levels should also make the replaced level np.nan
c = Categorical(["a","b","c","a"])
c.levels = ["a","b",np.nan]
self.assert_numpy_array_equal(c.levels , np.array(["a","b",np.nan],dtype=np.object_))
self.assert_numpy_array_equal(c._codes , np.array([0,1,2,0]))
def test_codes_immutable(self):
# Codes should be read only
c = Categorical(["a","b","c","a", np.nan])
exp = np.array([0,1,2,0, -1])
self.assert_numpy_array_equal(c.codes, exp)
# Assignments to codes should raise
def f():
c.codes = np.array([0,1,2,0,1])
self.assertRaises(ValueError, f)
# changes in the codes array should raise
# np 1.6.1 raises RuntimeError rather than ValueError
codes= c.codes
def f():
codes[4] = 1
if _np_version_under1p7:
self.assertRaises(RuntimeError, f)
else:
self.assertRaises(ValueError, f)
# But even after getting the codes, the original array should still be writeable!
c[4] = "a"
exp = np.array([0,1,2,0, 0])
self.assert_numpy_array_equal(c.codes, exp)
c._codes[4] = 2
exp = np.array([0,1,2,0, 2])
self.assert_numpy_array_equal(c.codes, exp)
def test_min_max(self):
# unordered cats have no min/max
cat = Categorical(["a","b","c","d"], ordered=False)
self.assertRaises(TypeError, lambda : cat.min())
self.assertRaises(TypeError, lambda : cat.max())
cat = Categorical(["a","b","c","d"], ordered=True)
_min = cat.min()
_max = cat.max()
self.assertEqual(_min, "a")
self.assertEqual(_max, "d")
cat = Categorical(["a","b","c","d"], levels=['d','c','b','a'], ordered=True)
_min = cat.min()
_max = cat.max()
self.assertEqual(_min, "d")
self.assertEqual(_max, "a")
cat = Categorical([np.nan,"b","c",np.nan], levels=['d','c','b','a'], ordered=True)
_min = cat.min()
_max = cat.max()
self.assertTrue(np.isnan(_min))
self.assertEqual(_max, "b")
_min = cat.min(numeric_only=True)
self.assertEqual(_min, "c")
_max = cat.max(numeric_only=True)
self.assertEqual(_max, "b")
cat = Categorical([np.nan,1,2,np.nan], levels=[5,4,3,2,1], ordered=True)
_min = cat.min()
_max = cat.max()
self.assertTrue(np.isnan(_min))
self.assertEqual(_max, 1)
_min = cat.min(numeric_only=True)
self.assertEqual(_min, 2)
_max = cat.max(numeric_only=True)
self.assertEqual(_max, 1)
def test_mode(self):
s = Categorical([1,1,2,4,5,5,5], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([5], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
s = Categorical([1,1,1,4,5,5,5], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([5,1], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
s = Categorical([1,2,3,4,5], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
# NaN should not become the mode!
s = Categorical([np.nan,np.nan,np.nan,4,5], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
s = Categorical([np.nan,np.nan,np.nan,4,5,4], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([4], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
s = Categorical([np.nan,np.nan,4,5,4], levels=[5,4,3,2,1], ordered=True)
res = s.mode()
exp = Categorical([4], levels=[5,4,3,2,1], ordered=True)
self.assertTrue(res.equals(exp))
def test_sort(self):
# unordered cats are not sortable
cat = Categorical(["a","b","b","a"], ordered=False)
self.assertRaises(TypeError, lambda : cat.sort())
cat = Categorical(["a","c","b","d"], ordered=True)
# order
res = cat.order()
exp = np.array(["a","b","c","d"],dtype=object)
self.assert_numpy_array_equal(res.__array__(), exp)
cat = Categorical(["a","c","b","d"], levels=["a","b","c","d"], ordered=True)
res = cat.order()
exp = np.array(["a","b","c","d"],dtype=object)
self.assert_numpy_array_equal(res.__array__(), exp)
res = cat.order(ascending=False)
exp = np.array(["d","c","b","a"],dtype=object)
self.assert_numpy_array_equal(res.__array__(), exp)
# sort (inplace order)
cat1 = cat.copy()
cat1.sort()
exp = np.array(["a","b","c","d"],dtype=object)
self.assert_numpy_array_equal(cat1.__array__(), exp)
def test_slicing_directly(self):
cat = Categorical(["a","b","c","d","a","b","c"])
sliced = cat[3]
tm.assert_equal(sliced, "d")
sliced = cat[3:5]
expected = Categorical(["d","a"], levels=['a', 'b', 'c', 'd'])
self.assert_numpy_array_equal(sliced._codes, expected._codes)
tm.assert_index_equal(sliced.levels, expected.levels)
def test_ndimensional_values(self):
exp_arr = np.array([['a', 'b'], ['c', 'b']], dtype=object)
cat = Categorical(exp_arr)
self.assertEqual(cat.shape, (2, 2))
self.assert_numpy_array_equal(cat.__array__(), exp_arr)
self.assert_numpy_array_equal(cat.T, exp_arr.T)
self.assert_numpy_array_equal(cat.ravel(), exp_arr.ravel())
# test indexing
self.assertEqual(cat[0, 0], 'a')
self.assert_numpy_array_equal(cat[0], exp_arr[0])
self.assert_numpy_array_equal(cat[:, :2], exp_arr)
self.assert_numpy_array_equal(cat[[0, 1], [0, 1]], np.diag(exp_arr))
self.assert_numpy_array_equal(cat[0, :], ['a', 'b'])
self.assert_numpy_array_equal(cat[0, [0, 1]], ['a', 'b'])
# TODO: repr, __setitem__, take, min, max, order, describe, _cat_compare_op
class TestCategoricalAsBlock(tm.TestCase):
_multiprocess_can_split_ = True
def setUp(self):
self.factor = Categorical.from_array(['a', 'b', 'b', 'a',
'a', 'c', 'c', 'c'])
df = DataFrame({'value': np.random.randint(0, 10000, 100)})
labels = [ "{0} - {1}".format(i, i + 499) for i in range(0, 10000, 500) ]
df = df.sort(columns=['value'], ascending=True)
df['value_group'] = pd.cut(df.value, range(0, 10500, 500), right=False, labels=labels)
self.cat = df
def test_dtypes(self):
dtype = com.CategoricalDtype()
hash(dtype)
self.assertTrue(com.is_categorical_dtype(dtype))
s = Series(self.factor,name='A')
# dtypes
self.assertTrue(com.is_categorical_dtype(s.dtype))
self.assertTrue(com.is_categorical_dtype(s))
self.assertFalse(com.is_categorical_dtype(np.dtype('float64')))
# np.dtype doesn't know about our new dtype
def f():
np.dtype(dtype)
self.assertRaises(TypeError, f)
self.assertFalse(dtype == np.str_)
self.assertFalse(np.str_ == dtype)
def test_basic(self):
# test basic creation / coercion of categoricals
s = Series(self.factor,name='A')
self.assertEqual(s.dtype,'category')
self.assertEqual(len(s),len(self.factor))
str(s.values)
str(s)
# in a frame
df = DataFrame({'A' : self.factor })
result = df['A']
tm.assert_series_equal(result,s)
result = df.iloc[:,0]
tm.assert_series_equal(result,s)
self.assertEqual(len(df),len(self.factor))
str(df.values)
str(df)
df = DataFrame({'A' : s })
result = df['A']
tm.assert_series_equal(result,s)
self.assertEqual(len(df),len(self.factor))
str(df.values)
str(df)
# multiples
df = DataFrame({'A' : s, 'B' : s, 'C' : 1})
result1 = df['A']
result2 = df['B']
tm.assert_series_equal(result1,s)
tm.assert_series_equal(result2,s)
self.assertEqual(len(df),len(self.factor))
str(df.values)
str(df)
def test_creation_astype(self):
l = ["a","b","c","a"]
s = pd.Series(l)
exp = pd.Series(Categorical(l))
res = s.astype('category')
tm.assert_series_equal(res, exp)
l = [1,2,3,1]
s = pd.Series(l)
exp = pd.Series(Categorical(l))
res = s.astype('category')
tm.assert_series_equal(res, exp)
df = pd.DataFrame({"cats":[1,2,3,4,5,6], "vals":[1,2,3,4,5,6]})
cats = Categorical([1,2,3,4,5,6])
exp_df = pd.DataFrame({"cats":cats, "vals":[1,2,3,4,5,6]})
df["cats"] = df["cats"].astype("category")
tm.assert_frame_equal(exp_df, df)
df = pd.DataFrame({"cats":['a', 'b', 'b', 'a', 'a', 'd'], "vals":[1,2,3,4,5,6]})
cats = Categorical(['a', 'b', 'b', 'a', 'a', 'd'])
exp_df = pd.DataFrame({"cats":cats, "vals":[1,2,3,4,5,6]})
df["cats"] = df["cats"].astype("category")
tm.assert_frame_equal(exp_df, df)
def test_sideeffects_free(self):
# Passing a categorical to a Series and then changing values in either the series or the
# categorical should not change the values in the other one!
cat = Categorical(["a","b","c","a"])
s = pd.Series(cat, copy=True)
self.assertFalse(s.cat is cat)
s.cat.levels = [1,2,3]
exp_s = np.array([1,2,3,1])
exp_cat = np.array(["a","b","c","a"])
self.assert_numpy_array_equal(s.__array__(), exp_s)
self.assert_numpy_array_equal(cat.__array__(), exp_cat)
# setting
s[0] = 2
exp_s2 = np.array([2,2,3,1])
self.assert_numpy_array_equal(s.__array__(), exp_s2)
self.assert_numpy_array_equal(cat.__array__(), exp_cat)
# however, copy is False by default
# so this WILL change values
cat = Categorical(["a","b","c","a"])
s = pd.Series(cat)
self.assertTrue(s.cat is cat)
s.cat.levels = [1,2,3]
exp_s = np.array([1,2,3,1])
self.assert_numpy_array_equal(s.__array__(), exp_s)
self.assert_numpy_array_equal(cat.__array__(), exp_s)
s[0] = 2
exp_s2 = np.array([2,2,3,1])
self.assert_numpy_array_equal(s.__array__(), exp_s2)
self.assert_numpy_array_equal(cat.__array__(), exp_s2)
def test_nan_handling(self):
# Nans are represented as -1 in labels
s = Series(Categorical(["a","b",np.nan,"a"]))
self.assert_numpy_array_equal(s.cat.levels, np.array(["a","b"]))
self.assert_numpy_array_equal(s.cat._codes, np.array([0,1,-1,0]))
# If levels have nan included, the label should point to that instead
s2 = Series(Categorical(["a","b",np.nan,"a"], levels=["a","b",np.nan]))
self.assert_numpy_array_equal(s2.cat.levels,
np.array(["a","b",np.nan], dtype=np.object_))
self.assert_numpy_array_equal(s2.cat._codes, np.array([0,1,2,0]))
# Changing levels should also make the replaced level np.nan
s3 = Series(Categorical(["a","b","c","a"]))
s3.cat.levels = ["a","b",np.nan]
self.assert_numpy_array_equal(s3.cat.levels,
np.array(["a","b",np.nan], dtype=np.object_))
self.assert_numpy_array_equal(s3.cat._codes, np.array([0,1,2,0]))
def test_series_delegations(self):
# invalid accessor
self.assertRaises(TypeError, lambda : Series([1,2,3]).cat)
tm.assertRaisesRegexp(TypeError,
r"Can only use .cat accessor with a 'category' dtype",
lambda : Series([1,2,3]).cat)
self.assertRaises(TypeError, lambda : Series(['a','b','c']).cat)
self.assertRaises(TypeError, lambda : Series(np.arange(5.)).cat)
self.assertRaises(TypeError, lambda : Series([Timestamp('20130101')]).cat)
# Series should delegate calls to '.level', '.ordered' and '.reorder()' to the categorical
s = Series(Categorical(["a","b","c","a"], ordered=True))
exp_levels = np.array(["a","b","c"])
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
s.cat.levels = [1,2,3]
exp_levels = np.array([1,2,3])
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
self.assertEqual(s.cat.ordered, True)
s.cat.ordered = False
self.assertEqual(s.cat.ordered, False)
# reorder
s = Series(Categorical(["a","b","c","a"], ordered=True))
exp_levels = np.array(["c","b","a"])
exp_values = np.array(["a","b","c","a"])
s.cat.reorder_levels(["c","b","a"])
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
self.assert_numpy_array_equal(s.cat.__array__(), exp_values)
self.assert_numpy_array_equal(s.__array__(), exp_values)
# remove unused levels
s = Series(Categorical(["a","b","b","a"], levels=["a","b","c"]))
exp_levels = np.array(["a","b"])
exp_values = np.array(["a","b","b","a"])
s.cat.remove_unused_levels()
self.assert_numpy_array_equal(s.cat.levels, exp_levels)
self.assert_numpy_array_equal(s.cat.__array__(), exp_values)
self.assert_numpy_array_equal(s.__array__(), exp_values)
# This method is likely to be confused, so test that it raises an error on wrong inputs:
def f():
s.reorder_levels([4,3,2,1])
self.assertRaises(Exception, f)
# right: s.cat.reorder_levels([4,3,2,1])
def test_series_functions_no_warnings(self):
df = pd.DataFrame({'value': np.random.randint(0, 100, 20)})
labels = [ "{0} - {1}".format(i, i + 9) for i in range(0, 100, 10)]
with tm.assert_produces_warning(False):
df['group'] = pd.cut(df.value, range(0, 105, 10), right=False, labels=labels)
def test_assignment_to_dataframe(self):
# assignment
df = DataFrame({'value': np.array(np.random.randint(0, 10000, 100),dtype='int32')})
labels = [ "{0} - {1}".format(i, i + 499) for i in range(0, 10000, 500) ]
df = df.sort(columns=['value'], ascending=True)
d = pd.cut(df.value, range(0, 10500, 500), right=False, labels=labels)
s = Series(d)
df['D'] = d
str(df)
result = df.dtypes
expected = Series([np.dtype('int32'), com.CategoricalDtype()],index=['value','D'])
tm.assert_series_equal(result,expected)
df['E'] = s
str(df)
result = df.dtypes
expected = Series([np.dtype('int32'), com.CategoricalDtype(), com.CategoricalDtype()],
index=['value','D','E'])
tm.assert_series_equal(result,expected)
result1 = df['D']
result2 = df['E']
self.assertTrue(result1._data._block.values.equals(d))
# sorting
s.name = 'E'
self.assertTrue(result2.sort_index().equals(s))
# FIXME?
#### what does this compare to? ###
result = df.sort_index()
cat = pd.Categorical([1,2,3,10], levels=[1,2,3,4,10])
df = pd.DataFrame(pd.Series(cat))
def test_describe(self):
# Categoricals should not show up together with numerical columns
result = self.cat.describe()
self.assertEquals(len(result.columns),1)
# empty levels show up as NA
s = Series(Categorical(["a","b","b","b"], levels=['a','b','c'], ordered=True))
result = s.cat.describe()
expected = DataFrame([[1,0.25],[3,0.75],[np.nan,np.nan]],
columns=['counts','freqs'],
index=Index(['a','b','c'],name='levels'))
tm.assert_frame_equal(result,expected)
result = s.describe()
expected = Series([4,2,"b",3],index=['count','unique','top', 'freq'])
tm.assert_series_equal(result,expected)
# NA as a level
cat = pd.Categorical(["a","c","c",np.nan], levels=["b","a","c",np.nan] )
result = cat.describe()
expected = DataFrame([[np.nan, np.nan],[1,0.25],[2,0.5], [1,0.25]],
columns=['counts','freqs'],
index=Index(['b','a','c',np.nan],name='levels'))
tm.assert_frame_equal(result,expected)
# In a frame, describe() for the cat should be the same as for string arrays (count, unique,
# top, freq)
cat = pd.Series(pd.Categorical(["a","b","c","c"]))
df3 = pd.DataFrame({"cat":cat, "s":["a","b","c","c"]})
res = df3.describe()
self.assert_numpy_array_equal(res["cat"].values, res["s"].values)
def test_repr(self):
a = pd.Series(pd.Categorical([1,2,3,4], name="a"))
exp = u("0 1\n1 2\n2 3\n3 4\n" +
"Name: a, dtype: category\nLevels (4, int64): [1 < 2 < 3 < 4]")
self.assertEqual(exp, a.__unicode__())
a = pd.Series(pd.Categorical(["a","b"] *25, name="a"))
exp = u("".join(["%s a\n%s b\n"%(i,i+1) for i in range(0,10,2)]) + "...\n" +
"".join(["%s a\n%s b\n"%(i,i+1) for i in range(40,50,2)]) +
"Name: a, Length: 50, dtype: category\n" +
"Levels (2, object): [a < b]")
self.assertEqual(exp,a._tidy_repr())
levs = list("abcdefghijklmnopqrstuvwxyz")
a = pd.Series(pd.Categorical(["a","b"], name="a", levels=levs))
exp = u("0 a\n1 b\n" +
"Name: a, dtype: category\n"
"Levels (26, object): [a < b < c < d ... w < x < y < z]")
self.assertEqual(exp,a.__unicode__())
def test_groupby_sort(self):
# http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby
# This should result in a properly sorted Series so that the plot
# has a sorted x axis
#self.cat.groupby(['value_group'])['value_group'].count().plot(kind='bar')
res = self.cat.groupby(['value_group'])['value_group'].count()
exp = res[sorted(res.index, key=lambda x: float(x.split()[0]))]
tm.assert_series_equal(res, exp)
def test_min_max(self):
# unordered cats have no min/max
cat = Series(Categorical(["a","b","c","d"], ordered=False))
self.assertRaises(TypeError, lambda : cat.min())
self.assertRaises(TypeError, lambda : cat.max())
cat = Series(Categorical(["a","b","c","d"], ordered=True))
_min = cat.min()
_max = cat.max()
self.assertEqual(_min, "a")
self.assertEqual(_max, "d")
cat = Series(Categorical(["a","b","c","d"], levels=['d','c','b','a'], ordered=True))
_min = cat.min()
_max = cat.max()
self.assertEqual(_min, "d")
self.assertEqual(_max, "a")
cat = Series(Categorical([np.nan,"b","c",np.nan], levels=['d','c','b','a'], ordered=True))
_min = cat.min()
_max = cat.max()
self.assertTrue(np.isnan(_min))
self.assertEqual(_max, "b")
cat = Series(Categorical([np.nan,1,2,np.nan], levels=[5,4,3,2,1], ordered=True))
_min = cat.min()
_max = cat.max()
self.assertTrue(np.isnan(_min))
self.assertEqual(_max, 1)
def test_mode(self):
s = Series(Categorical([1,1,2,4,5,5,5], levels=[5,4,3,2,1], ordered=True))
res = s.mode()
exp = Series(Categorical([5], levels=[5,4,3,2,1], ordered=True))
tm.assert_series_equal(res, exp)
s = Series(Categorical([1,1,1,4,5,5,5], levels=[5,4,3,2,1], ordered=True))
res = s.mode()
exp = Series(Categorical([5,1], levels=[5,4,3,2,1], ordered=True))
tm.assert_series_equal(res, exp)
s = Series(Categorical([1,2,3,4,5], levels=[5,4,3,2,1], ordered=True))
res = s.mode()
exp = Series(Categorical([], levels=[5,4,3,2,1], ordered=True))
tm.assert_series_equal(res, exp)
def test_value_counts(self):
s = pd.Series(pd.Categorical(["a","b","c","c","c","b"], levels=["c","a","b","d"]))
res = s.value_counts(sort=False)
exp = Series([3,1,2,0], index=["c","a","b","d"])
tm.assert_series_equal(res, exp)
res = s.value_counts(sort=True)
exp = Series([3,2,1,0], index=["c","b","a","d"])
tm.assert_series_equal(res, exp)
def test_groupby(self):
cats = Categorical(["a", "a", "a", "b", "b", "b", "c", "c", "c"], levels=["a","b","c","d"])
data = DataFrame({"a":[1,1,1,2,2,2,3,4,5], "b":cats})
expected = DataFrame({ 'a' : Series([1,2,4,np.nan],index=Index(['a','b','c','d'],name='b')) })
result = data.groupby("b").mean()
tm.assert_frame_equal(result, expected)
raw_cat1 = Categorical(["a","a","b","b"], levels=["a","b","z"])
raw_cat2 = Categorical(["c","d","c","d"], levels=["c","d","y"])
df = DataFrame({"A":raw_cat1,"B":raw_cat2, "values":[1,2,3,4]})
# single grouper
gb = df.groupby("A")
expected = DataFrame({ 'values' : Series([3,7,np.nan],index=Index(['a','b','z'],name='A')) })
result = gb.sum()
tm.assert_frame_equal(result, expected)
# multiple groupers
gb = df.groupby(['A','B'])
expected = DataFrame({ 'values' : Series([1,2,np.nan,3,4,np.nan,np.nan,np.nan,np.nan],
index=pd.MultiIndex.from_product([['a','b','z'],['c','d','y']],names=['A','B'])) })
result = gb.sum()
tm.assert_frame_equal(result, expected)
# multiple groupers with a non-cat
df = df.copy()
df['C'] = ['foo','bar']*2
gb = df.groupby(['A','B','C'])
expected = DataFrame({ 'values' :
Series(np.nan,index=pd.MultiIndex.from_product([['a','b','z'],
['c','d','y'],
['foo','bar']],
names=['A','B','C']))
}).sortlevel()
expected.iloc[[1,2,7,8],0] = [1,2,3,4]
result = gb.sum()
tm.assert_frame_equal(result, expected)
def test_pivot_table(self):
raw_cat1 = Categorical(["a","a","b","b"], levels=["a","b","z"])
raw_cat2 = Categorical(["c","d","c","d"], levels=["c","d","y"])
df = DataFrame({"A":raw_cat1,"B":raw_cat2, "values":[1,2,3,4]})
result = pd.pivot_table(df, values='values', index=['A', 'B'])
expected = Series([1,2,np.nan,3,4,np.nan,np.nan,np.nan,np.nan],
index=pd.MultiIndex.from_product([['a','b','z'],['c','d','y']],names=['A','B']),
name='values')
tm.assert_series_equal(result, expected)
def test_count(self):
s = Series(Categorical([np.nan,1,2,np.nan], levels=[5,4,3,2,1], ordered=True))
result = s.count()
self.assertEqual(result, 2)
def test_sort(self):
# unordered cats are not sortable
cat = Series(Categorical(["a","b","b","a"], ordered=False))
self.assertRaises(TypeError, lambda : cat.sort())
cat = Series(Categorical(["a","c","b","d"], ordered=True))
res = cat.order()
exp = np.array(["a","b","c","d"])
self.assert_numpy_array_equal(res.__array__(), exp)
cat = Series(Categorical(["a","c","b","d"], levels=["a","b","c","d"], ordered=True))
res = cat.order()
exp = np.array(["a","b","c","d"])
self.assert_numpy_array_equal(res.__array__(), exp)
res = cat.order(ascending=False)
exp = np.array(["d","c","b","a"])
self.assert_numpy_array_equal(res.__array__(), exp)
raw_cat1 = Categorical(["a","b","c","d"], levels=["a","b","c","d"], ordered=False)
raw_cat2 = Categorical(["a","b","c","d"], levels=["d","c","b","a"])
s = ["a","b","c","d"]
df = DataFrame({"unsort":raw_cat1,"sort":raw_cat2, "string":s, "values":[1,2,3,4]})
# Cats must be sorted in a dataframe
res = df.sort(columns=["string"], ascending=False)
exp = np.array(["d", "c", "b", "a"])
self.assert_numpy_array_equal(res["sort"].cat.__array__(), exp)
self.assertEqual(res["sort"].dtype, "category")
res = df.sort(columns=["sort"], ascending=False)
exp = df.sort(columns=["string"], ascending=True)
self.assert_numpy_array_equal(res["values"], exp["values"])
self.assertEqual(res["sort"].dtype, "category")
self.assertEqual(res["unsort"].dtype, "category")
def f():
df.sort(columns=["unsort"], ascending=False)
self.assertRaises(TypeError, f)
def test_slicing(self):
cat = Series(Categorical([1,2,3,4]))
reversed = cat[::-1]
exp = np.array([4,3,2,1])
self.assert_numpy_array_equal(reversed.__array__(), exp)
df = DataFrame({'value': (np.arange(100)+1).astype('int64')})
df['D'] = pd.cut(df.value, bins=[0,25,50,75,100])
expected = Series([11,'(0, 25]'],index=['value','D'])
result = df.iloc[10]
tm.assert_series_equal(result,expected)
expected = DataFrame({'value': np.arange(11,21).astype('int64')},