3
3
import nose
4
4
import numpy as np
5
5
6
- from pandas import Index , MultiIndex , DataFrame , Series
6
+ from pandas import Index , MultiIndex , DataFrame , Series , Categorical
7
7
from pandas .compat import OrderedDict , lrange
8
8
from pandas .sparse .array import SparseArray
9
9
from pandas .core .internals import *
@@ -41,9 +41,11 @@ def create_block(typestr, placement, item_shape=None, num_offset=0):
41
41
* complex, c16, c8
42
42
* bool
43
43
* object, string, O
44
- * datetime, dt
44
+ * datetime, dt, M8[ns]
45
+ * timedelta, td, m8[ns]
45
46
* sparse (SparseArray with fill_value=0.0)
46
47
* sparse_na (SparseArray with fill_value=np.nan)
48
+ * category, category2
47
49
48
50
"""
49
51
placement = BlockPlacement (placement )
@@ -67,8 +69,14 @@ def create_block(typestr, placement, item_shape=None, num_offset=0):
67
69
shape )
68
70
elif typestr in ('bool' ):
69
71
values = np .ones (shape , dtype = np .bool_ )
70
- elif typestr in ('datetime' , 'dt' ):
72
+ elif typestr in ('datetime' , 'dt' , 'M8[ns]' ):
71
73
values = (mat * 1e9 ).astype ('M8[ns]' )
74
+ elif typestr in ('timedelta' , 'td' , 'm8[ns]' ):
75
+ values = (mat * 1 ).astype ('m8[ns]' )
76
+ elif typestr in ('category' ):
77
+ values = Categorical ([1 ,1 ,2 ,2 ,3 ,3 ,3 ,3 ,4 ,4 ])
78
+ elif typestr in ('category2' ):
79
+ values = Categorical (['a' ,'a' ,'a' ,'a' ,'b' ,'b' ,'c' ,'c' ,'c' ,'d' ])
72
80
elif typestr in ('sparse' , 'sparse_na' ):
73
81
# FIXME: doesn't support num_rows != 10
74
82
assert shape [- 1 ] == 10
@@ -556,7 +564,54 @@ def _compare(old_mgr, new_mgr):
556
564
self .assertEqual (new_mgr .get ('h' ).dtype , np .float16 )
557
565
558
566
def test_interleave (self ):
559
- pass
567
+
568
+
569
+ # self
570
+ for dtype in ['f8' ,'i8' ,'object' ,'bool' ,'complex' ,'M8[ns]' ,'m8[ns]' ]:
571
+ mgr = create_mgr ('a: {0}' .format (dtype ))
572
+ self .assertEqual (mgr .as_matrix ().dtype ,dtype )
573
+ mgr = create_mgr ('a: {0}; b: {0}' .format (dtype ))
574
+ self .assertEqual (mgr .as_matrix ().dtype ,dtype )
575
+
576
+ # will be converted according the actual dtype of the underlying
577
+ mgr = create_mgr ('a: category' )
578
+ self .assertEqual (mgr .as_matrix ().dtype ,'i8' )
579
+ mgr = create_mgr ('a: category; b: category' )
580
+ self .assertEqual (mgr .as_matrix ().dtype ,'i8' ),
581
+ mgr = create_mgr ('a: category; b: category2' )
582
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
583
+ mgr = create_mgr ('a: category2' )
584
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
585
+ mgr = create_mgr ('a: category2; b: category2' )
586
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
587
+
588
+ # combinations
589
+ mgr = create_mgr ('a: f8' )
590
+ self .assertEqual (mgr .as_matrix ().dtype ,'f8' )
591
+ mgr = create_mgr ('a: f8; b: i8' )
592
+ self .assertEqual (mgr .as_matrix ().dtype ,'f8' )
593
+ mgr = create_mgr ('a: f4; b: i8' )
594
+ self .assertEqual (mgr .as_matrix ().dtype ,'f4' )
595
+ mgr = create_mgr ('a: f4; b: i8; d: object' )
596
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
597
+ mgr = create_mgr ('a: bool; b: i8' )
598
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
599
+ mgr = create_mgr ('a: complex' )
600
+ self .assertEqual (mgr .as_matrix ().dtype ,'complex' )
601
+ mgr = create_mgr ('a: f8; b: category' )
602
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
603
+ mgr = create_mgr ('a: M8[ns]; b: category' )
604
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
605
+ mgr = create_mgr ('a: M8[ns]; b: bool' )
606
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
607
+ mgr = create_mgr ('a: M8[ns]; b: i8' )
608
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
609
+ mgr = create_mgr ('a: m8[ns]; b: bool' )
610
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
611
+ mgr = create_mgr ('a: m8[ns]; b: i8' )
612
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
613
+ mgr = create_mgr ('a: M8[ns]; b: m8[ns]' )
614
+ self .assertEqual (mgr .as_matrix ().dtype ,'object' )
560
615
561
616
def test_interleave_non_unique_cols (self ):
562
617
df = DataFrame ([
0 commit comments