-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rdflib utility: sparql_results_to_df
refs RDFLib/rdflib#1179
- Loading branch information
Showing
5 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pytest | ||
import rdflib | ||
|
||
from nxontology_data.utils import sparql_results_to_df | ||
|
||
|
||
@pytest.fixture | ||
def rdflib_foaf_graph() -> rdflib.Graph: | ||
""" | ||
FOAF (Friend of a Friend) testing graph from rdflib. | ||
""" | ||
graph = rdflib.Graph() | ||
return graph.parse( | ||
source="https://github.com/RDFLib/rdflib/raw/56dc4207ce6e7b11ed7b45fb4fd4020ba548e718/examples/foaf.n3", | ||
format="n3", | ||
) | ||
|
||
|
||
_foaf_sparql = """\ | ||
SELECT | ||
?subject | ||
?subject_is_tim | ||
(COUNT(*) AS ?n_triples) | ||
(MIN(?predicate) AS ?sample_predicate) | ||
(SAMPLE(?missing) AS ?missing) | ||
WHERE { | ||
?subject ?predicate ?object. | ||
BIND(?subject = <http://www.w3.org/People/Berners-Lee/card#i> AS ?subject_is_tim) | ||
OPTIONAL {?subject <this_predicate_does_not_exist> ?missing .} | ||
} | ||
GROUP BY ?subject ?subject_is_tim | ||
ORDER BY DESC(?n_triples) ?subject | ||
LIMIT 10 | ||
""" | ||
|
||
|
||
@pytest.mark.slow | ||
def test_sparql_results_to_df(rdflib_foaf_graph: rdflib.Graph) -> None: | ||
results = rdflib_foaf_graph.query(_foaf_sparql) | ||
df = sparql_results_to_df(results) | ||
assert len(df) == 10 | ||
# test column values (no ? prefix), type (as strings), and order | ||
assert list(df.columns) == [ | ||
"subject", | ||
"subject_is_tim", | ||
"n_triples", | ||
"sample_predicate", | ||
"missing", | ||
] | ||
first_row = next(df.itertuples()) | ||
# test value of subject, ensuring type conversion to str | ||
assert first_row.subject == "http://www.w3.org/People/Berners-Lee/card#i" | ||
# test value of subject_is_tim, ensuring type conversion to bool | ||
assert first_row.subject_is_tim is True | ||
# test value of n_triples, ensuring type conversion to int | ||
assert first_row.n_triples == 61 | ||
# test value of sample_predicate, ensuring type conversion to str | ||
assert ( | ||
first_row.sample_predicate == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" | ||
) | ||
# test value of missing, ensuring it's None | ||
assert first_row.missing is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pandas as pd | ||
from rdflib.plugins.sparql.processor import SPARQLResult | ||
|
||
|
||
def sparql_results_to_df(results: SPARQLResult) -> pd.DataFrame: | ||
""" | ||
Export results from an rdflib SPARQL query into a `pandas.DataFrame`, | ||
using Python types. See https://github.com/RDFLib/rdflib/issues/1179 | ||
and https://github.com/RDFLib/sparqlwrapper/issues/205. | ||
""" | ||
return pd.DataFrame( | ||
data=([None if x is None else x.toPython() for x in row] for row in results), | ||
columns=[str(x) for x in results.vars], | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters