Skip to content

Commit 0fad11b

Browse files
authored
Merge pull request #124 from adamnsch/separate-property-columns-bug
Fix separate property columns for node list properties
2 parents e4c260b + e56e9be commit 0fad11b

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

changelog/1.2.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
## Bug fixes
1111

12+
* Fixed a bug where the `separate_property_columns=True` option of `gds.graph.streamNodeProperties` did not handle list node properties correctly.
13+
1214

1315
## Improvements
1416

graphdatascience/graph/graph_proc_runner.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ def streamNodeProperties(
113113

114114
# new format was requested, but the query was run via Cypher
115115
if separate_property_columns and "propertyValue" in result.keys():
116-
return result.pivot_table("propertyValue", "nodeId", columns="nodeProperty").reset_index()
116+
result = result.pivot(index="nodeId", columns="nodeProperty", values="propertyValue")
117+
result = result.reset_index()
118+
result.columns.name = None
117119
# old format was requested but the query was run via Arrow
118120
elif not separate_property_columns and "propertyValue" not in result.keys():
119-
return result.melt(id_vars=["nodeId"]).rename(
121+
result = result.melt(id_vars=["nodeId"]).rename(
120122
columns={"variable": "nodeProperty", "value": "propertyValue"}
121123
)
122124

@@ -147,12 +149,16 @@ def streamRelationshipProperties(
147149

148150
# new format was requested, but the query was run via Cypher
149151
if separate_property_columns and "propertyValue" in result.keys():
150-
return result.pivot_table(
151-
"propertyValue", ["sourceNodeId", "targetNodeId", "relationshipType"], columns="relationshipProperty"
152-
).reset_index()
152+
result = result.pivot(
153+
index=["sourceNodeId", "targetNodeId", "relationshipType"],
154+
columns="relationshipProperty",
155+
values="propertyValue",
156+
)
157+
result = result.reset_index()
158+
result.columns.name = None
153159
# old format was requested but the query was run via Arrow
154160
elif not separate_property_columns and "propertyValue" not in result.keys():
155-
return result.melt(id_vars=["sourceNodeId", "targetNodeId", "relationshipType"]).rename(
161+
result = result.melt(id_vars=["sourceNodeId", "targetNodeId", "relationshipType"]).rename(
156162
columns={"variable": "relationshipProperty", "value": "propertyValue"}
157163
)
158164

graphdatascience/tests/integration/test_graph_ops.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def run_around_tests(runner: Neo4jQueryRunner) -> Generator[None, None, None]:
2020
runner.run_query(
2121
"""
2222
CREATE
23-
(a: Node {x: 1, y: 2}),
24-
(b: Node {x: 2, y: 3}),
25-
(c: Node {x: 3, y: 4}),
23+
(a: Node {x: 1, y: 2, z: [42]}),
24+
(b: Node {x: 2, y: 3, z: [1337]}),
25+
(c: Node {x: 3, y: 4, z: [9]}),
2626
(a)-[:REL {relX: 4, relY: 5}]->(b),
2727
(a)-[:REL {relX: 5, relY: 6}]->(c),
2828
(b)-[:REL {relX: 6, relY: 7}]->(c),
@@ -217,13 +217,17 @@ def test_graph_streamNodeProperties_without_arrow(gds_without_arrow: GraphDataSc
217217
def test_graph_streamNodeProperties_without_arrow_separate_property_columns(
218218
gds_without_arrow: GraphDataScience,
219219
) -> None:
220-
G, _ = gds_without_arrow.graph.project(GRAPH_NAME, {"Node": {"properties": ["x", "y"]}}, "*")
220+
G, _ = gds_without_arrow.graph.project(GRAPH_NAME, {"Node": {"properties": ["x", "z"]}}, "*")
221221

222-
result = gds_without_arrow.graph.streamNodeProperties(G, ["x", "y"], separate_property_columns=True, concurrency=2)
222+
result = gds_without_arrow.graph.streamNodeProperties(G, ["x", "z"], separate_property_columns=True, concurrency=2)
223+
224+
assert list(result.keys()) == ["nodeId", "x", "z"]
223225

224-
assert list(result.keys()) == ["nodeId", "x", "y"]
225226
assert {e for e in result["x"]} == {1, 2, 3}
226-
assert {e for e in result["y"]} == {2, 3, 4}
227+
228+
assert len(result["z"]) == 3
229+
for e in result["z"]:
230+
assert e in [[9], [42], [1337]]
227231

228232

229233
def test_graph_streamRelationshipProperty_with_arrow(gds: GraphDataScience) -> None:

0 commit comments

Comments
 (0)