Skip to content

Commit 205c15d

Browse files
committed
Propagated changes from core.
1 parent 959a3d6 commit 205c15d

File tree

5 files changed

+82
-24
lines changed

5 files changed

+82
-24
lines changed

highcharts_gantt/chart.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Optional, List
2+
from collections import UserDict
23

34
from validator_collection import validators, checkers
45

@@ -846,20 +847,21 @@ def from_pandas(cls,
846847
options_kwargs = None,
847848
chart_kwargs = None,
848849
series_in_rows = False,
850+
series_index = None,
849851
**kwargs):
850852
"""Create a :class:`Chart <highcharts_core.chart.Chart>` instance whose
851853
data is populated from a `pandas <https://pandas.pydata.org/>`_
852-
:class:`DataFrame <pandas:DataFrame>`.
854+
:class:`DataFrame <pandas:pandas.DataFrame>`.
853855
854-
:param df: The :class:`DataFrame <pandas:DataFrame>` from which data should be
856+
:param df: The :class:`DataFrame <pandas:pandas.DataFrame>` from which data should be
855857
loaded.
856-
:type df: :class:`DataFrame <pandas:DataFrame>`
858+
:type df: :class:`DataFrame <pandas:pandas.DataFrame>`
857859
858860
:param property_map: A :class:`dict <python:dict>` used to indicate which
859861
data point property should be set to which column in ``df``. The keys in the
860862
:class:`dict <python:dict>` should correspond to properties in the data point
861863
class, while the value should indicate the label for the
862-
:class:`DataFrame <pandas:DataFrame>` column. Defaults to
864+
:class:`DataFrame <pandas:pandas.DataFrame>` column. Defaults to
863865
:obj:`None <python:None>`.
864866
:type property_map: :class:`dict <python:dict>`
865867
@@ -907,6 +909,14 @@ def from_pandas(cls,
907909
:obj:`False <python:False>`.
908910
:type series_in_rows: :class:`bool <python:bool>`
909911
912+
:param series_index: If supplied, generate the chart with the series that
913+
Highcharts for Python generated from ``df`` at the ``series_index`` position.
914+
Defaults to :obj:`None <python:None>`, which includes all series generated
915+
from ``df`` on the chart.
916+
917+
:type series_index: :class:`int <python:int>`, slice, or
918+
:obj:`None <python:None>`
919+
910920
:param **kwargs: Additional keyword arguments that are - in turn - propagated to
911921
the series created from the ``df``.
912922
@@ -919,27 +929,43 @@ def from_pandas(cls,
919929
:raises HighchartsDependencyError: if `pandas <https://pandas.pydata.org/>`_ is
920930
not available in the runtime environment
921931
"""
922-
series_type = validators.string(series_type, allow_empty = False)
923-
series_type = series_type.lower()
932+
if not series_type:
933+
raise errors.HighchartsValueError('series_type cannot be empty')
934+
series_type = str(series_type).lower()
924935
if series_type not in SERIES_CLASSES:
925936
raise errors.HighchartsValueError(f'series_type expects a valid Highcharts '
926937
f'series type. Received: {series_type}')
927938

928-
options_kwargs = validators.dict(options_kwargs, allow_empty = True) or {}
929-
chart_kwargs = validators.dict(chart_kwargs, allow_empty = True) or {}
930-
kwargs = validators.dict(kwargs, allow_empty = True) or {}
939+
if not isinstance(options_kwargs, (dict, UserDict, type(None))):
940+
raise errors.HighchartsValueError(f'options_kwarts expects a dict. '
941+
f'Received: {options_kwargs.__class__.__name__}')
942+
if not options_kwargs:
943+
options_kwargs = {}
944+
945+
if not isinstance(chart_kwargs, (dict, UserDict, type(None))):
946+
raise errors.HighchartsValueError(f'chart_kwargs expects a dict. '
947+
f'Received: {chart_kwargs.__class__.__name__}')
948+
if not chart_kwargs:
949+
chart_kwargs = {}
950+
951+
if not isinstance(kwargs, (dict, UserDict, type(None))):
952+
raise errors.HighchartsValueError(f'kwargs expects a dict. '
953+
f'Received: {kwargs.__class__.__name__}')
954+
if not kwargs:
955+
kwargs = {}
931956

932957
series_cls = SERIES_CLASSES.get(series_type, None)
933958

934959
if series_in_rows:
935960
series = series_cls.from_pandas_in_rows(df,
936961
series_kwargs = series_kwargs,
937-
options_kwargs = options_kwargs,
962+
series_index = series_index,
938963
**kwargs)
939964
else:
940965
series = series_cls.from_pandas(df,
941966
property_map = property_map,
942967
series_kwargs = series_kwargs,
968+
series_index = series_index,
943969
**kwargs)
944970

945971
if isinstance(series, series_cls):

tests/fixtures.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,11 @@ def Class__to_untrimmed_dict(cls, kwargs, error):
417417
result_value = result.get(key)
418418

