Skip to content

Commit 4800d9d

Browse files
committed
Fixed typos in docs.
1 parent a4cebf5 commit 4800d9d

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

README.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Hello World, and Basic Usage
330330
my_chart.options.credits = my_credits
331331
332332
# EXAMPLE 3.
333-
# Pandas with timeseries
333+
# Pandas with time series
334334
import pandas as pd
335335
import datetime as dt
336336
import numpy as np
@@ -344,7 +344,11 @@ Hello World, and Basic Usage
344344
345345
df['ref_date'] = pd.to_datetime(df['ref_date'])
346346
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
347+
348+
df.index = (df.index.astype(np.int64) / 10**6).astype(np.int64)
349+
# Correcting nanoseconds to epoch, which is crucial for javascript rendering,
350+
# See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
351+
# for more information on this behaviour
348352
349353
from highcharts_core.chart import Chart
350354
chart = Chart.from_pandas(

docs/tutorials/pandas.rst

+51-44
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,56 @@ What we did here is we added a ``series_index`` argument, which tells **Highchar
412412
include the series found at that index in the resulting chart. In this case, we supplied a :func:`slice <python:slice>`
413413
object, which operates just like ``list_of_series[7:10]``. The result only returns those series between index 7 and 10.
414414

415+
Working with Time Series
416+
======================================
417+
418+
Normally, in the context of Pandas one would reference their Pandas DataFrame with the time series at the index.
419+
However, JavaScript (and the Highcharts JS library) renders time in relationship to the Unix epoch of January 1, 1970.
420+
421+
.. seealso::
422+
423+
To see how this behaves, check the example under
424+
`Date.now() - JavaScript | MDN <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now>`__
425+
and try playing with your browser console with a command like ``Date.now();``. You should see a very large integer representing the number of nanoseconds elapsed since the first of January. 1970.
426+
427+
While Highcharts for Python will automatically convert NumPy `datetime64 <numpy:numpy.datetime64>` values into their
428+
appropriate integers, you may want to do this conversion yourself. A demonstration is given below:
429+
430+
.. code-block:: python
431+
432+
import pandas as pd
433+
import datetime as dt
434+
import numpy as np
435+
df = pd.DataFrame([
436+
{"ref_date": dt.date(2024, 1, 1), "data": 1},
437+
{"ref_date": dt.date(2024, 1, 2), "data": 5},
438+
{"ref_date": dt.date(2024, 1, 3), "data": None},
439+
{"ref_date": dt.date(2024, 1, 4), "data": 4},
440+
{"ref_date": dt.date(2024, 1, 5), "data": None},
441+
])
442+
443+
df['ref_date'] = pd.to_datetime(df['ref_date'])
444+
df.set_index('ref_date', inplace=True)
445+
446+
df.index = (df.index.astype(np.int64) / 10**6).astype(np.int64)
447+
# This line is the important one! It converts the datetime64 values into their epoch-based millisecond equivalents.
448+
449+
from highcharts_core.chart import Chart
450+
chart = Chart.from_pandas(
451+
df=df.reset_index(),
452+
series_type='line',
453+
property_map={
454+
'x': df.index.name,
455+
'y': df.columns.to_list()
456+
}
457+
)
458+
459+
chart.options.x_axis = {
460+
'type': 'datetime'
461+
}
462+
463+
chart.display()
464+
415465
------------------------
416466

417467
**********************************************************************
@@ -467,6 +517,7 @@ Filtering Series Created from Rows
467517
my_series = LineSeries.from_pandas_in_rows(df, series_index = slice(0, 5))
468518
469519
This will return the first five series in the list of 57.
520+
470521
--------------------------
471522

472523
***********************************************************
@@ -497,47 +548,3 @@ the ``series_index`` argument tells it to only use the 10th series generated.
497548
the method will throw a
498549
:exc:`HighchartsPandasDeserializationError <highcharts_core.errors.HighchartsPandasDeserializationError>`
499550

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)