Skip to content

Commit 87b0c8a

Browse files
committed
Implemented utility_functions.datetime64_to_datetime.
1 parent fc69697 commit 87b0c8a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

highcharts_core/utility_functions.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Collection of utility functions used across the library."""
22
import csv
3+
import datetime
34
import os
45
import string
56
import random
@@ -907,4 +908,29 @@ def dict_to_ndarray(as_dict):
907908
columns = [as_dict[key] for key in as_dict]
908909
as_ndarray = np.column_stack(columns)
909910

910-
return as_ndarray
911+
return as_ndarray
912+
913+
914+
def datetime64_to_datetime(dt64):
915+
"""Convert a NumPy :class:`datetime64 <numpy:numpy.datetime64>` to a Python
916+
:class:`datetime <python:datetime.datetime>`.
917+
918+
:param dt64: The NumPy :class:`datetime64 <numpy:numpy.datetime64>` to convert.
919+
:type dt64: :class:`numpy.datetime64 <numpy:numpy.datetime64>`
920+
921+
:returns: A Python :class:`datetime <python:datetime.datetime>` instance.
922+
:rtype: :class:`datetime <python:datetime.datetime>`
923+
924+
:raises HighchartsDependencyError: if NumPy is not available in the runtime
925+
environment
926+
927+
"""
928+
if not HAS_NUMPY:
929+
raise errors.HighchartsDependencyError('NumPy is required for this feature. '
930+
'It was not found in your runtime '
931+
'environment. Please make sure it is '
932+
'installed in your runtime '
933+
'environment.')
934+
timestamp = (dt64 - np.datetime64("1970-01-01T00:00:00")) / np.timedelta64(1, "s")
935+
936+
return datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)

0 commit comments

Comments
 (0)