Skip to content

Commit 96bed59

Browse files
Added Support for numpy ndarrays and scalars
1 parent 0f850bf commit 96bed59

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

gqlalchemy/utilities.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
import math
16+
import numpy as np
17+
1618
from datetime import datetime, date, time, timedelta
1719
from enum import Enum
1820
from typing import Any, Dict, List, Optional, Tuple, Union
@@ -88,13 +90,16 @@ def to_cypher_value(value: Any, config: NetworkXCypherConfig = None) -> str:
8890
if value_type in [int, float, bool]:
8991
return str(value)
9092

91-
if value_type in [list, set, tuple]:
93+
if value_type in [list, set, tuple, np.ndarray]:
9294
return f"[{', '.join(map(to_cypher_value, value))}]"
9395

9496
if value_type == dict:
9597
lines = ", ".join(f"{k}: {to_cypher_value(v)}" for k, v in value.items())
9698
return f"{{{lines}}}"
9799

100+
if isinstance(value, np.generic):
101+
return to_cypher_value(value.item())
102+
98103
if value is None:
99104
return "null"
100105

tests/test_utilities.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import datetime
1616
import math
17+
import numpy as np
1718
import pytest
1819

1920
from gqlalchemy.utilities import (
@@ -37,6 +38,10 @@
3738
({"k1": 123, "k2": {"subkey1": "abc"}}, "{k1: 123, k2: {subkey1: 'abc'}}"),
3839
(None, "null"),
3940
(True, "True"),
41+
(np.array([1, 2, 3]), "[1, 2, 3]"),
42+
(np.array([1.23, 2.34, 3.45]), "[1.23, 2.34, 3.45]"),
43+
(np.array(["1", "2", "3"]), "['1', '2', '3']"),
44+
(np.array([[1, 2], [3, 4], [4, 5]]), "[[1, 2], [3, 4], [4, 5]]"),
4045
],
4146
)
4247
def test_to_cypher_value(value, cypher_value):

0 commit comments

Comments
 (0)