Skip to content

Commit a1193c0

Browse files
committed
Cleaned up code and tests
- Minor edit to whatsnew entry to use Timedelta and the class tag - Moved test comments inside functions - Cleaned up tests to make them less verbose - Changed timedelta conversion in test and write_cells to use timedelta.total_seconds() - In write_cells, changed timedelta if to elif - Removed Period import that was no longer used due to refactoring of _conv_value
1 parent 3a798eb commit a1193c0

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ I/O
489489
- Bug in :func:`DataFrame.to_latex()` where pairs of braces meant to serve as invisible placeholders were escaped (:issue:`18667`)
490490
- Bug in :func:`read_json` where large numeric values were causing an ``OverflowError`` (:issue:`18842`)
491491
- Bug in :func:`DataFrame.to_parquet` where an exception was raised if the write destination is S3 (:issue:`19134`)
492-
- Type ``Interval`` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`)
493-
- Type ``timedelta`` now supported in :func:`DataFrame.to_excel` for xls file type (:issue:`19242`, :issue:`9155`)
492+
- :class:`Interval` now supported in :func:`DataFrame.to_excel` for all Excel file types (:issue:`19242`)
493+
- :class:`Timedelta` now supported in :func:`DataFrame.to_excel` for xls file type (:issue:`19242`, :issue:`9155`)
494494
-
495495

496496
Plotting

pandas/io/excel.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from pandas.io.common import (_is_url, _urlopen, _validate_header_arg,
2222
get_filepath_or_buffer, _NA_VALUES,
2323
_stringify_path)
24-
# from pandas.core.indexes.period import Period
2524
import pandas._libs.json as json
2625
from pandas.compat import (map, zip, reduce, range, lrange, u, add_metaclass,
2726
string_types, OrderedDict)
@@ -1463,12 +1462,9 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
14631462
num_format_str = self.datetime_format
14641463
elif isinstance(cell.val, date):
14651464
num_format_str = self.date_format
1466-
1467-
if isinstance(cell.val, timedelta):
1465+
elif isinstance(cell.val, timedelta):
14681466
delta = cell.val
1469-
val = (delta.days + (float(delta.seconds) +
1470-
float(delta.microseconds) / 1E6) /
1471-
(60 * 60 * 24))
1467+
val = delta.total_seconds() / float(86400)
14721468

14731469
stylekey = json.dumps(cell.style)
14741470
if num_format_str:

pandas/tests/io/test_excel.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,61 +1440,51 @@ def test_excel_date_datetime_format(self):
14401440
# to use df_expected to check the result
14411441
tm.assert_frame_equal(rs2, df_expected)
14421442

1443-
# GH19242 - test writing Interval without labels
1444-
def test_to_excel_interval_no_label(self):
1443+
def test_to_excel_interval_no_labels(self):
1444+
# GH19242 - test writing Interval without labels
14451445
_skip_if_no_xlrd()
14461446

14471447
with ensure_clean(self.ext) as path:
1448-
rand = np.random.randint(-10, 10, size=(20, 2))
1449-
frame = DataFrame(rand)
1450-
intervals = pd.cut(frame[0], 10)
1451-
frame['new'] = intervals
1452-
frame_expected = DataFrame(rand)
1453-
frame_expected['new'] = intervals.astype(str)
1448+
frame = DataFrame(np.random.randint(-10, 10, size=(20, 1)))
1449+
expected = frame.copy()
1450+
frame['new'] = pd.cut(frame[0], 10)
1451+
expected['new'] = pd.cut(expected[0], 10).astype(str)
14541452
frame.to_excel(path, 'test1')
14551453
reader = ExcelFile(path)
14561454
recons = read_excel(reader, 'test1')
1457-
tm.assert_frame_equal(frame_expected, recons)
1455+
tm.assert_frame_equal(expected, recons)
14581456

1459-
# GH19242 - test writing Interval with labels
1460-
def test_to_excel_interval_w_label(self):
1457+
def test_to_excel_interval_labels(self):
1458+
# GH19242 - test writing Interval with labels
14611459
_skip_if_no_xlrd()
14621460

14631461
with ensure_clean(self.ext) as path:
1464-
rand = np.random.randint(-10, 10, size=(20, 2))
1465-
frame = DataFrame(rand)
1462+
frame = DataFrame(np.random.randint(-10, 10, size=(20, 1)))
1463+
expected = frame.copy()
14661464
intervals = pd.cut(frame[0], 10, labels=['A', 'B', 'C', 'D', 'E',
14671465
'F', 'G', 'H', 'I', 'J'])
14681466
frame['new'] = intervals
1469-
frame_expected = DataFrame(rand)
1470-
frame_expected['new'] = pd.Series(list(intervals))
1467+
expected['new'] = pd.Series(list(intervals))
14711468
frame.to_excel(path, 'test1')
14721469
reader = ExcelFile(path)
14731470
recons = read_excel(reader, 'test1')
1474-
tm.assert_frame_equal(frame_expected, recons)
1471+
tm.assert_frame_equal(expected, recons)
14751472

1476-
# GH 19242 & GH9155 - test writing timedelta to xls
14771473
def test_to_excel_timedelta(self):
1474+
# GH 19242, GH9155 - test writing timedelta to xls
14781475
_skip_if_no_xlrd()
14791476

14801477
with ensure_clean('.xls') as path:
1481-
rand = np.random.randint(-10, 10, size=(20, 1))
1482-
frame = DataFrame(rand, columns=['A'])
1478+
frame = DataFrame(np.random.randint(-10, 10, size=(20, 1)),
1479+
columns=['A'])
1480+
expected = frame.copy()
14831481
frame['new'] = frame['A'].apply(lambda x: timedelta(seconds=x))
1484-
frame_expected = DataFrame(rand, columns=['A'])
1485-
delta = frame_expected['A'].apply(lambda x: timedelta(seconds=x))
1486-
1487-
def timedelta_rep(x):
1488-
return (x.days + (float(x.seconds) +
1489-
float(x.microseconds) / 1E6) /
1490-
(60 * 60 * 24))
1491-
1492-
frame_expected['new'] = delta.apply(timedelta_rep)
1482+
expected['new'] = expected['A'].apply(
1483+
lambda x: timedelta(seconds=x).total_seconds() / float(86400))
14931484
frame.to_excel(path, 'test1')
14941485
reader = ExcelFile(path)
14951486
recons = read_excel(reader, 'test1')
1496-
1497-
tm.assert_frame_equal(frame_expected, recons)
1487+
tm.assert_frame_equal(expected, recons)
14981488

14991489
def test_to_excel_periodindex(self):
15001490
_skip_if_no_xlrd()

0 commit comments

Comments
 (0)