Incorrect conversion for datetime values (export and import of excel file both done by openpyxl)
Please try the following script:
import datetime
import io
import os
import pandas as pd
def silentrm(filepath):
try:
os.remove(filepath)
except:
pass
for t in [1234567890.123456, 1234567890.000000, 1234567890]:
dt = datetime.datetime.utcfromtimestamp(t)
f = '/dev/shm/file.xlsx'
silentrm(f) # remove file if exists
pd.DataFrame(columns=['column1'], data=[[dt]]).to_excel(f, index=0, engine='openpyxl')
with open(f, 'rb') as g:
df = pd.read_excel(io.BytesIO(g.read()), engine='openpyxl')
if not df.column1[0] == dt:
print(f'Test failed! Expected: {dt}\tActual: {df.column1[0]}')
The test fails with the timestamps 1234567890
(both providing it as int and as float, with up-to-microsecond precision).
Don't know whether this is to be assumed as expected behaviour, maybe related to python's internal datetime storage, or to excel format's date objects representation, or it is an actual bug -- btw, I would expect these conversion issues to happen when floating point values are involved, not when dealing with datetime objects.
Tested using openpyxl 3.0.6 with pandas 1.1.3 and numpy 1.19.4