From Python
The Python binding converts the six types to and from the standard datetime module, both as
results and as params=. Python's types count microseconds, the engine nanoseconds: a value
with digits below the microsecond becomes a pandas type, which
holds nanoseconds and subclasses the standard type, when the optional extra is installed
(pip install graphersal[pandas]); without pandas it raises ImportError naming the extra.
Nothing is ever rounded.
| Graphersal | Python | With digits below the microsecond |
|---|---|---|
datetime | aware datetime.datetime with tzinfo=datetime.timezone(timedelta(seconds=offset)) | aware pandas.Timestamp, the same tzinfo |
datetime with a zone name | aware datetime.datetime with tzinfo=zoneinfo.ZoneInfo(name) | aware pandas.Timestamp in the ZoneInfo |
localdatetime | naive datetime.datetime | naive pandas.Timestamp |
date | datetime.date | - |
time | naive datetime.time | ValueError |
offsettime | datetime.time with tzinfo=datetime.timezone(timedelta(seconds=offset)) | ValueError |
duration without months | datetime.timedelta (an Interval beyond 999999999 days) | pandas.Timedelta (an Interval beyond an int64 of nanoseconds, about 292 years) |
duration with months | graphersal.Interval | graphersal.Interval |
Why pandas: pandas.Timestamp and pandas.Timedelta are subclasses of datetime.datetime and
datetime.timedelta, so code written for the standard types keeps working; they hold
nanoseconds, fixed offsets and ZoneInfo zones, and pandas is the most widely installed library
that has them. whenever holds nanoseconds too (also a time
of day) but its types are not the standard ones, and numpy.datetime64 has neither offsets,
zones nor a time of day. Which type comes back depends on the value only, never on what is
installed. Limits:
- A time of day (
time,offsettime) with digits below the microsecond raisesValueError: no common Python type holds one (pandas has no time of day; apandas.Timedeltasince midnight would come back as a duration). Truncate it withdate_trunc(DT.microsecond)or read it withas_string(). - A
pandas.Timestampholds nanoseconds only from 1677-09-21 to 2262-04-11: adatetimeorlocaldatetimewith digits below the microsecond outside these years raises.
import datetime as dt
import graphersal
graph = graphersal.Graph()
at = dt.datetime(2023, 8, 2, 9, 30, tzinfo=dt.timezone(dt.timedelta(hours=2)))
graph.query("g.inject(x).date_add(DT.day, 1).next()", params={"x": at})
# datetime.datetime(2023, 8, 3, 9, 30, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))
graph.query('duration("P1Y2M")')
# Interval('P1Y2M')
- An aware
datetimeis passed with the UTC offset it has at that instant; with azoneinfo.ZoneInfoits zone name is kept too (a name the bundled database does not know is an error), and it comes back as aZoneInfodatetime (with the rightfoldin a repeated hour). Any othertzinfogives its offset only. An offset with a fraction of a second is refused. A Python installation without the zone (notzdataon some systems) raises when such a value is returned, rather than dropping the name. - An aware
datetime.timewith a fixed offset (datetime.timezone) is anoffsettime; one whosetzinfohas no fixed offset (aZoneInfo) is refused: a zone's offset needs a date. - A
timedeltabecomes whole days and the rest, both with the sign of the whole (timedelta(hours=-1)is-PT1H). graphersal.Intervalis immutable, equal when its three fields are equal (Interval("P1M") != Interval(0, 30), as in the engine), hasmonths,days,microsandnanos(Pythonints, exact over the whole range of a duration;microsraisesValueErrorfor a time part with digits below the microsecond) andto_iso(), and is built from the fields or from ISO 8601 text with up to nine fraction digits (Interval("P1Y2M3DT4.000000001H")).- A
pandas.Timestamporpandas.Timedeltaparameter keeps its nanoseconds; apandas.Timestampin apytzzone keeps the zone name as aZoneInfoone does.pandas.NaTis refused (passNone). - Python's
datetimeholds the years 1 to 9999; a date before year 1 cannot be returned as one (read it withas_string()).
The binding's README has the details and more examples.