Period#
- class isoperiod.Period[source]#
A repeating period of time, which splits the Gregorian timeline into consecutive, numbered intervals.
A Period divides all of time into intervals of one fixed size - every hour, every 15 minutes, every month - and numbers each interval with an integer “ordinal”. Two methods map between the two views:
ordinal(datetime) -> intThe ordinal of the interval containing that datetime.
datetime(ordinal) -> datetimeThe first instant of that interval.
They are inverses, in the sense that
period.ordinal(d) == nfor every datetime whereperiod.datetime(n) <= d < period.datetime(n + 1).An offset shifts every one of those boundaries by a fixed amount, so intervals keep their size but start somewhere else - e.g. a hydrological day running 09:00 to 09:00, or a year starting in October. In string form it is written
<duration>+<offset>("P1D+T9H"), this package’s own extension of the ISO 8601 duration format. See Offsets and origins.Period is abstract: build one with an
of_*factory, then refine it with thewith_*methods, each of which returns a new Period rather than mutating the original.Instances are immutable and hashable, so they work in sets and as dict keys. They sort shortest first, with a calendar month taken as its mean Gregorian length so that calendar and fixed-length periods order against one another; comparing two Periods that differ only in their tzinfo raises
TypeError, just as comparing a naive datetime with an aware one does.Examples
>>> from datetime import datetime, timedelta >>> from isoperiod import Period
Which hour is 09:47 in, and when did that hour start and end?
>>> pt1h = Period.of_hours(1) >>> d = datetime(2024, 3, 1, 9, 47) >>> n = pt1h.ordinal(d) >>> pt1h.datetime(n) datetime.datetime(2024, 3, 1, 9, 0) >>> pt1h.datetime(n + 1) datetime.datetime(2024, 3, 1, 10, 0)
Ordinals are consecutive integers, so interval arithmetic is integer arithmetic.
>>> pt1h.ordinal(d + timedelta(days=1)) - pt1h.ordinal(d) 24
Months work the same way, despite their varying length.
>>> Period.of_months(1).floor(datetime(2024, 3, 15)) datetime.datetime(2024, 3, 1, 0, 0)
An offset moves every boundary: a day running 09:00 -> 09:00.
>>> water_day = Period.of_days(1).with_hour_offset(9) >>> water_day.floor(datetime(2024, 3, 15, 7, 30)) datetime.datetime(2024, 3, 14, 9, 0)
- classmethod __new__(*args, **kwargs)#
Constructors#
From a named unit
Return a Period of |
|
Return a Period of |
|
Return a Period of |
|
Return a Period of |
|
Return a Period of |
|
Return a Period of |
|
Return a Period of |
From a string or timedelta
Return a Period from the supplied string. |
|
Return a Period from a strict ISO 8601 duration string |
|
Return a Period from an (extended) ISO 8601 duration string |
|
Return a Period from date/duration string. |
|
Return a Period matching the duration of |
Attributes#
The standard ISO 8601 duration string of this period |
|
This period's duration, offset and origin, spelled out in English words. |
|
This period's frequency, named where there is a common word or recognised name for it. |
|
A timedelta object that matches the duration of this period, or None if no such timedelta exists |
|
Get the tzinfo property |
|
Return a string that captures the month and microsecond offsets of this period which conforms to the offset bit of the string required in a Period.of_duration string. |
|
The month offset of this period |
|
The microsecond offset of this period |
|
Get the minimum valid ordinal of this Period. |
|
Get the maximum valid ordinal |
|
A string that captures the step and multiplier of this period and which is suitable for use with Polars. |
|
A string that captures the month and microsecond offsets of this period and which is suitable for use with Polars. |
Methods#
Intervals and ordinals
Return the ordinal of the interval that contains the supplied datetime. |
|
Return the datetime at which the interval with the supplied ordinal starts. |
|
Return the first instant of the interval that contains the supplied datetime. |
|
Return the bounds of the interval containing the supplied datetime, as a (start, end) pair. |
|
Yield the start of every interval overlapping the half-open window from |
|
Return True if the supplied datetime is exactly the start of one of this period's intervals. |
Builders
Every builder returns a new Period; none mutate the original.
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a copy of this Period with every boundary shifted by |
|
Return a Period derived from this one but with the origin set to the specified datetime. |
|
Return a copy of this Period labelled with |
|
Return a Period derived from this one but with no date/time offset. |
|
Return a Period derived from this one but with no ordinal shift. |
|
Return this period stripped of any offset and origin, keeping only its step and multiplier. |
Comparison
Return how many intervals of this period fit into each interval of |
|
Return True if every interval of this period falls entirely inside a single interval of |
|
Check if this period has an offset |
|
Return True if the way that this period splits the timeline does not depend on the epoch used to perform calculations. |