@@ -3853,7 +3853,7 @@ def test_to_excel_from_excel(self):
3853
3853
3854
3854
self .frame .to_excel (path ,'test1' )
3855
3855
reader = ExcelFile (path )
3856
- recons = reader .parse ('test1' , index_col = 0 , skiprows = [1 ])
3856
+ recons = reader .parse ('test1' , index_col = 0 , skiprows = [2 ])
3857
3857
assert_frame_equal (self .frame .ix [1 :], recons )
3858
3858
3859
3859
self .frame .to_excel (path ,'test1' ,na_rep = 'NA' )
@@ -3908,6 +3908,28 @@ def test_to_excel_from_excel(self):
3908
3908
xp .columns = col_aliases
3909
3909
assert_frame_equal (xp , rs )
3910
3910
3911
+ # test index_label
3912
+ frame = (DataFrame (np .random .randn (10 ,2 )) >= 0 )
3913
+ frame .to_excel (path , 'test1' , index_label = ['test' ])
3914
+ reader = ExcelFile (path )
3915
+ recons = reader .parse ('test1' ).astype (np .int64 )
3916
+ frame .index .names = ['test' ]
3917
+ self .assertEqual (frame .index .names , recons .index .names )
3918
+
3919
+ frame = (DataFrame (np .random .randn (10 ,2 )) >= 0 )
3920
+ frame .to_excel (path , 'test1' , index_label = ['test' , 'dummy' , 'dummy2' ])
3921
+ reader = ExcelFile (path )
3922
+ recons = reader .parse ('test1' ).astype (np .int64 )
3923
+ frame .index .names = ['test' ]
3924
+ self .assertEqual (frame .index .names , recons .index .names )
3925
+
3926
+ frame = (DataFrame (np .random .randn (10 ,2 )) >= 0 )
3927
+ frame .to_excel (path , 'test1' , index_label = 'test' )
3928
+ reader = ExcelFile (path )
3929
+ recons = reader .parse ('test1' ).astype (np .int64 )
3930
+ frame .index .names = ['test' ]
3931
+ self .assertEqual (frame .index .names , recons .index .names )
3932
+
3911
3933
os .remove (path )
3912
3934
3913
3935
# datetime.date, not sure what to test here exactly
@@ -3993,22 +4015,28 @@ def test_to_excel_multiindex(self):
3993
4015
recons = reader .parse ('test1' )
3994
4016
assert_frame_equal (tsframe , recons )
3995
4017
3996
- # no index
3997
- tsframe .index .names = ['first' , 'second' ]
3998
- tsframe .to_excel (path , 'test1' )
3999
- reader = ExcelFile (path )
4000
- recons = reader .parse ('test1' )
4001
- assert_almost_equal (tsframe .values ,
4002
- recons .ix [:, tsframe .columns ].values )
4003
- self .assertEqual (len (tsframe .columns ) + 2 , len (recons .columns ))
4004
-
4005
- tsframe .index .names = [None , None ]
4006
4018
4007
4019
# no index
4008
- tsframe .to_excel (path , 'test1' , index = False )
4009
- reader = ExcelFile (path )
4010
- recons = reader .parse ('test1' , index_col = None )
4011
- assert_almost_equal (recons .values , self .tsframe .values )
4020
+ #TODO : mention this does not make sence anymore
4021
+ #with the new formatting as we are not alligning colnames and indexlabels
4022
+ #on the same row
4023
+
4024
+ # tsframe.index.names = ['first', 'second']
4025
+ # tsframe.to_excel(path, 'test1')
4026
+ # reader = ExcelFile(path)
4027
+ # recons = reader.parse('test1')
4028
+ # assert_almost_equal(tsframe.values,
4029
+ # recons.ix[:, tsframe.columns].values)
4030
+ # self.assertEqual(len(tsframe.columns) + 2, len(recons.columns))
4031
+
4032
+ # tsframe.index.names = [None, None]
4033
+
4034
+ # # no index
4035
+ # tsframe.to_excel(path, 'test1', index=False)
4036
+ # reader = ExcelFile(path)
4037
+ # recons = reader.parse('test1', index_col=None)
4038
+ # assert_almost_equal(recons.values, self.tsframe.values)
4039
+
4012
4040
self .tsframe .index = old_index # needed if setUP becomes classmethod
4013
4041
4014
4042
# write a big DataFrame
@@ -4071,6 +4099,125 @@ def test_to_excel_unicode_filename(self):
4071
4099
assert_frame_equal (rs , xp )
4072
4100
os .remove (filename )
4073
4101
4102
+ def test_to_excel_styleconverter (self ):
4103
+ from pandas .io .parsers import CellStyleConverter
4104
+ try :
4105
+ import xlwt
4106
+ import openpyxl
4107
+ except ImportError :
4108
+ raise nose .SkipTest
4109
+
4110
+ hstyle = {"font" : {"bold" : True },
4111
+ "borders" : {"top" : "thin" ,
4112
+ "right" : "thin" ,
4113
+ "bottom" : "thin" ,
4114
+ "left" : "thin" },
4115
+ "alignment" : {"horizontal" : "center" }}
4116
+ xls_style = CellStyleConverter .to_xls (hstyle )
4117
+ self .assertTrue (xls_style .font .bold )
4118
+ self .assertEquals (xlwt .Borders .THIN , xls_style .borders .top )
4119
+ self .assertEquals (xlwt .Borders .THIN , xls_style .borders .right )
4120
+ self .assertEquals (xlwt .Borders .THIN , xls_style .borders .bottom )
4121
+ self .assertEquals (xlwt .Borders .THIN , xls_style .borders .left )
4122
+ self .assertEquals (xlwt .Alignment .HORZ_CENTER , xls_style .alignment .horz )
4123
+
4124
+ xlsx_style = CellStyleConverter .to_xlsx (hstyle )
4125
+ self .assertTrue (xlsx_style .font .bold )
4126
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4127
+ xlsx_style .borders .top .border_style )
4128
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4129
+ xlsx_style .borders .right .border_style )
4130
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4131
+ xlsx_style .borders .bottom .border_style )
4132
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4133
+ xlsx_style .borders .left .border_style )
4134
+ self .assertEquals (openpyxl .style .Alignment .HORIZONTAL_CENTER ,
4135
+ xlsx_style .alignment .horizontal )
4136
+
4137
+ def test_to_excel_header_styling (self ):
4138
+
4139
+ import StringIO
4140
+ s = StringIO .StringIO (
4141
+ """Date,ticker,type,value
4142
+ 2001-01-01,x,close,12.2
4143
+ 2001-01-01,x,open ,12.1
4144
+ 2001-01-01,y,close,12.2
4145
+ 2001-01-01,y,open ,12.1
4146
+ 2001-02-01,x,close,12.2
4147
+ 2001-02-01,x,open ,12.1
4148
+ 2001-02-01,y,close,12.2
4149
+ 2001-02-01,y,open ,12.1
4150
+ 2001-03-01,x,close,12.2
4151
+ 2001-03-01,x,open ,12.1
4152
+ 2001-03-01,y,close,12.2
4153
+ 2001-03-01,y,open ,12.1""" )
4154
+ df = read_csv (s , parse_dates = ["Date" ])
4155
+ pdf = df .pivot_table (values = "value" , rows = ["ticker" ],
4156
+ cols = ["Date" , "type" ])
4157
+
4158
+ try :
4159
+ import xlrd
4160
+ import openpyxl
4161
+ from openpyxl .cell import get_column_letter
4162
+ except ImportError :
4163
+ raise nose .SkipTest
4164
+
4165
+ filename = '__tmp__.xls'
4166
+ pdf .to_excel (filename , 'test1' )
4167
+
4168
+
4169
+ wbk = xlrd .open_workbook (filename ,
4170
+ formatting_info = True )
4171
+ self .assertEquals (["test1" ], wbk .sheet_names ())
4172
+ ws = wbk .sheet_by_name ('test1' )
4173
+ self .assertEquals ([(0 , 1 , 5 , 7 ), (0 , 1 , 3 , 5 ), (0 , 1 , 1 , 3 )],
4174
+ ws .merged_cells )
4175
+ for i in range (0 , 2 ):
4176
+ for j in range (0 , 7 ):
4177
+ xfx = ws .cell_xf_index (0 , 0 )
4178
+ cell_xf = wbk .xf_list [xfx ]
4179
+ font = wbk .font_list
4180
+ self .assertEquals (1 , font [cell_xf .font_index ].bold )
4181
+ self .assertEquals (1 , cell_xf .border .top_line_style )
4182
+ self .assertEquals (1 , cell_xf .border .right_line_style )
4183
+ self .assertEquals (1 , cell_xf .border .bottom_line_style )
4184
+ self .assertEquals (1 , cell_xf .border .left_line_style )
4185
+ self .assertEquals (2 , cell_xf .alignment .hor_align )
4186
+
4187
+ os .remove (filename )
4188
+ # test xlsx_styling
4189
+ filename = '__tmp__.xlsx'
4190
+ pdf .to_excel (filename , 'test1' )
4191
+
4192
+ wbk = openpyxl .load_workbook (filename )
4193
+ self .assertEquals (["test1" ], wbk .get_sheet_names ())
4194
+ ws = wbk .get_sheet_by_name ('test1' )
4195
+
4196
+ xlsaddrs = ["%s2" % chr (i ) for i in range (ord ('A' ), ord ('H' ))]
4197
+ xlsaddrs += ["A%s" % i for i in range (1 , 6 )]
4198
+ xlsaddrs += ["B1" , "D1" , "F1" ]
4199
+ for xlsaddr in xlsaddrs :
4200
+ cell = ws .cell (xlsaddr )
4201
+ self .assertTrue (cell .style .font .bold )
4202
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4203
+ cell .style .borders .top .border_style )
4204
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4205
+ cell .style .borders .right .border_style )
4206
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4207
+ cell .style .borders .bottom .border_style )
4208
+ self .assertEquals (openpyxl .style .Border .BORDER_THIN ,
4209
+ cell .style .borders .left .border_style )
4210
+ self .assertEquals (openpyxl .style .Alignment .HORIZONTAL_CENTER ,
4211
+ cell .style .alignment .horizontal )
4212
+
4213
+ mergedcells_addrs = ["C1" , "E1" , "G1" ]
4214
+ for maddr in mergedcells_addrs :
4215
+ self .assertTrue (ws .cell (maddr ).merged )
4216
+
4217
+ os .remove (filename )
4218
+
4219
+
4220
+
4074
4221
def test_info (self ):
4075
4222
io = StringIO ()
4076
4223
self .frame .info (buf = io )
0 commit comments