Last active
October 31, 2016 13:49
-
-
Save sancau/dad3c4dcf494f624f1f956769b7aa999 to your computer and use it in GitHub Desktop.
Parsing excel timestamp format with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXCEL_TIME = 42607.5234143519 | |
| # way 1 | |
| import datetime | |
| def xldate_as_datetime(xldate, datemode): | |
| # datemode: 0 for 1900-based, 1 for 1904-based | |
| return ( | |
| datetime.datetime(1899, 12, 30) | |
| + datetime.timedelta(days=xldate + 1462 * datemode) | |
| ) | |
| way_1_res = xldate_as_datetime(EXCEL_TIME, 0) # datemode 0 | |
| # way 2 | |
| import xlrd | |
| way_2_res = xlrd.xldate_as_tuple(42607.5234143519, 0) # datemode 0 | |
| # way 3 (PROBABLY BEST ONE) | |
| import pandas as pd | |
| way_3_res = pd.to_datetime('1899-12-30') + pd.to_timedelta(EXCEL_TIME, 'D') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment