Skip to content

Commit 57106a3

Browse files
committed
update core tests
1 parent c717261 commit 57106a3

5 files changed

Lines changed: 46 additions & 13 deletions

File tree

holoviews/core/util/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,9 +860,7 @@ def asarray(arraylike, strict=True):
860860
return arraylike
861861
elif isinstance(arraylike, list):
862862
return np.asarray(arraylike, dtype=object)
863-
elif not isinstance(arraylike, np.ndarray) and isinstance(arraylike, arraylike_types):
864-
return arraylike.values
865-
elif hasattr(arraylike, '__array__'):
863+
elif hasattr(arraylike, '__array__') or isinstance(arraylike, arraylike_types):
866864
return np.asarray(arraylike)
867865
elif strict:
868866
raise ValueError(f'Could not convert {type(arraylike)} type to array')

holoviews/core/util/dependencies.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,15 @@ def __version__(self):
112112
NUMPY_GE_2_0_0 = NUMPY_VERSION >= (2, 0, 0)
113113
PANDAS_GE_2_1_0 = PANDAS_VERSION >= (2, 1, 0)
114114
PANDAS_GE_2_2_0 = PANDAS_VERSION >= (2, 2, 0)
115-
116-
__all__ = ["NUMPY_GE_2_0_0", "NUMPY_VERSION", "PANDAS_GE_2_1_0", "PANDAS_GE_2_2_0", "PANDAS_VERSION", "PARAM_VERSION", "_LazyModule"]
115+
PANDAS_GE_3_0_0 = PANDAS_VERSION >= (3, 0, 0)
116+
117+
__all__ = [
118+
"NUMPY_GE_2_0_0",
119+
"NUMPY_VERSION",
120+
"PANDAS_GE_2_1_0",
121+
"PANDAS_GE_2_2_0",
122+
"PANDAS_GE_3_0_0",
123+
"PANDAS_VERSION",
124+
"PARAM_VERSION",
125+
"_LazyModule",
126+
]

