-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
to_excel incorrectly sets time to midnight for exported datetime column #9139
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
Comments
@scls19fr Thanks for the report! Pandas supports a couple of external modules for writing Excel modules, so this might also depend on which one you're using. Here is where you can find the relevant source code if you're interested in trying your hand at a fix: https://github.com/pydata/pandas/blob/master/pandas/io/excel.py |
This was also reported here: http://stackoverflow.com/questions/27548833/pandas-to-excel-in-version-0-15-2-not-working-for-datetime-objects But, as I posted over there, I cannot reproduce this. @scls19fr Can you show |
|
I did
|
@scls19fr Thanks, I can now confirm the same: working for |
I am the author of XlsxWriter. Here is my analysis of what is happening in this issue starting with the smaller example above: import requests
from pandas.io.json import json_normalize
import json
import pandas as pd
response = requests.get("http://api.openweathermap.org/data/2.5/history/station?start=1356220800&end=1356307200&type=hour&id=5530")
df = json_normalize(json.loads(response.text)['list'])
df['dt'] = pd.to_datetime(df['dt'], unit='s')
df = df[['dt', 'main.temp.ma']] If we take one of these values as an example: >>> timestamp = df['dt'][23]
>>> timestamp
Timestamp('2012-12-23 23:00:00') An Excel date is stored as a delta of days from an epoch (the integer part of the number) and a percentage of the day for the time in milliseconds (the float part). XlsxWriter and afaik Xlwt use a calculation like the following: >>> import datetime
>>> epoch = datetime.datetime(1899, 12, 31, 0, 0, 0)
>>> excel_datetime = timestamp - epoch
>>> excel_datetime
Timedelta('41265 days 23:00:00')
>>> excel_datetime.days()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> excel_datetime.days
41265
>>> excel_datetime.seconds
0
>>> excel_datetime.microseconds
0 As you can see the seconds part of the For comparison here is how a >>> mydate = datetime.datetime(2012, 12, 23, 23, 0, 0)
>>> excel_datetime = mydate - epoch
>>> excel_datetime
datetime.timedelta(41265, 82800)
>>> excel_datetime.days
41265
>>> excel_datetime.seconds
82800
>>> excel_datetime.microseconds
0 XlsxWriter performs this epoch calculation because the >>> isinstance(timestamp, datetime.datetime)
True So for me it looks like the issue here is that the pandas There are a few workable solutions but I'll hold off until someone familiar with the John |
So the problem is the conversion is relying upon an implementation detail of There are many ways to do this.
|
@jmcnamara This is very helpful, thanks! For an explanation of how This is certainly an unfortunate side-effect of overriding super-class behavior in a subclass. I recall raising this issue in the pull request adding The simplest fix would be to use |
FWIW, I think pandas should fix this issue on our side (since we broke it with our strange |
this is not an upstream issue |
@jreback wait... wat? |
I've added a PR for this based on coercing the Even if you choose a different solution you can use the testcase. I was surprised that the existing testcases didn't pick up this issue but none of them actually test a roundtrip of a date and time. |
@neirbowj sorry, I meant xlwt/xlsxwriter |
Was it actually fixed? It seems like I still get this issue. For example:
and the result is: As you can see the exported timestamp is different than the given one. BTW: using |
well if you use master it is. This will be in the forthcoming 0.16.0 release. |
Hello,
I try this:
It displays:
but if we look at Excel file
dt
column looks liketo_csv
method is okKind regards
The text was updated successfully, but these errors were encountered: