Permalink
Browse files

BUG: partial handling of booleans in to_excel, fixes #940

  • Loading branch information...
1 parent 4117b4f commit 31ab2925310ced2b51404cfd3b30e0f50a62015e Dieter Vandenbussche committed Mar 19, 2012
Showing with 28 additions and 3 deletions.
  1. +11 −3 pandas/io/parsers.py
  2. +17 −0 pandas/tests/test_frame.py
View
14 pandas/io/parsers.py
@@ -777,6 +777,8 @@ def _writerow_xls(self, row, sheet_name):
sheetrow.write(i,val, self.fm_date)
elif isinstance(val, np.int64):
sheetrow.write(i,int(val))
+ elif isinstance(val, np.bool8):
+ sheetrow.write(i,bool(val))
else:
sheetrow.write(i,val)
row_idx += 1
@@ -791,8 +793,14 @@ def _writerow_xlsx(self, row, sheet_name):
sheet = self.book.create_sheet()
sheet.title = sheet_name
row_idx = 0
-
- sheet.append([int(val) if isinstance(val, np.int64) else val
- for val in row])
+
+ conv_row = []
+ for val in row:
+ if isinstance(val, np.int64):
+ val = int(val)
+ elif isinstance(val, np.bool8):
+ val = bool(val)
+ conv_row.append(val)
+ sheet.append(conv_row)
row_idx += 1
self.sheets[sheet_name] = (sheet, row_idx)
View
17 pandas/tests/test_frame.py
@@ -2518,6 +2518,13 @@ def test_to_excel_from_excel(self):
reader = ExcelFile(path)
recons = reader.parse('test1')
assert_frame_equal(frame, recons)
+
+ #Test reading/writing np.bool8, roundtrip only works for xlsx
+ frame = (DataFrame(np.random.randn(10,2)) >= 0)
+ frame.to_excel(path,'test1')
+ reader = ExcelFile(path)
+ recons = reader.parse('test1').astype(np.bool8)
+ assert_frame_equal(frame, recons)
# Test writing to separate sheets
writer = ExcelWriter(path)
@@ -2545,6 +2552,16 @@ def test_to_excel_from_excel(self):
recons = reader.parse('test1')
assert_frame_equal(self.tsframe, recons)
os.remove(path)
+
+ #Test roundtrip np.bool8, does not seem to work for xls
+ path = '__tmp__.xlsx'
+ frame = (DataFrame(np.random.randn(10,2)) >= 0)
+ frame.to_excel(path,'test1')
+ reader = ExcelFile(path)
+ recons = reader.parse('test1')
+ assert_frame_equal(frame, recons)
+ os.remove(path)
+
def test_to_excel_multiindex(self):
try:

0 comments on commit 31ab292

Please sign in to comment.