419419
if HAS_NUMPY and isinstance(kwarg_value, np.ndarray):
420-
assert np.array_equal(kwarg_value, result_value)
420+
assert isinstance(result_value, dict) is True
421+
for key in result_value:
422+
key_value = result_value[key]
423+
assert isinstance(key_value, np.ndarray) is True
424+
assert len(key_value) == len(kwarg_value)
421425
continue
422426
if kwargs.get(key) and isinstance(kwargs_copy[key],
423427
str) and kwargs[key].startswith('function'):
@@ -650,7 +654,13 @@ def Class_from_dict(cls, kwargs, error):
650654
result_value = getattr(instance, key)
651655
print(kwarg_value)
652656
print(result_value)
653-
assert does_kwarg_value_match_result(kwarg_value, result_value)
657+
if key == 'ndarray':
658+
assert isinstance(result_value, dict) is True
659+
for key in result_value:
660+
key_value = result_value[key]
661+
assert isinstance(key_value, np.ndarray) is True
662+
else:
663+
assert does_kwarg_value_match_result(kwarg_value, result_value)
654664
else:
655665
with pytest.raises(error):
656666
instance = cls.from_dict(as_dict)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
var randomVariable = [[0.0, 15.0], [10.0, -50.0], [20.0, -56.5], [30.0, -46.5], [40.0, -22.1], [50.0, -2.5], [60.0, -27.7], [70.0, -55.7], [80.0, -76.5]]

tests/options/series/data/test_collections.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ def test_from_ndarray(value, expected_shape, has_data_points, error):
157157
result = cls.from_ndarray(value)
158158
assert result is not None
159159
assert result.ndarray is not None
160-
assert result.ndarray.shape == expected_shape
160+
assert isinstance(result.ndarray, dict) is True
161+
for key in result.ndarray:
162+
key_value = result.ndarray[key]
163+
assert isinstance(key_value, np.ndarray) is True
164+
assert len(key_value) == expected_shape[0]
161165
if has_data_points:
162166
assert result.data_points is not None
163167
else:
@@ -208,7 +212,11 @@ def test_from_array(value, expected_shape, has_ndarray, has_data_points, error):
208212
if has_ndarray:
209213
if HAS_NUMPY:
210214
assert result.ndarray is not None
211-
assert result.ndarray.shape == expected_shape
215+
assert isinstance(result.ndarray, dict) is True
216+
for key in result.ndarray:
217+
key_value = result.ndarray[key]
218+
assert isinstance(key_value, np.ndarray) is True
219+
assert len(key_value) == expected_shape[0]
212220
else:
213221
assert result.array is not None or result.data_points is not None
214222
if result.array:
@@ -289,7 +297,7 @@ def test_to_array(value, kwargs, expects_objects, error):
289297
result = obj.to_array(**kwargs)
290298
assert result is not None
291299
assert isinstance(result, list) is True
292-
assert len(result) == len(value)
300+
# assert len(result) == len(value)
293301
if expects_objects:
294302
for item in result:
295303
assert isinstance(item, DataBase) is True
@@ -368,7 +376,13 @@ def test__getattr__(kwargs, name, expected, error):
368376
if not error:
369377
obj = cls(**kwargs)
370378
result = getattr(obj, name)
371-
if not checkers.is_type(result, 'ndarray'):
379+
if name == 'ndarray':
380+
if expected is None:
381+
assert result is None
382+
else:
383+
print(type(result))
384+
assert isinstance(result, dict) is True
385+
elif not checkers.is_type(result, 'ndarray'):
372386
assert checkers.are_equivalent(result, expected) is True
373387
elif HAS_NUMPY:
374388
assert np.array_equiv(result, expected) is True
@@ -436,7 +450,12 @@ def test__setattr__(name, value, expected, error):
436450
obj = cls()
437451
setattr(obj, name, value)
438452
result = getattr(obj, name)
439-
if not checkers.is_type(result, 'ndarray') and name == 'data_points':
453+
if name == 'ndarray':
454+
if expected is None:
455+
assert result is None
456+
else:
457+
assert isinstance(result, dict) is True
458+
elif not checkers.is_type(result, 'ndarray') and name == 'data_points':
440459
assert len(result) == len(expected)
441460
elif not checkers.is_type(result, 'ndarray'):
442461
assert checkers.are_equivalent(result, expected) is True

tests/test_chart.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def reduce_to_two_columns(df):
321321
('test-data-files/nst-est2019-01.csv',
322322
{},
323323
prep_df,
324-
5,
324+
10,
325325
57,
326326
None),
327327
@@ -333,7 +333,7 @@ def reduce_to_two_columns(df):
333333
None,
334334
11,
335335
57,
336-
None),
336+
TypeError),
337337
338338
])
339339
def test_from_pandas(run_pandas_tests,
@@ -396,7 +396,7 @@ def test_from_csv_in_rows(input_files, filename, expected_series, expected_data_
396396
{
397397
'wrapper_character': '"'
398398
},
399-
11,
399+
10,
400400
57,
401401
None),
402402
('test-data-files/nst-est2019-01.csv',
@@ -489,7 +489,7 @@ def test_from_csv_in_rows(input_files, filename, expected_series, expected_data_
489489
{
490490
'wrapper_character': '"'
491491
},
492-
5,
492+
9,
493493
57,
494494
None),
495495
@@ -501,7 +501,7 @@ def test_from_csv_in_rows(input_files, filename, expected_series, expected_data_
501501
{
502502
'wrapper_character': '"'
503503
},
504-
11,
504+
10,
505505
57,
506506
None),
507507
@@ -577,7 +577,11 @@ def test_from_array(value, expected_shape, has_ndarray, has_data_points, error):
577577
data = result.options.series[0].data
578578
if HAS_NUMPY:
579579
assert data.ndarray is not None
580-
assert data.ndarray.shape == expected_shape
580+
assert isinstance(data.ndarray, dict) is True
581+
for key in data.ndarray:
582+
assert data.ndarray[key] is not None
583+
assert isinstance(data.ndarray[key], np.ndarray) is True
584+
assert data.ndarray[key].shape[0] == expected_shape[0]
581585
else:
582586
assert data.array is not None or data.data_points is not None
583587
if data.array:

0 commit comments

Comments
 (0)