Skip to content

Commit

Permalink
BUG: ExcelWriter raises exception on PeriodIndex #2240
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan authored and wesm committed Nov 14, 2012
1 parent a97b9b5 commit 687424e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,12 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
encoded_cols = list(cols)
writer.writerow(encoded_cols)

nlevels = getattr(self.index, 'nlevels', 1)
for j, idx in enumerate(self.index):
data_index = self.index
if isinstance(self.index, PeriodIndex):
data_index = self.index.to_timestamp()

nlevels = getattr(data_index, 'nlevels', 1)
for j, idx in enumerate(data_index):
row_fields = []
if index:
if nlevels == 1:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3842,6 +3842,24 @@ def test_to_excel_from_excel(self):
assert_frame_equal(frame, recons)
os.remove(path)

def test_to_excel_periodindex(self):
try:
import xlwt
import xlrd
import openpyxl
except ImportError:
raise nose.SkipTest

for ext in ['xls', 'xlsx']:
path = '__tmp__.' + ext
frame = self.tsframe
xp = frame.resample('M', kind='period')
xp.to_excel(path, 'sht1')

reader = ExcelFile(path)
rs = reader.parse('sht1', index_col=0, parse_dates=True)
assert_frame_equal(xp, rs.to_period('M'))
os.remove(path)

def test_to_excel_multiindex(self):
try:
Expand Down

0 comments on commit 687424e

Please sign in to comment.