Skip to content
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

from_pandas method raises HighchartsValueError when a DataFrame column is a datetime object #118

Closed
abbas-mohd56 opened this issue Oct 17, 2023 · 2 comments · Fixed by #120
Assignees
Labels
bug Something isn't working
Milestone

Comments

@abbas-mohd56
Copy link

Problem
Creating a Series object using the from_pandas method raises a HighchartsValueError if the column is of type datetime. The problem started appearing after upgrading highcharts_core to v1.4.0.

Imagine a DataFrame df with the following data:

    Timestamp  Value
0  2023-10-01     10
1  2023-10-02     20
2  2023-10-03     30
3  2023-10-04     40
4  2023-10-05     50

When creating a LineSeries object as shown

from highcharts_core.options.series.area import LineSeries
LineSeries.from_pandas(df, property_map={"x": "timestamp", "y": "value"})

The following error is raised

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/highcharts_core/options/series/data/collections.py", line 274, in __setattr__
    setattr(data_points[index], name, value[index])
  File "/usr/local/lib/python3.10/site-packages/highcharts_core/options/series/data/cartesian.py", line 134, in x
    value = validators.string(value)
  File "/usr/local/lib/python3.10/site-packages/validator_collection/_decorators.py", line 51, in func_wrapper
    return func(*args, **updated_kwargs)
  File "/usr/local/lib/python3.10/site-packages/validator_collection/validators.py", line 241, in string
    raise errors.CannotCoerceError('value (%s) was not coerced to a string' % value)
validator_collection.errors.CannotCoerceError: value (2023-10-01T00:00:00.000000000) was not coerced to a string

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/highcharts_core/options/series/base.py", line 2108, in from_pandas
    series_list = cls._from_pandas_multi_map(df,
  File "/usr/local/lib/python3.10/site-packages/highcharts_core/options/series/base.py", line 1859, in _from_pandas_multi_map
    setattr(collection_instance, key, prop_array)
  File "/usr/local/lib/python3.10/site-packages/highcharts_core/options/series/data/collections.py", line 285, in __setattr__
    raise errors.HighchartsValueError(
highcharts_core.errors.HighchartsValueError: Unable to set x to 2023-10-01T00:00:00.000000000. If using a helper method, this is likely due to mismatched columns. Please review your input data.

To Repoduce run the following code

import pandas as pd
import datetime as dt
from highcharts_core.options.series.area import LineSeries

# Generate timestamps for the first 5 days of October 2023
start_date = dt.datetime(2023, 10, 1)
end_date = dt.datetime(2023, 10, 5)
date_range = [start_date + dt.timedelta(days=i) for i in range(5)]

# Create a list of values
values = [10, 20, 30, 40, 50]

# Create a DataFrame
df = pd.DataFrame({'Timestamp': date_range, 'Value': values})
LineSeries.from_pandas(df, property_map={"x": "Timestamp", "y": "Value"})

Environment:

  • OS: Linux
  • Python Version: 3.10.8
  • Highcharts Python Version: 1.4.0
@abbas-mohd56 abbas-mohd56 added the bug Something isn't working label Oct 17, 2023
@hcpchris hcpchris added this to the 1.4.1 milestone Oct 17, 2023
@hcpchris
Copy link
Collaborator

Great catch, @abbas-mohd56 ! This is definitely a bug introduced with v.1.4.0. I'll patch it in v.1.4.1 and likely release the patch later today.

@hcpchris
Copy link
Collaborator

@abbas-mohd56 : FYI - this issue should have been fixed in the new version (v.1.4.1) that was just released. Thanks for letting us know about the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants