Skip to content

Added function to convert Excel date to datetime. #78

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

Merged
merged 1 commit into from
Jan 26, 2014
Merged

Added function to convert Excel date to datetime. #78

merged 1 commit into from
Jan 26, 2014

Conversation

jmcnamara
Copy link
Contributor

This relates to issue #76 and the initial pull request #77.

I've added a function, xldate.xldate_as_datetime(), to return a datetime.datetime object from an Excel serial date/time.

This would be useful for anyone interested in preserving milli/microseconds from an Excel date and would allow us to close Pandas issue: pandas-dev/pandas/issues/5945.

The PR includes tests based on dates and time extracted from Excel files. The function is very slightly slower than the xldate.xldate_as_tuple() function:

$ python
Python 2.7.2 (default, Feb 19 2013, 23:47:31)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from xlrd import xldate
>>> import timeit
>>>
>>> xldate.xldate_as_tuple(41657.75165, 0)
(2014, 1, 18, 18, 2, 23)
>>>
>>> xldate.xldate_as_datetime(41657.75165, 0)
datetime.datetime(2014, 1, 18, 18, 2, 22, 560000)
>>>
>>> timeit.timeit('xldate.xldate_as_tuple(   41657.75165, 0)', setup='from xlrd import xldate', number=170000)
1.045557975769043
>>>
>>> timeit.timeit('xldate.xldate_as_datetime(41657.75165, 0)', setup='from xlrd import xldate', number=170000)
1.0717370510101318
>>>

I didn't update the docs because I wasn't sure if they are auto-generated or not. If you need me to update them I can do that too.

John

self.assertEqual(got, exp)


if __name__ == '__main__':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we lose this boilerplate? nose is the "blessed" way to run these and the boilerplate isn't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I was copying the format of the existing date test cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to clean them up too if you in there ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No thanks. :-)

Added xldate_to_datetime() function to convert from Excel
serial date/time to datetime.datetime object.
@jmcnamara
Copy link
Contributor Author

Hi Chris,

I've updated the PR with the 3 initial test classes merged into 1 and with reduced and documented testcases.

I also profiled the int() and subtraction versus divmod() and the former is faster so I've left that as is:

>>> import timeit
>>>
>>> timeit.timeit('days = int(60376.2345); fraction = 60376.2345 - days', number=10**7)
3.014816999435425
>>>
>>> timeit.timeit('days, fraction = divmod(60376.2345, 1)',               number=10**7)
3.755660057067871
>>>

Hope it passes muster now.

John

cjw296 added a commit that referenced this pull request Jan 26, 2014
Added function to convert Excel date to datetime.
@cjw296 cjw296 merged commit 53d1dd0 into python-excel:master Jan 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants