@@ -577,58 +577,39 @@ def sum(self, *args, **kwargs):
577
577
578
578
#XXX: sep argument does not seem very useful
579
579
def to_excel (self , filename , sep = None ):
580
- import ExcelCom as ec
580
+ import xlsxwriter as xl
581
581
582
582
if sep is None :
583
583
sep = '_'
584
584
#sep = self.sep
585
- xl = ec .comExcel ()
586
- xl .load (filename )
585
+ workbook = xl .Workbook (filename )
587
586
if self .ndim > 2 :
588
587
for key in product (* [axis .labels for axis in self .axes [:- 2 ]]):
589
588
sheetname = sep .join (str (k ) for k in key )
590
-
591
589
# sheet names must not:
592
590
# * contain any of the following characters: : \ / ? * [ ]
593
591
#XXX: this will NOT work for unicode strings !
594
592
sheetname = sheetname .translate (string .maketrans ('[:]' , '(-)' ),
595
593
r'\/?*' ) # chars to delete
596
594
# * exceed 31 characters
597
- sheetname = sheetname [:31 ]
595
+ # sheetname = sheetname[:31]
598
596
# * be blank
599
597
assert sheetname , "sheet name cannot be blank"
600
-
601
- sheetdata = np .asarray (self [key ])
602
-
603
- xl .addworksheets (sheetname )
604
-
605
- #TODO: reuse as_table, possibly adding an argument to
606
- # as_table to determine how many dimensions should be "folded"
607
-
608
- # last axis (time) as columns headers (ie the first row)
609
- xl .setRange (sheetname , 2 , 1 ,
610
- (tuple (str (l ) for l in self .axes [- 1 ].labels ),))
611
-
612
- # next to last axis as rows headers (ie the first column)
613
- xl .setRange (sheetname , 1 , 2 ,
614
- tuple ((x ,) for x in self .axes [- 2 ].labels ))
615
- xl .setRange (sheetname , 2 , 2 , sheetdata )
598
+ worksheet = workbook .add_worksheet (sheetname )
599
+ worksheet .write_row (0 , 1 , self .axes [- 1 ].labels )
600
+ worksheet .write_column (1 , 0 , self .axes [- 2 ].labels )
601
+ for row , data in enumerate (np .asarray (self [key ])):
602
+ worksheet .write_row (1 + row , 1 , data )
603
+
616
604
else :
617
- xl .addworksheets ('Sheet1' )
618
-
619
- # last axis (time) as columns headers (ie the first row)
620
- xl .setRange ('Sheet1' , 2 , 1 ,
621
- (tuple (str (l ) for l in self .axes [- 1 ].labels ),))
605
+ worksheet = workbook .add_worksheet ('Sheet1' )
606
+ worksheet .write_row (0 , 1 , self .axes [- 1 ].labels )
622
607
if self .ndim == 2 :
623
- # next to last axis as rows headers (ie the first column )
624
- xl . setRange ( 'Sheet1' , 1 , 2 ,
625
- tuple (( str ( x ),) for x in self . axes [ - 2 ]. labels ))
626
- xl . setRange ( 'Sheet1' , 2 , 2 , np . asarray ( self ))
608
+ worksheet . write_column ( 1 , 0 , self . axes [ - 2 ]. labels )
609
+ for row , data in enumerate ( np . asarray ( self )):
610
+ worksheet . write_row ( 1 + row , 1 , data )
611
+
627
612
628
- xl .save (filename )
629
- xl .close ()
630
- xl .end
631
- del xl
632
613
633
614
def transpose (self , * args ):
634
615
axes_names = set (axis .name for axis in args )
@@ -698,16 +679,17 @@ def df_aslarray(df, na=np.nan):
698
679
699
680
700
681
# CSV functions
701
- def read_csv (filepath , sep = ',' , na = np .nan ):
682
+ def read_csv (filepath , index_col , sep = ',' , na = np .nan ):
702
683
# read the first line to determine how many axes (time excluded) we have
703
- with open (filepath , 'rb' ) as f :
704
- reader = csv .reader (f , delimiter = sep )
705
- header = [parse (cell ) for cell in reader .next ()]
706
- axes_names = [cell for cell in header if isinstance (cell , basestring )]
707
- df = pd .read_csv (filepath , index_col = range (len (axes_names )), sep = sep )
708
- assert df .index .names == axes_names , "%s != %s" % (df .index .names ,
709
- axes_names )
710
- return df_aslarray (df , na )
684
+ # with open(filepath, 'rb') as f:
685
+ # reader = csv.reader(f, delimiter=sep)
686
+ # header = [parse(cell) for cell in reader.next()]
687
+ # axes_names = [cell for cell in header if isinstance(cell, basestring)]
688
+ # df = pd.read_csv(filepath, index_col=range(len(axes_names)), sep=sep)
689
+ # assert df.index.names == axes_names, "%s != %s" % (df.index.names,
690
+ # axes_names)
691
+ df = pd .read_csv (filepath , index_col = index_col , sep = sep )
692
+ return df_aslarray (df .reindex_axis (sorted (df .columns ), axis = 1 ), na )
711
693
712
694
def save_csv (l_array , filepath , sep = ',' , na = np .nan ):
713
695
df = l_array .as_dataframe ()
@@ -735,7 +717,7 @@ def save_excel(l_array, name, filepath):
735
717
736
718
def read_excel (name , filepath , index_col ):
737
719
df = pd .read_excel (filepath , name , index_col = index_col )
738
- return df_aslarray (df )
720
+ return df_aslarray (df . reindex_axis ( sorted ( df . columns ), axis = 1 ) )
739
721
740
722
def SaveMatrices (h5_filename ):
741
723
try :
@@ -795,10 +777,13 @@ def LoadMatrix(h5_filename, matname):
795
777
if __name__ == '__main__' :
796
778
#reg.Collapse('c:/tmp/reg.csv')
797
779
#reg.ToAv('reg.av')
798
- test = read_csv ('ert_eff_ic_a.tsv' , '\t ' , 0 )
780
+ bel = read_csv ('bel.csv' , index_col = [0 ,1 ,2 ,3 ])
781
+ test = read_csv ('ert_eff_ic_a.tsv' , index_col = [0 ,1 ,2 ], sep = '\t ' , na = 0 )
799
782
test .ToCsv ('brol.csv' )
800
783
save_csv (test , 'brolpd.csv' )
784
+ test_csv = read_csv ('brolpd.csv' , index_col = [0 ,1 ,2 ])
801
785
save_excel (test , "TEST" , "test.xls" )
802
786
test_xls = read_excel ("TEST" , "test.xls" , index_col = [0 ,1 ,2 ])
803
787
save_h5 (test , 'test' , 'store.h5' )
804
- test_h5 = read_h5 ('test' , 'store.h5' )
788
+ test_h5 = read_h5 ('test' , 'store.h5' )
789
+ save_h5 (bel , 'bel' , 'store.h5' )
0 commit comments