pub struct Period {
pub ordinal: i64,
pub freq: PeriodFreq,
}Expand description
A single pandas-style Period value.
Stored as an integer ordinal on a frequency-specific axis plus the
frequency code. Two Periods with different freq are incompatible —
arithmetic and comparison require same-freq operands. The derived order
(ordinal, then freq) is a total one for index labels (45fzr): within one
freq it is the periods’ own.
Fields§
§ordinal: i64§freq: PeriodFreqImplementations§
Source§impl Period
impl Period
pub const fn new(ordinal: i64, freq: PeriodFreq) -> Self
Sourcepub const fn is_nat(&self) -> bool
pub const fn is_nat(&self) -> bool
Whether this is NaT: the missing period, ordinal i64::MIN (what a
missing element of a PeriodIndex holds and what renders NaT).
Sourcepub const fn ordinal(&self) -> i64
pub const fn ordinal(&self) -> i64
Integer position on this period’s frequency axis, matching
pd.Period.ordinal.
Sourcepub const fn freq(&self) -> PeriodFreq
pub const fn freq(&self) -> PeriodFreq
Frequency code for this period, matching pd.Period.freq.
Sourcepub fn cmp_same_freq(&self, other: &Self) -> Option<Ordering>
pub fn cmp_same_freq(&self, other: &Self) -> Option<Ordering>
Same-freq ordinal comparison. Returns None if freq differs —
caller decides whether that’s an error or a panic site.
Sourcepub fn shift(&self, n: i64) -> Self
pub fn shift(&self, n: i64) -> Self
Shift by n periods of the current frequency.
Matches pd.Period + n and pd.Period - n.
Sourcepub fn diff(&self, other: &Self) -> Option<i64>
pub fn diff(&self, other: &Self) -> Option<i64>
Period-difference in units of the shared frequency.
Returns None if freq differs (pandas raises IncompatibleFrequency).
Sourcepub fn from_timestamp_nanos(nanos: i64, freq: PeriodFreq) -> Option<Self>
pub fn from_timestamp_nanos(nanos: i64, freq: PeriodFreq) -> Option<Self>
Construct a Period at freq containing the wall-clock instant nanos.
Returns None if nanos is Timestamp::NAT or overflows the ordinal axis.
Sourcepub fn now(freq: &str) -> Result<Self, TypeError>
pub fn now(freq: &str) -> Result<Self, TypeError>
Return the current UTC period for freq, matching pd.Period.now(freq).
Sourcepub fn start_nanos(&self) -> Option<i64>
pub fn start_nanos(&self) -> Option<i64>
Epoch nanoseconds of this period’s inclusive start boundary (pd.Period.start_time).
Returns None for NaT or out-of-range ordinals.
Sourcepub fn end_nanos(&self) -> Option<i64>
pub fn end_nanos(&self) -> Option<i64>
Epoch nanoseconds of this period’s inclusive end boundary (pd.Period.end_time).
Returns None for NaT or out-of-range ordinals.
Sourcepub fn start_time(&self) -> Timestamp
pub fn start_time(&self) -> Timestamp
Timestamp at the period’s start boundary (pd.Period.start_time).
Returns Timestamp::nat() for NaT or out-of-range ordinals.
Sourcepub fn end_time(&self) -> Timestamp
pub fn end_time(&self) -> Timestamp
Timestamp at the period’s inclusive end boundary (pd.Period.end_time).
Returns Timestamp::nat() for NaT or out-of-range ordinals.
Sourcepub fn to_timestamp(&self, how: &str) -> Result<Timestamp, TypeError>
pub fn to_timestamp(&self, how: &str) -> Result<Timestamp, TypeError>
Convert period to a Timestamp at the requested boundary ("start"/"S" or "end"/"E"),
matching pd.Period.to_timestamp(how=...).
Sourcepub fn asfreq(&self, freq: &str) -> Result<Self, TypeError>
pub fn asfreq(&self, freq: &str) -> Result<Self, TypeError>
Convert period to a new frequency using pandas’ default "end" boundary,
matching pd.Period.asfreq(freq).
Sourcepub fn asfreq_with_how(&self, freq: &str, how: &str) -> Result<Self, TypeError>
pub fn asfreq_with_how(&self, freq: &str, how: &str) -> Result<Self, TypeError>
Convert period to a new frequency at the requested boundary ("start"/"S" or "end"/"E"),
matching pd.Period.asfreq(freq, how).
Sourcepub fn strftime(&self, fmt: &str) -> String
pub fn strftime(&self, fmt: &str) -> String
Format period using strftime directives (pd.Period.strftime).
Returns "NaT" for NaT.
Sourcepub fn year(&self) -> Option<i64>
pub fn year(&self) -> Option<i64>
Calendar year of the period (pd.Period.year). Returns None for NaT.
Sourcepub fn month(&self) -> Option<i64>
pub fn month(&self) -> Option<i64>
Calendar month (1-12) of the period (pd.Period.month). Returns None for NaT.
Sourcepub fn day(&self) -> Option<i64>
pub fn day(&self) -> Option<i64>
Calendar day of month (1-31) of the period (pd.Period.day). Returns None for NaT.
Sourcepub fn hour(&self) -> Option<i64>
pub fn hour(&self) -> Option<i64>
Hour component (0-23) of the period (pd.Period.hour). Returns None for NaT.
Sourcepub fn minute(&self) -> Option<i64>
pub fn minute(&self) -> Option<i64>
Minute component (0-59) of the period (pd.Period.minute). Returns None for NaT.
Sourcepub fn second(&self) -> Option<i64>
pub fn second(&self) -> Option<i64>
Second component (0-59) of the period (pd.Period.second). Returns None for NaT.
Sourcepub fn quarter(&self) -> Option<i64>
pub fn quarter(&self) -> Option<i64>
Quarter (1-4) of the period (pd.Period.quarter): fiscal quarter for
Quarterly frequencies, calendar quarter of the end date otherwise.
Sourcepub fn qyear(&self) -> Option<i64>
pub fn qyear(&self) -> Option<i64>
Fiscal year of the period (pd.Period.qyear). Returns None for NaT.
Sourcepub fn dayofweek(&self) -> Option<i64>
pub fn dayofweek(&self) -> Option<i64>
Day of the week (Monday=0, Sunday=6), matching pd.Period.dayofweek.
Sourcepub fn day_of_week(&self) -> Option<i64>
pub fn day_of_week(&self) -> Option<i64>
Alias for dayofweek, matching pd.Period.day_of_week.
Sourcepub fn day_of_year(&self) -> Option<i64>
pub fn day_of_year(&self) -> Option<i64>
Alias for dayofyear, matching pd.Period.day_of_year.
Sourcepub fn days_in_month(&self) -> Option<i64>
pub fn days_in_month(&self) -> Option<i64>
Number of days in the period’s month (28-31), matching pd.Period.days_in_month.
Sourcepub fn daysinmonth(&self) -> Option<i64>
pub fn daysinmonth(&self) -> Option<i64>
Alias for days_in_month, matching pd.Period.daysinmonth.
Sourcepub fn weekofyear(&self) -> Option<i64>
pub fn weekofyear(&self) -> Option<i64>
Alias for week, matching pd.Period.weekofyear.
Sourcepub fn is_leap_year(&self) -> Option<bool>
pub fn is_leap_year(&self) -> Option<bool>
Whether the period’s year is a leap year (pd.Period.is_leap_year).
Sourcepub fn parse(s: &str) -> Result<Self, TypeError>
pub fn parse(s: &str) -> Result<Self, TypeError>
Parse common pandas Period(...) strings and infer the frequency.
Supported forms mirror pandas’ unambiguous scalar constructor cases:
annual ("2024"), quarterly ("2024Q1"), monthly ("2024-01"),
and daily ("2024-01-15"). The ordinal axes match pandas:
1970, 1970Q1, 1970-01, and 1970-01-01 all have ordinal 0.
Sourcepub fn calendar_string(&self) -> String
pub fn calendar_string(&self) -> String
Pandas calendar string for this period, matching str(pd.Period).
Inverts the frequency-specific ordinal axes anchored at 1970:
1970/1970Q1/1970-01/1970-01-01/1970-01-01 00:00 all have
ordinal 0. Returns "NaT" for the missing sentinel (i64::MIN).
EVERY frequency is exact, Weekly and Business included.
⚠️ THOSE TWO ARE NOT INDEXED IN DAYS, which is what made them wrong until
08363d42f: a weekly ordinal counts WEEKS from the Monday 1969-12-22 and
renders as the RANGE it spans (2024-03-11/2024-03-17), and a business
ordinal counts BUSINESS DAYS at five per week. Both used to be handed to
civil_from_days, which reads its argument as days since the epoch, so
ordinal 2829 rendered 1977-09-30 instead of the week of 2024-03-15.
The note that used to sit here called them “best-effort” and said neither
was “reachable through the current parse/cast paths”. The first half was
a real limitation; the second was too narrow a survey. Self::parse
indeed cannot produce them and neither can cast_scalar — but
DatetimeIndex::to_period("W") builds them directly, and fp-index then
renders those values through Display in PeriodIndex::format,
to_index, to_flat_index, the .str accessor and the get_loc error
message. The frequencies were unreachable only from the two paths the
note happened to check.