0

I created an ontology using protegee and now want to insert data using RDFLIB in python. Because I have to write the update sparql statements in string and my data comes in various types including, float64, integer, strings, and Datetime, I had to do some parsing and so not all are working. Below is a snippet of my code:

df=df.tail(2000)
for ind in df.index:
pData=df['Product'][ind]
lData=df['Lifecycle left in minutes'][ind]
XLPerc=df['Percent of Lifecycle left'][ind]


q = """
INSERT DATA
{ 
myontology:XP myontology:LifecycleData XL.
myontology:XP myontology:UseCycleData XU.
#myontology:XP myontology:LifecyclePer XLPerc.
myontology:XP myontology:Temperature XTemperature.
#myontology:XP myontology:LifecyclePer XLPerc

}

""".replace('XU', str(uData)).replace('XL', str(lData)).replace('XP', str(pData))
g.update(
q, 
initNs={
    "myontology":Namespace("https://js......../myontology.owl#")
  }
)

So I am looping over my Dataframe (df) and inserting it into the ontology. Some are working and some are not working despite using the same method. I am getting ParseException error as follows:

ParseException: Expected end of text, found 'I' (at char 5), (line:2, col:5) There is a long error code but this is the last line. I can provide more information if needed. I do not know what the issue is, can somebody help me? Thank you.

5
  • what are the values of the columns? Only numbers? Or also strings? string values for examples have to be quoted in the update statement as all RDF string literals have to be Commented Apr 1, 2022 at 16:32
  • @UninformedUser Thank you for your comment. The values from the columns are in different formats and how I access them also affects their type. SN int64 Day object Temperature float64 Product object Start-time datetime64[ns] End-time datetime64[ns] Usecycle left int64 Manufacturing date datetime64[ns] Expiry Date datetime64[ns] Lifecycle left timedelta64[ns] Lifecycle in minutes float64 Lifecycle left in minutes float64 float64 Percent of Usecycle left float64 dtype: object Commented Apr 1, 2022 at 19:01
  • The previous comment contains the various columns and their data types. if I do df['Manufacturing date'].values[1], the result is numpy.datetime64('2021-07-12T19:33:28.833921000'). But if I do df['Manufacturing date'].values, I get a list of datetime64[ns] as '2021-06-28T11:22:45.004533000', '2021-05-28T11:22:45.004533000', Commented Apr 1, 2022 at 19:11
  • not sure if I can follow you - are there rows that are now working or are there other columns that aren't working? Note, in SPARQL literals beyond numbers usually have to be "quoted", For example while myontology:XP myontology:LifecycleData 123 . works myontology:XP myontology:LifecycleData ABC . does not - strings or dates have to be put into quotes, and for dates for example you also have to append a datatype URI to the literal, e.g. myontology:XP ex:someDateProperty "2021-07-12T19:33:28.833921000"^^xsd:dateTime . Commented Apr 2, 2022 at 10:15
  • @UninformedUser Thank you for the contributions. I took the statements and ontology to GraphDB and ran the SPARQL queries using direct values such as 123, "Product2", etc. I later came back to python and realized that the replace functions were not evaluating correctly. Many thanks. Commented Apr 6, 2022 at 12:59

1 Answer 1

0

I have been able to rectify the problem myself. The replace() functions were not evaluating correctly due to too similar variables. For instance, myontology:XP myontology:LifecyclePer XLPerc and myontology:XP myontology:LifecycleData XL. both had XL as in XLPerc and XL itself. So, while evaluating, the XL in XLPerc was replaced with another value such as 68.23433Perc and not the expected value68.23433, and many other similar errors like this.

I solved this by defining my variables as unique as possible and now it is evaluating just fine. Thank you everyone for your help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.