Skip to main content

Period

Struct Period 

Source
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: PeriodFreq

Implementations§

Source§

impl Period

Source

pub const fn new(ordinal: i64, freq: PeriodFreq) -> Self

Source

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).

Source

pub const fn ordinal(&self) -> i64

Integer position on this period’s frequency axis, matching pd.Period.ordinal.

Source

pub const fn freq(&self) -> PeriodFreq

Frequency code for this period, matching pd.Period.freq.

Source

pub fn freqstr(&self) -> String

Canonical pandas frequency alias, matching pd.Period.freqstr.

Source

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.

Source

pub fn shift(&self, n: i64) -> Self

Shift by n periods of the current frequency. Matches pd.Period + n and pd.Period - n.

Source

pub fn diff(&self, other: &Self) -> Option<i64>

Period-difference in units of the shared frequency. Returns None if freq differs (pandas raises IncompatibleFrequency).

Source

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.

Source

pub fn now(freq: &str) -> Result<Self, TypeError>

Return the current UTC period for freq, matching pd.Period.now(freq).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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=...).

Source

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).

Source

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).

Source

pub fn strftime(&self, fmt: &str) -> String

Format period using strftime directives (pd.Period.strftime). Returns "NaT" for NaT.

Source

pub fn year(&self) -> Option<i64>

Calendar year of the period (pd.Period.year). Returns None for NaT.

Source

pub fn month(&self) -> Option<i64>

Calendar month (1-12) of the period (pd.Period.month). Returns None for NaT.

Source

pub fn day(&self) -> Option<i64>

Calendar day of month (1-31) of the period (pd.Period.day). Returns None for NaT.

Source

pub fn hour(&self) -> Option<i64>

Hour component (0-23) of the period (pd.Period.hour). Returns None for NaT.

Source

pub fn minute(&self) -> Option<i64>

Minute component (0-59) of the period (pd.Period.minute). Returns None for NaT.

Source

pub fn second(&self) -> Option<i64>

Second component (0-59) of the period (pd.Period.second). Returns None for NaT.

Source

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.

Source

pub fn qyear(&self) -> Option<i64>

Fiscal year of the period (pd.Period.qyear). Returns None for NaT.

Source

pub fn dayofweek(&self) -> Option<i64>

Day of the week (Monday=0, Sunday=6), matching pd.Period.dayofweek.

Source

pub fn day_of_week(&self) -> Option<i64>

Alias for dayofweek, matching pd.Period.day_of_week.

Source

pub fn weekday(&self) -> Option<i64>

Alias for dayofweek, matching pd.Period.weekday.

Source

pub fn dayofyear(&self) -> Option<i64>

Day of the year (1-366), matching pd.Period.dayofyear.

Source

pub fn day_of_year(&self) -> Option<i64>

Alias for dayofyear, matching pd.Period.day_of_year.

Source

pub fn days_in_month(&self) -> Option<i64>

Number of days in the period’s month (28-31), matching pd.Period.days_in_month.

Source

pub fn daysinmonth(&self) -> Option<i64>

Alias for days_in_month, matching pd.Period.daysinmonth.

Source

pub fn week(&self) -> Option<i64>

ISO-8601 week number (1-53), matching pd.Period.week.

Source

pub fn weekofyear(&self) -> Option<i64>

Alias for week, matching pd.Period.weekofyear.

Source

pub fn is_leap_year(&self) -> Option<bool>

Whether the period’s year is a leap year (pd.Period.is_leap_year).

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Period

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Period

Source§

impl Debug for Period

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Period

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Period

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Pandas str(Period) form: the calendar string (2024, 2024Q1, 2024-03, 2024-01-15, …). NaT (ordinal i64::MIN) renders NaT.

Source§

impl Eq for Period

Source§

impl Hash for Period

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Period

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Period

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Period

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Period

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Period

Auto Trait Implementations§

§

impl Forget for Period

§

impl Freeze for Period

§

impl Move for Period

§

impl RefUnwindSafe for Period

§

impl Send for Period

§

impl Sync for Period

§

impl Unpin for Period

§

impl UnsafeUnpin for Period

§

impl UnwindSafe for Period

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.