Skip to content

Commit ca5e8a6

Browse files
authored
fix: handled the null values in the dataframe for existing open issue-2690 (#3244)
* handled the null values in the dataframe * format check
1 parent b1190fb commit ca5e8a6

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

awswrangler/neptune/_neptune.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ def execute_sparql(client: NeptuneClient, query: str) -> pd.DataFrame:
110110
df = None
111111
if "results" in data and "bindings" in data["results"]:
112112
df = pd.DataFrame(data["results"]["bindings"], columns=data.get("head", {}).get("vars"))
113-
df = df.applymap(lambda d: d["value"] if "value" in d else None)
113+
114+
def _binding_value(d: Any) -> Any:
115+
return d.get("value") if isinstance(d, dict) else None
116+
117+
for col in df.columns:
118+
df[col] = df[col].apply(_binding_value)
114119
else:
115120
df = pd.DataFrame(data)
116121

tests/unit/test_neptune.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,51 @@ def test_sparql_write_triples(neptune_endpoint, neptune_port) -> dict[str, Any]:
471471
assert len(batch_df.index) == len(final_df.index) + 50
472472

473473

474+
def test_sparql_bindings_nan_safe() -> None:
475+
class _DummyClient:
476+
def read_sparql(self, _query: str) -> dict[str, Any]:
477+
return {
478+
"head": {"vars": ["book_id", "title", "checked_out", "last_updated", "category_code"]},
479+
"results": {
480+
"bindings": [
481+
{
482+
"title": {
483+
"xml:lang": "en",
484+
"type": "literal",
485+
"value": "The Art of Space Travel",
486+
},
487+
"book_id": {"type": "literal", "value": "B10045982"},
488+
"last_updated": {
489+
"datatype": "http://www.w3.org/2001/XMLSchema#dateTime",
490+
"type": "literal",
491+
"value": "2025-10-01T10:30:00.000Z",
492+
},
493+
"category_code": {
494+
"datatype": "http://www.w3.org/2001/XMLSchema#integer",
495+
"type": "literal",
496+
"value": "5",
497+
},
498+
},
499+
]
500+
},
501+
}
502+
503+
client: Any = _DummyClient()
504+
df = wr.neptune.execute_sparql(
505+
client,
506+
"SELECT ?book_id ?title ?checked_out ?last_updated ?category_code WHERE { ?s ?p ?o }",
507+
)
508+
assert df.shape == (1, 5)
509+
# Row 1
510+
v1 = df.iloc[0].tolist()
511+
book_id, title, checked_out, last_updated, category_code = v1
512+
assert book_id == "B10045982"
513+
assert title == "The Art of Space Travel"
514+
assert checked_out is None # missing binding becomes None
515+
assert last_updated == "2025-10-01T10:30:00.000Z"
516+
assert category_code == "5"
517+
518+
474519
def test_sparql_write_quads(neptune_endpoint, neptune_port) -> dict[str, Any]:
475520
label = f"foo_{uuid.uuid4()}"
476521
sparkql_query = f"SELECT ?p ?o FROM <bar> WHERE {{ <{label}> ?p ?o .}}"

0 commit comments

Comments
 (0)