Matt Camp
Matt Camp

Reputation: 1528

Py2Neo not creating Datetime data type correctly?

I trying build out some functions to populate my new Neo4j graph and I am having a hard time getting dates to populate as the correct data type in Neo4j using Py2Neo v4 and Neo4j 3.4.7. According to the Neo4j docummention there are Datetime data types... and spatial points which I want to get too as well

I can not for the life of me find any documentation in Py2Neo for using either spatial points or temporal points. I found that there was back in v2 of Py2Neo a plugin for these data types but haven't found anything else on it.

I can send a Python datetime.datetime object to Neo4j as a node property but when I try to query with Cypher it doesn't acknowledge it as being in the correct format.

# python/py2neo code being used to make the node
example_node = Node("Example", date=datetime.datetime.now())
tx.create(example_node)
# cypher query
MATCH (e:Example)
WHERE e.date > datetime("2018-12-31")
RETURN e

Note: if I cast e.date to datetime like this datetime(e.date) I get the syntax error:

Neo.ClientError.Statement.SyntaxError: Text cannot be parsed to a DateTime
"2019-01-14 13:00:52"

Any help in finding proper documentation in Py2neo, or maybe even a better driver to use would be appreciated.

Thank you

Upvotes: 5

Views: 1635

Answers (2)

Tim B
Tim B

Reputation: 68

With the current version of py2neo neotime does not work anymore Just use datetime and for timezone aware dates incl pytz

from py2neo import Graph, Node
import time
import datetime
import pytz

g = Graph()
g.run("MATCH(n:DateNode) delete n")
n = Node(
    "DateNode",
    id=12345,
    time_time=time.time(),
    date='date("2019-06-04")',
    datetime_datetime=datetime.datetime(
        year=2019, month=12, day=23, hour=11, minute=49, second=30
    ),
    datetime_date=datetime.date.fromisoformat("2019-12-04"),
    datetime_fromtimestamp=datetime.date.fromtimestamp(time.time()),
    datetime_datetime_now=datetime.datetime.now(),
    datetime_datetime_strptime=datetime.datetime.strptime(
        "December 25, 2010", "%B %d, %Y"
    ),
    datetime_datetime_UTC=datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC),
)
g.create(n)

print(
    g.run(
        "MATCH (n:DateNode{id:12345}) WITH n LIMIT 1 UNWIND keys(n) AS key RETURN key, apoc.meta.type(n[key]) as val"
    ).data()
)

Results in

[
   {
      "key":"datetime_datetime_UTC",
      "val":"ZonedDateTime"
   },
   {
      "key":"datetime_datetime",
      "val":"LocalDateTime"
   },
   {
      "key":"datetime_datetime_now",
      "val":"LocalDateTime"
   },
   {
      "key":"datetime_datetime_strptime",
      "val":"LocalDateTime"
   },
   {
      "key":"date",
      "val":"STRING"
   },
   {
      "key":"id",
      "val":"INTEGER"
   },
   {
      "key":"time_time",
      "val":"FLOAT"
   },
   {
      "key":"datetime_fromtimestamp",
      "val":"LocalDate"
   },
   {
      "key":"datetime_date",
      "val":"LocalDate"
   }
]

Upvotes: 1

BigRig
BigRig

Reputation: 104

Turns out that py2neo uses the neotime module under the hood for returning/creating Neo4j date/time types. (Link to documentation here) I discovered this by converting an existing string field to a date type using Cypher and seeing what py2neo returned when I queried the graph.

from py2neo import Graph, Node
import neotime
import uuid

# Create graph object
graph = Graph()

# Create example node for a blog post
post = Node(
    'Post', 
    id=str(uuid.uuid4()), 
    title='Neo4j Date Post', 
    text='Here is some text',
    # Use neotime to create Neo4j date/time fields
    timestamp=neotime.DateTime.now(), 
    date=neotime.Date(2020, 5, 23)
)
graph.create(post)

Queries on your graph will return neotime.Date or neotime.DateTime objects, which thankfully have a to_native() method you can call, which converts them to datetime objects

import neotime

print(neotime.Date(2020, 5, 24).to_native())
# datetime.date(2020, 5, 24)

print(neotime.DateTime.now().to_native())
# datetime.datetime(2020, 5, 24, 11, 43, 30, 373512)

Upvotes: 6

Related Questions