Skip to content

BUG: Fix for Timestamp handling in xlwt and xlsxwriter engines. #9175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Bug Fixes
- Bug in ``pivot`` and `unstack`` where ``nan`` values would break index alignment (:issue:`7466`)


- Fixed bug where minutes and seconds components were zeroed when writing
Timestamp objects to Excel using xlwt and xlsxwriter (:issue:`9138`).



Expand Down
9 changes: 9 additions & 0 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pandas.compat import map, zip, reduce, range, lrange, u, add_metaclass
from pandas.core import config
from pandas.core.common import pprint_thing
from pandas import Timestamp
import pandas.compat as compat
import pandas.compat.openpyxl_compat as openpyxl_compat
import pandas.core.common as com
Expand Down Expand Up @@ -1118,6 +1119,10 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
val = _conv_value(cell.val)

num_format_str = None

if isinstance(val, Timestamp):
val = val.to_pydatetime()

if isinstance(cell.val, datetime.datetime):
num_format_str = self.datetime_format
elif isinstance(cell.val, datetime.date):
Expand Down Expand Up @@ -1239,6 +1244,10 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):

for cell in cells:
num_format_str = None

if isinstance(cell.val, Timestamp):
cell.val = cell.val.to_pydatetime()

if isinstance(cell.val, datetime.datetime):
num_format_str = self.datetime_format
elif isinstance(cell.val, datetime.date):
Expand Down
22 changes: 22 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,28 @@ def test_swapped_columns(self):
tm.assert_series_equal(write_frame['A'], read_frame['A'])
tm.assert_series_equal(write_frame['B'], read_frame['B'])

def test_datetimes(self):
# Test writing and reading datetimes. For issue #9139.
_skip_if_no_xlrd()

datetimes = [datetime(2013, 1, 13, 1, 2, 3),
datetime(2013, 1, 13, 2, 45, 56),
datetime(2013, 1, 13, 4, 29, 49),
datetime(2013, 1, 13, 6, 13, 42),
datetime(2013, 1, 13, 7, 57, 35),
datetime(2013, 1, 13, 9, 41, 28),
datetime(2013, 1, 13, 11, 25, 21),
datetime(2013, 1, 13, 13, 9, 14),
datetime(2013, 1, 13, 14, 53, 7),
datetime(2013, 1, 13, 16, 37, 0),
datetime(2013, 1, 13, 18, 20, 52)]

with ensure_clean(self.ext) as path:
write_frame = DataFrame.from_items([('A', datetimes)])
write_frame.to_excel(path, 'Sheet1')
read_frame = read_excel(path, 'Sheet1', header=0)

tm.assert_series_equal(write_frame['A'], read_frame['A'])

def raise_wrapper(major_ver):
def versioned_raise_wrapper(orig_method):
Expand Down