holoviews/operation/element.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from ..core.util import (
3333
datetime_types,
3434
dt_to_int,
35+
dtype_kind,
3536
group_sanitizer,
3637
is_cupy_array,
3738
is_dask_array,
@@ -1065,16 +1066,23 @@ class interpolate_curve(Operation):
10651066

10661067
_per_element = True
10671068

1069+
@staticmethod
1070+
def _get_dtype(obj):
1071+
if dtype_kind(obj) == "O":
1072+
# Handle non-numpy objects like Pandas 3.0 StringArray
1073+
return "O"
1074+
return obj.dtype
1075+
10681076
@classmethod
10691077
def pts_to_prestep(cls, x, values):
10701078
steps = np.zeros(2 * len(x) - 1)
1071-
value_steps = tuple(np.empty(2 * len(x) - 1, dtype=v.dtype) for v in values)
1079+
value_steps = tuple(np.empty(2 * len(x) - 1, dtype=cls._get_dtype(v)) for v in values)
10721080

10731081
steps[0::2] = x
10741082
steps[1::2] = steps[0:-2:2]
10751083

10761084
val_arrays = []
1077-
for v, s in zip(values, value_steps, strict=None):
1085+
for v, s in zip(values, value_steps, strict=True):
10781086
s[0::2] = v
10791087
s[1::2] = s[2::2]
10801088
val_arrays.append(s)
@@ -1084,13 +1092,13 @@ def pts_to_prestep(cls, x, values):
10841092
@classmethod
10851093
def pts_to_midstep(cls, x, values):
10861094
steps = np.zeros(2 * len(x))
1087-
value_steps = tuple(np.empty(2 * len(x), dtype=v.dtype) for v in values)
1095+
value_steps = tuple(np.empty(2 * len(x), dtype=cls._get_dtype(v)) for v in values)
10881096

10891097
steps[1:-1:2] = steps[2::2] = x[:-1] + (x[1:] - x[:-1])/2
10901098
steps[0], steps[-1] = x[0], x[-1]
10911099

10921100
val_arrays = []
1093-
for v, s in zip(values, value_steps, strict=None):
1101+
for v, s in zip(values, value_steps, strict=True):
10941102
s[0::2] = v
10951103
s[1::2] = s[0::2]
10961104
val_arrays.append(s)
@@ -1100,13 +1108,13 @@ def pts_to_midstep(cls, x, values):
11001108
@classmethod
11011109
def pts_to_poststep(cls, x, values):
11021110
steps = np.zeros(2 * len(x) - 1)
1103-
value_steps = tuple(np.empty(2 * len(x) - 1, dtype=v.dtype) for v in values)
1111+
value_steps = tuple(np.empty(2 * len(x) - 1, dtype=cls._get_dtype(v)) for v in values)
11041112

11051113
steps[0::2] = x
11061114
steps[1::2] = steps[2::2]
11071115

11081116
val_arrays = []
1109-
for v, s in zip(values, value_steps, strict=None):
1117+
for v, s in zip(values, value_steps, strict=True):
11101118
s[0::2] = v
11111119
s[1::2] = s[0:-2:2]
11121120
val_arrays.append(s)

holoviews/plotting/bokeh/path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ...core import util
88
from ...core.dimension import Dimension
9-
from ...core.util import dtype_kind
9+
from ...core.util import arraylike_types, dtype_kind
1010
from ...element import Contours, Polygons
1111
from ...util.transform import dim
1212
from .callbacks import PolyDrawCallback, PolyEditCallback
@@ -50,7 +50,7 @@ def _element_transform(self, transform, element, ranges):
5050
data = super()._element_transform(transform, element, ranges)
5151
new_data = []
5252
for d in data:
53-
if isinstance(d, np.ndarray) and len(d) == 1:
53+
if isinstance(d, (np.ndarray, arraylike_types)) and len(d) == 1:
5454
new_data.append(d[0])
5555
else:
5656
new_data.append(d)

holoviews/tests/core/data/test_pandasinterface.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from holoviews.core.data.pandas import PandasInterface
88
from holoviews.core.dimension import Dimension
99
from holoviews.core.spaces import HoloMap
10+
from holoviews.core.util.dependencies import PANDAS_GE_3_0_0
1011
from holoviews.element import Distribution, Points, Scatter
1112

1213
from .base import HeterogeneousColumnTests, InterfaceTests
@@ -172,6 +173,14 @@ def test_dataset_range_with_object_index(self):
172173
ds = Dataset(df, kdims='index')
173174
assert ds.range('index') == ('A', 'D')
174175

176+
def test_dataset_dataset_ht_dtypes(self):
177+
ds = self.table
178+
string_dtype = pd.StringDtype(na_value=np.nan) if PANDAS_GE_3_0_0 else np.dtype('object')
179+
assert ds.interface.dtype(ds, 'Gender') == string_dtype
180+
assert ds.interface.dtype(ds, 'Age') == np.dtype(int)
181+
assert ds.interface.dtype(ds, 'Weight') == np.dtype(int)
182+
assert ds.interface.dtype(ds, 'Height') == np.dtype('float64')
183+
175184

176185
class PandasInterfaceTests(BasePandasInterfaceTests):
177186

@@ -412,6 +421,14 @@ def test_groupby_one_index_one_column(self):
412421
for k, v in grouped.items():
413422
pd.testing.assert_frame_equal(v.data, ds.select(values=k).data)
414423

424+
def test_dataset_dataset_ht_dtypes(self):
425+
ds = self.table
426+
string_dtype = pd.StringDtype(na_value=np.nan) if PANDAS_GE_3_0_0 else np.dtype('object')
427+
assert ds.interface.dtype(ds, 'Gender') == string_dtype
428+
assert ds.interface.dtype(ds, 'Age') == np.dtype(int)
429+
assert ds.interface.dtype(ds, 'Weight') == np.dtype(int)
430+
assert ds.interface.dtype(ds, 'Height') == np.dtype('float64')
431+
415432
def test_regression_no_auto_index(self):
416433
# https://github.com/holoviz/holoviews/issues/6298
417434

0 commit comments

Comments
 (0)