Skip to content

Commit 687424e

Browse files
changhiskhanwesm
authored andcommitted
BUG: ExcelWriter raises exception on PeriodIndex #2240
1 parent a97b9b5 commit 687424e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,12 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
11801180
encoded_cols = list(cols)
11811181
writer.writerow(encoded_cols)
11821182

1183-
nlevels = getattr(self.index, 'nlevels', 1)
1184-
for j, idx in enumerate(self.index):
1183+
data_index = self.index
1184+
if isinstance(self.index, PeriodIndex):
1185+
data_index = self.index.to_timestamp()
1186+
1187+
nlevels = getattr(data_index, 'nlevels', 1)
1188+
for j, idx in enumerate(data_index):
11851189
row_fields = []
11861190
if index:
11871191
if nlevels == 1:

pandas/tests/test_frame.py

+18
Original file line numberDiff line numberDiff line change
@@ -3842,6 +3842,24 @@ def test_to_excel_from_excel(self):
38423842
assert_frame_equal(frame, recons)
38433843
os.remove(path)
38443844

3845+
def test_to_excel_periodindex(self):
3846+
try:
3847+
import xlwt
3848+
import xlrd
3849+
import openpyxl
3850+
except ImportError:
3851+
raise nose.SkipTest
3852+
3853+
for ext in ['xls', 'xlsx']:
3854+
path = '__tmp__.' + ext
3855+
frame = self.tsframe
3856+
xp = frame.resample('M', kind='period')
3857+
xp.to_excel(path, 'sht1')
3858+
3859+
reader = ExcelFile(path)
3860+
rs = reader.parse('sht1', index_col=0, parse_dates=True)
3861+
assert_frame_equal(xp, rs.to_period('M'))
3862+
os.remove(path)
38453863

38463864
def test_to_excel_multiindex(self):
38473865
try:

0 commit comments

Comments
 (0)