Skip to content

Commit 52502dc

Browse files
committed
Fixed handling of numpy.datetime64 objects in DataPointCollection.
1 parent 8349c28 commit 52502dc

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

highcharts_core/options/series/data/collections.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from typing import Optional, List
23
from collections import UserDict
34

@@ -273,7 +274,18 @@ def __setattr__(self, name, value):
273274
try:
274275
setattr(data_points[index], name, value[index])
275276
except validator_errors.CannotCoerceError:
276-
if isinstance(value[index], str) and ',' in value[index]:
277+
if 'datetime64' in value[index].__class__.__name__:
278+
try:
279+
coerced_value = value[index].astype(datetime.datetime)
280+
IS_DATETIME = True
281+
except (ValueError, TypeError):
282+
IS_DATETIME = False
283+
else:
284+
IS_DATETIME = False
285+
286+
if IS_DATETIME:
287+
setattr(data_points[index], name, coerced_value)
288+
elif isinstance(value[index], str) and ',' in value[index]:
277289
coerced_value = value[index].replace(',', '')
278290
setattr(data_points[index], name, coerced_value)
279291
elif checkers.is_numeric(value[index]) or (

tests/options/series/test_area.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,31 @@ def test_LineSeries_from_csv(input_files, filename, property_map, kwargs, expect
31163116
**kwargs)
31173117

31183118

3119+
def test_Bugfix118_LineSeries_from_pandas_with_datetime():
3120+
import datetime as dt
3121+
try:
3122+
import pandas as pd
3123+
HAS_PANDAS = True
3124+
except ImportError:
3125+
HAS_PANDAS = False
3126+
3127+
if HAS_PANDAS:
3128+
# Generate timestamps for the first 5 days of October 2023
3129+
start_date = dt.datetime(2023, 10, 1)
3130+
end_date = dt.datetime(2023, 10, 5)
3131+
date_range = [start_date + dt.timedelta(days=i) for i in range(5)]
3132+
3133+
# Create a list of values
3134+
values = [10, 20, 30, 40, 50]
3135+
3136+
# Create a DataFrame
3137+
df = pd.DataFrame({'Timestamp': date_range, 'Value': values})
3138+
my_series = cls5.from_pandas(df, property_map={"x": "Timestamp", "y": "Value"})
3139+
3140+
assert my_series is not None
3141+
assert isinstance(my_series, cls5)
3142+
3143+
31193144
#### NEXT CLASS
31203145

31213146
@pytest.mark.parametrize('kwargs, error', STANDARD_PARAMS)

0 commit comments

Comments
 (0)