Skip to content

Commit 4290ecc

Browse files
committed
[python][RDF] Add tests to AsRVec and FromNumpy for data of type string
1 parent 1e61f25 commit 4290ecc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

bindings/pyroot/pythonizations/test/rdataframe_makenumpy.py

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ def test_dtypes(self):
2424
df = ROOT.RDF.FromNumpy(data)
2525
self.assertEqual(df.Mean("x").GetValue(), 2)
2626

27+
def test_object_dtype_strings(self):
28+
"""
29+
Test reading numpy arrays with dtype=object containing strings
30+
"""
31+
data = {"x": np.array(["test_string_1", "test_string_2"], dtype=object)}
32+
df = ROOT.RDF.FromNumpy(data)
33+
colnames = df.GetColumnNames()
34+
35+
self.assertIn("x", colnames)
36+
37+
entries = df.AsNumpy()["x"]
38+
self.assertTrue(np.array_equal(entries, np.array(["test_string_1", "test_string_2"], dtype=object)))
39+
2740
def test_multiple_columns(self):
2841
"""
2942
Test reading multiple columns

bindings/pyroot/pythonizations/test/rvec_asrvec.py

+28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ def test_dtypes(self):
4949
self.check_memory_adoption(root_obj, np_obj)
5050
self.check_size(2, root_obj)
5151

52+
53+
def test_object_dtype_strings(self):
54+
"""
55+
Test adoption of numpy arrays with dtype=object and dtype=str containing strings
56+
"""
57+
for dtype in [object, np.str_, '<U13']:
58+
with self.subTest(dtype=dtype):
59+
np_obj = np.array(["test_string_1", "test_string_2"], dtype=dtype)
60+
root_obj = ROOT.VecOps.AsRVec(np_obj)
61+
62+
self.check_size(2, root_obj)
63+
self.assertEqual(root_obj[0], np_obj[0])
64+
self.assertEqual(root_obj[1], np_obj[1])
65+
66+
67+
def test_object_dtype_mixed_types(self):
68+
"""
69+
Test that a TypeError is raised for numpy arrays with dtype=object
70+
containing elements of different types
71+
"""
72+
np_obj = np.array(["string", {}], dtype=object)
73+
74+
with self.assertRaises(TypeError) as context:
75+
ROOT.VecOps.AsRVec(np_obj)
76+
77+
self.assertIn("All elements in the numpy array of objects must be of the same type", str(context.exception))
78+
79+
5280
def test_multidim(self):
5381
"""
5482
Test adoption of multi-dimensional numpy arrays

0 commit comments

Comments
 (0)