From 5a6b9f5762505e4a33a264ac0922c116d12392bc Mon Sep 17 00:00:00 2001 From: Peter Bower <37089506+pbower@users.noreply.github.com> Date: Sat, 1 Aug 2026 01:22:21 +0100 Subject: [PATCH] Add time unit accessor to TemporalArray --- src/enums/collections/temporal_array.rs | 18 ++++++++++++++++-- .../views/collections/temporal_array_view.rs | 19 ++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/enums/collections/temporal_array.rs b/src/enums/collections/temporal_array.rs index 973b23e..edeb241 100644 --- a/src/enums/collections/temporal_array.rs +++ b/src/enums/collections/temporal_array.rs @@ -30,7 +30,7 @@ use std::{ sync::Arc, }; -use crate::{Bitmask, DatetimeArray, MaskedArray}; +use crate::{Bitmask, DatetimeArray, MaskedArray, TimeUnit}; use crate::{ enums::{error::MinarrowError, shape_dim::ShapeDim}, traits::{concatenate::Concatenate, shape::Shape}, @@ -102,6 +102,20 @@ impl TemporalArray { } } + /// The time unit the samples are measured in i.e. seconds, milliseconds, + /// microseconds, nanoseconds, or days. + /// + /// Returns `None` for the `Null` placeholder variant, which carries no + /// datetime payload and therefore no unit. + #[inline] + pub fn time_unit(&self) -> Option { + match self { + TemporalArray::Datetime32(arr) => Some(arr.time_unit), + TemporalArray::Datetime64(arr) => Some(arr.time_unit), + TemporalArray::Null => None, + } + } + /// Removes the rows in `[start, end)`, shifting later rows left. /// A shared inner array is cloned first i.e. copy-on-write. /// @@ -325,7 +339,7 @@ impl Concatenate for TemporalArray { use crate::DatetimeOps; #[cfg(feature = "datetime_ops")] -use crate::enums::time_units::{TimePeriod, TimeUnit}; +use crate::enums::time_units::TimePeriod; #[cfg(feature = "datetime_ops")] use time::Duration; diff --git a/src/structs/views/collections/temporal_array_view.rs b/src/structs/views/collections/temporal_array_view.rs index 1a3cede..4bb9bf0 100644 --- a/src/structs/views/collections/temporal_array_view.rs +++ b/src/structs/views/collections/temporal_array_view.rs @@ -50,7 +50,7 @@ use crate::enums::shape_dim::ShapeDim; use crate::traits::concatenate::Concatenate; use crate::traits::print::MAX_PREVIEW; use crate::traits::shape::Shape; -use crate::{Array, ArrayV, BitmaskV, MaskedArray, TemporalArray}; +use crate::{Array, ArrayV, BitmaskV, MaskedArray, TemporalArray, TimeUnit}; /// # TemporalArrayView /// @@ -183,6 +183,17 @@ impl TemporalArrayV { self.len } + /// The time unit the samples are measured in i.e. seconds, milliseconds, + /// microseconds, nanoseconds, or days. + /// + /// Reads the unit off the backing datetime array, so the window's offset + /// and length do not affect it. Returns `None` for the `Null` placeholder + /// variant, which carries no datetime payload and therefore no unit. + #[inline] + pub fn time_unit(&self) -> Option { + self.array.time_unit() + } + /// Returns the full backing array wrapped as an `Array` enum, ignoring the view's offset and length. /// /// Use this to access inner array methods. The returned array is the unwindowed original. @@ -338,7 +349,6 @@ impl Display for TemporalArrayV { { use time::OffsetDateTime; - use crate::TimeUnit; let unit = match &self.array { TemporalArray::Datetime32(arr) => &arr.time_unit, TemporalArray::Datetime64(arr) => &arr.time_unit, @@ -389,8 +399,6 @@ impl Display for TemporalArrayV { #[cfg(not(feature = "datetime_ops"))] { - use crate::TimeUnit; - let unit = match &self.array { TemporalArray::Datetime32(arr) => &arr.time_unit, TemporalArray::Datetime64(arr) => &arr.time_unit, @@ -422,9 +430,6 @@ impl Display for TemporalArrayV { #[cfg(feature = "datetime_ops")] use crate::DatetimeOps; -#[cfg(feature = "datetime_ops")] -use crate::enums::time_units::TimeUnit; - #[cfg(feature = "datetime_ops")] use time::Duration;