Skip to content

Commit a4cebf5

Browse files
committed
Merge branch 'develop' of github.com:highcharts-for-python/highcharts-core into develop
2 parents 5db5aa8 + 14581a2 commit a4cebf5

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

README.rst

+34
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,40 @@ Hello World, and Basic Usage
329329
href = 'https://www.highchartspython.com')
330330
my_chart.options.credits = my_credits
331331
332+
# EXAMPLE 3.
333+
# Pandas with timeseries
334+
import pandas as pd
335+
import datetime as dt
336+
import numpy as np
337+
df = pd.DataFrame([
338+
{"ref_date": dt.date(2024, 1, 1), "data": 1},
339+
{"ref_date": dt.date(2024, 1, 2), "data": 5},
340+
{"ref_date": dt.date(2024, 1, 3), "data": None},
341+
{"ref_date": dt.date(2024, 1, 4), "data": 4},
342+
{"ref_date": dt.date(2024, 1, 5), "data": None},
343+
])
344+
345+
df['ref_date'] = pd.to_datetime(df['ref_date'])
346+
df.set_index('ref_date', inplace=True)
347+
df.index = (df.index.astype(np.int64) / 10**6).astype(np.int64) # Correcting nanoseconds to epoch, this is crucial for javascript rendering, check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now for more information on this behaviour
348+
349+
from highcharts_core.chart import Chart
350+
chart = Chart.from_pandas(
351+
df=df.reset_index(),
352+
series_type='line',
353+
property_map={
354+
'x': df.index.name,
355+
'y': df.columns.to_list()
356+
}
357+
)
358+
359+
chart.options.x_axis = {
360+
'type': 'datetime'
361+
}
362+
363+
chart.display()
364+
365+
332366
5. Generate the JavaScript Code for Your Chart
333367
=================================================
334368

docs/tutorials/pandas.rst

+46-1
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,49 @@ the ``series_index`` argument tells it to only use the 10th series generated.
495495
the arguments supplied lead to an unambiguous *single* series. If they are ambiguous - meaning they
496496
lead to multiple series generated from the :class:`DataFrame <pandas:pandas.DataFrame>` - then
497497
the method will throw a
498-
:exc:`HighchartsPandasDeserializationError <highcharts_core.errors.HighchartsPandasDeserializationError>`
498+
:exc:`HighchartsPandasDeserializationError <highcharts_core.errors.HighchartsPandasDeserializationError>`
499+
500+
Working with Time Series
501+
======================================
502+
503+
Normally, in the context of pandas one would reference their pandas DataFrame with the timeseries at the index.
504+
505+
Beware that javascript renders time through epoch, and so does the Highcharts javascript library, thus keep in mind this is a requirement!
506+
507+
For further read on the top, check the example under `Date.now() - JavaScript | MDN <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now>`__,
508+
also try playing with your browser console and use something like `Date.now();`, and you should see a very large int represeting the current time in epoch, meaning the time elapsed since 1970, January the first in nanoseconds.
509+
510+
A demonstration is given below.
511+
512+
.. code-block:: python
513+
514+
import pandas as pd
515+
import datetime as dt
516+
import numpy as np
517+
df = pd.DataFrame([
518+
{"ref_date": dt.date(2024, 1, 1), "data": 1},
519+
{"ref_date": dt.date(2024, 1, 2), "data": 5},
520+
{"ref_date": dt.date(2024, 1, 3), "data": None},
521+
{"ref_date": dt.date(2024, 1, 4), "data": 4},
522+
{"ref_date": dt.date(2024, 1, 5), "data": None},
523+
])
524+
525+
df['ref_date'] = pd.to_datetime(df['ref_date'])
526+
df.set_index('ref_date', inplace=True)
527+
df.index = (df.index.astype(np.int64) / 10**6).astype(np.int64)
528+
529+
from highcharts_core.chart import Chart
530+
chart = Chart.from_pandas(
531+
df=df.reset_index(),
532+
series_type='line',
533+
property_map={
534+
'x': df.index.name,
535+
'y': df.columns.to_list()
536+
}
537+
)
538+
539+
chart.options.x_axis = {
540+
'type': 'datetime'
541+
}
542+
543+
chart.display()

0 commit comments

Comments
 (0)