Skip to content

BUG: Creating series from DataFrame does not serialize Timestamp object to MILLISECONDS from Unix epoch #74

@abbas-mohd56

Description

@abbas-mohd56

Problem
When creating a Highcharts Series object from a pandas Dataframe(), any column with pandas Timestamp() object does not get serialized as milliseconds from the Unix epoch when using the to_dict() and to_json() methods. to_js_literal() does the serialization correctly but it's not ideal when I want to send data through an API. The string returned by the to_js_literal() cannot be parsed with Javascript's JSON.parse() method.

Example

# Sample DataFrame saved in df
timestamp     value
2023-04-01    13669.0
2023-04-02    21534.0
2023-04-03    23207.0

After creating Highcharts series using series = LineSeries.from_pandas(df,property_map={"x": "timestamp", "y": "value"})

# series.to_js_literal() ouputs
"{
  data: [{
  x: 1680307200000.0,
  y: 13669.0
},
{
  x: 1680393600000.0,
  y: 21534.0
},
{
  x: 1680480000000.0,
  y: 23207.0
}],
  type: 'line'
}"

# series.to_dict() ouputs
{'data': [{'x': Timestamp('2023-04-01 00:00:00'), 'y': 13669.0}, {'x': Timestamp('2023-04-02 00:00:00'), 'y': 21534.0}, {'x': Timestamp('2023-04-03 00:00:00'), 'y': 23207.0}], 'type': 'line'}

# series.to_json() just breaks with the following error
TypeError: Object of type Timestamp is not JSON serializable

While the to_js_literal() method gives me what I want, it's difficult to work with when sending data over an API.

Expected behavior
I expected to_json() to have the same output as to_js_literal(), but in JSON format and to_dict() to have serialized the Timestamp() object to milliseconds from Unix epoch.

Environment:

  • OS: Linux
  • Python Version: 3.10.8
  • Highcharts Python Version: 1.2.3

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions