Skip to content

Commit

Permalink
feat(rust)!: Rename frame_equal/series_equal to equals (#12663)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Nov 25, 2023
1 parent bd1344e commit cf53713
Show file tree
Hide file tree
Showing 65 changed files with 218 additions and 218 deletions.
16 changes: 8 additions & 8 deletions crates/polars-core/src/chunked_array/comparison/mod.rs
Expand Up @@ -721,27 +721,27 @@ where
impl ChunkCompare<&ListChunked> for ListChunked {
type Item = BooleanChunked;
fn equal(&self, rhs: &ListChunked) -> BooleanChunked {
let _series_equal = |lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(l.series_equal(r)),
let _series_equals = |lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(l.equals(r)),
_ => None,
};

_list_comparison_helper(self, rhs, _series_equal)
_list_comparison_helper(self, rhs, _series_equals)
}

fn equal_missing(&self, rhs: &ListChunked) -> BooleanChunked {
let _series_equal_missing = |lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(l.series_equal_missing(r)),
let _series_equals_missing = |lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(l.equals_missing(r)),
(None, None) => Some(true),
_ => Some(false),
};

_list_comparison_helper(self, rhs, _series_equal_missing)
_list_comparison_helper(self, rhs, _series_equals_missing)
}

fn not_equal(&self, rhs: &ListChunked) -> BooleanChunked {
let _series_not_equal = |lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(!l.series_equal(r)),
(Some(l), Some(r)) => Some(!l.equals(r)),
_ => None,
};

Expand All @@ -751,7 +751,7 @@ impl ChunkCompare<&ListChunked> for ListChunked {
fn not_equal_missing(&self, rhs: &ListChunked) -> BooleanChunked {
let _series_not_equal_missing =
|lhs: Option<&Series>, rhs: Option<&Series>| match (lhs, rhs) {
(Some(l), Some(r)) => Some(!l.series_equal_missing(r)),
(Some(l), Some(r)) => Some(!l.equals_missing(r)),
(None, None) => Some(false),
_ => Some(true),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/list/iterator.rs
Expand Up @@ -355,7 +355,7 @@ mod test {
// SAFETY: unstable series never lives longer than the iterator.
unsafe {
ca.amortized_iter().zip(&ca).for_each(|(s1, s2)| {
assert!(s1.unwrap().as_ref().series_equal(&s2.unwrap()));
assert!(s1.unwrap().as_ref().equals(&s2.unwrap()));
})
};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/ops/aggregate/mod.rs
Expand Up @@ -1083,7 +1083,7 @@ mod test {
fn test_median_floats() {
let a = Series::new("a", &[1.0f64, 2.0, 3.0]);
let expected = Series::new("a", [2.0f64]);
assert!(a.median_as_series().series_equal_missing(&expected));
assert!(a.median_as_series().equals_missing(&expected));
assert_eq!(a.median(), Some(2.0f64))
}
}
6 changes: 3 additions & 3 deletions crates/polars-core/src/chunked_array/ops/sort/mod.rs
Expand Up @@ -829,7 +829,7 @@ mod test {
"a" => ["a", "a", "b", "b", "c", "c"],
"b" => [3, 5, 4, 4, 2, 5]
)?;
assert!(out.frame_equal(&expected));
assert!(out.equals(&expected));

let df = df!(
"groups" => [1, 2, 3],
Expand All @@ -841,14 +841,14 @@ mod test {
"groups" => [3, 2, 1],
"values" => ["b", "a", "a"]
)?;
assert!(out.frame_equal(&expected));
assert!(out.equals(&expected));

let out = df.sort(["values", "groups"], vec![false, true], false)?;
let expected = df!(
"groups" => [2, 1, 3],
"values" => ["a", "a", "b"]
)?;
assert!(out.frame_equal(&expected));
assert!(out.equals(&expected));

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-core/src/frame/explode.rs
Expand Up @@ -399,7 +399,7 @@ mod test {
"C" => [1, 1, 1, 1, 1, 1, 1],
]?;

assert!(out.frame_equal_missing(&expected));
assert!(out.equals_missing(&expected));

let list = Series::new("foo", [s0.clone(), s1.clear(), s1.clone()]);
let df = DataFrame::new(vec![list, s0, s1])?;
Expand All @@ -410,7 +410,7 @@ mod test {
"C" => [1, 1, 1, 1, 1, 1, 1],
]?;

assert!(out.frame_equal_missing(&expected));
assert!(out.equals_missing(&expected));
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions crates/polars-core/src/frame/mod.rs
Expand Up @@ -1285,8 +1285,8 @@ impl DataFrame {
/// "2" => &[2, 2, 2]
/// }?;
///
/// assert!(df.select(&["0", "1"])?.frame_equal(&df.select_by_range(0..=1)?));
/// assert!(df.frame_equal(&df.select_by_range(..)?));
/// assert!(df.select(&["0", "1"])?.equals(&df.select_by_range(0..=1)?));
/// assert!(df.equals(&df.select_by_range(..)?));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn select_by_range<R>(&self, range: R) -> PolarsResult<Self>
Expand Down Expand Up @@ -3564,7 +3564,7 @@ mod test {
"str" => ["a", "b", "c"]
}
.unwrap();
assert!(df.frame_equal(&valid));
assert!(df.equals(&valid));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-core/src/frame/row/transpose.rs
Expand Up @@ -270,7 +270,7 @@ mod test {
"column_2" => [3, 30],

]?;
assert!(out.frame_equal_missing(&expected));
assert!(out.equals_missing(&expected));

let df = df![
"a" => [Some(1), None, Some(3)],
Expand All @@ -283,7 +283,7 @@ mod test {
"column_2" => [Some(3), None],

]?;
assert!(out.frame_equal_missing(&expected));
assert!(out.equals_missing(&expected));

let df = df![
"a" => ["a", "b", "c"],
Expand All @@ -296,7 +296,7 @@ mod test {
"column_2" => [Some("c"), None],

]?;
assert!(out.frame_equal_missing(&expected));
assert!(out.equals_missing(&expected));
Ok(())
}
}
18 changes: 9 additions & 9 deletions crates/polars-core/src/serde/mod.rs
Expand Up @@ -15,14 +15,14 @@ mod test {
let json = serde_json::to_string(&ca).unwrap();

let out = serde_json::from_str::<Series>(&json).unwrap();
assert!(ca.into_series().series_equal_missing(&out));
assert!(ca.into_series().equals_missing(&out));

let ca = Utf8Chunked::new("foo", &[Some("foo"), None, Some("bar")]);

let json = serde_json::to_string(&ca).unwrap();

let out = serde_json::from_str::<Series>(&json).unwrap(); // uses `Deserialize<'de>`
assert!(ca.into_series().series_equal_missing(&out));
assert!(ca.into_series().equals_missing(&out));

Ok(())
}
Expand All @@ -35,7 +35,7 @@ mod test {
let json = serde_json::to_string(&ca).unwrap();

let out = serde_json::from_reader::<_, Series>(json.as_bytes()).unwrap(); // uses `DeserializeOwned`
assert!(ca.into_series().series_equal_missing(&out));
assert!(ca.into_series().equals_missing(&out));
}

fn sample_dataframe() -> DataFrame {
Expand Down Expand Up @@ -66,15 +66,15 @@ mod test {
let df = sample_dataframe();
let json = serde_json::to_string(&df).unwrap();
let out = serde_json::from_str::<DataFrame>(&json).unwrap(); // uses `Deserialize<'de>`
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}

#[test]
fn test_serde_df_bincode() {
let df = sample_dataframe();
let bytes = bincode::serialize(&df).unwrap();
let out = bincode::deserialize::<DataFrame>(&bytes).unwrap(); // uses `Deserialize<'de>`
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}

/// test using the `DeserializedOwned` trait
Expand All @@ -84,7 +84,7 @@ mod test {
let json = serde_json::to_string(&df).unwrap();

let out = serde_json::from_reader::<_, DataFrame>(json.as_bytes()).unwrap(); // uses `DeserializeOwned`
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}

#[test]
Expand All @@ -100,7 +100,7 @@ mod test {
let df = DataFrame::new(vec![s1]).unwrap();
let bytes = bincode::serialize(&df).unwrap();
let out = bincode::deserialize_from::<_, DataFrame>(bytes.as_slice()).unwrap();
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}

#[test]
Expand Down Expand Up @@ -139,14 +139,14 @@ mod test {

let df_str = serde_json::to_string(&df).unwrap();
let out = serde_json::from_str::<DataFrame>(&df_str).unwrap();
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}
/// test using the `DeserializedOwned` trait
#[test]
fn test_serde_df_owned_bincode() {
let df = sample_dataframe();
let bytes = bincode::serialize(&df).unwrap();
let out = bincode::deserialize_from::<_, DataFrame>(bytes.as_slice()).unwrap(); // uses `DeserializeOwned`
assert!(df.frame_equal_missing(&out));
assert!(df.equals_missing(&out));
}
}
2 changes: 1 addition & 1 deletion crates/polars-core/src/series/mod.rs
Expand Up @@ -135,7 +135,7 @@ pub struct Series(pub Arc<dyn SeriesTrait>);

impl PartialEq for Wrap<Series> {
fn eq(&self, other: &Self) -> bool {
self.0.series_equal_missing(other)
self.0.equals_missing(other)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/series/ops/to_list.rs
Expand Up @@ -137,7 +137,7 @@ mod test {
let expected = builder.finish();

let out = s.implode()?;
assert!(expected.into_series().series_equal(&out.into_series()));
assert!(expected.into_series().equals(&out.into_series()));

Ok(())
}
Expand Down
34 changes: 17 additions & 17 deletions crates/polars-core/src/testing.rs
Expand Up @@ -5,18 +5,18 @@ use crate::prelude::*;

impl Series {
/// Check if series are equal. Note that `None == None` evaluates to `false`
pub fn series_equal(&self, other: &Series) -> bool {
pub fn equals(&self, other: &Series) -> bool {
if self.null_count() > 0 || other.null_count() > 0 || self.dtype() != other.dtype() {
false
} else {
self.series_equal_missing(other)
self.equals_missing(other)
}
}

/// Check if all values in series are equal where `None == None` evaluates to `true`.
/// Two [`Datetime`](DataType::Datetime) series are *not* equal if their timezones are different, regardless
/// if they represent the same UTC time or not.
pub fn series_equal_missing(&self, other: &Series) -> bool {
pub fn equals_missing(&self, other: &Series) -> bool {
match (self.dtype(), other.dtype()) {
#[cfg(feature = "timezones")]
(DataType::Datetime(_, tz_lhs), DataType::Datetime(_, tz_rhs)) => {
Expand Down Expand Up @@ -58,13 +58,13 @@ impl Series {

impl PartialEq for Series {
fn eq(&self, other: &Self) -> bool {
self.series_equal_missing(other)
self.equals_missing(other)
}
}

impl DataFrame {
/// Check if [`DataFrame`]' schemas are equal.
pub fn frame_equal_schema(&self, other: &DataFrame) -> PolarsResult<()> {
pub fn schema_equal(&self, other: &DataFrame) -> PolarsResult<()> {
for (lhs, rhs) in self.iter().zip(other.iter()) {
polars_ensure!(
lhs.name() == rhs.name(),
Expand All @@ -91,15 +91,15 @@ impl DataFrame {
/// let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
///
/// assert!(!df1.frame_equal(&df2));
/// assert!(!df1.equals(&df2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn frame_equal(&self, other: &DataFrame) -> bool {
pub fn equals(&self, other: &DataFrame) -> bool {
if self.shape() != other.shape() {
return false;
}
for (left, right) in self.get_columns().iter().zip(other.get_columns()) {
if !left.series_equal(right) {
if !left.equals(right) {
return false;
}
}
Expand All @@ -117,15 +117,15 @@ impl DataFrame {
/// let df2: DataFrame = df!("Atomic number" => &[1, 51, 300],
/// "Element" => &[Some("Hydrogen"), Some("Antimony"), None])?;
///
/// assert!(df1.frame_equal_missing(&df2));
/// assert!(df1.equals_missing(&df2));
/// # Ok::<(), PolarsError>(())
/// ```
pub fn frame_equal_missing(&self, other: &DataFrame) -> bool {
pub fn equals_missing(&self, other: &DataFrame) -> bool {
if self.shape() != other.shape() {
return false;
}
for (left, right) in self.get_columns().iter().zip(other.get_columns()) {
if !left.series_equal_missing(right) {
if !left.equals_missing(right) {
return false;
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ impl PartialEq for DataFrame {
.columns
.iter()
.zip(other.columns.iter())
.all(|(s1, s2)| s1.series_equal_missing(s2))
.all(|(s1, s2)| s1.equals_missing(s2))
}
}

Expand All @@ -169,20 +169,20 @@ mod test {
use crate::prelude::*;

#[test]
fn test_series_equal() {
fn test_series_equals() {
let a = Series::new("a", &[1_u32, 2, 3]);
let b = Series::new("a", &[1_u32, 2, 3]);
assert!(a.series_equal(&b));
assert!(a.equals(&b));

let s = Series::new("foo", &[None, Some(1i64)]);
assert!(s.series_equal_missing(&s));
assert!(s.equals_missing(&s));
}

#[test]
fn test_series_dtype_noteq() {
let s_i32 = Series::new("a", &[1_i32, 2_i32]);
let s_i64 = Series::new("a", &[1_i64, 2_i64]);
assert!(!s_i32.series_equal(&s_i64));
assert!(!s_i32.equals(&s_i64));
}

#[test]
Expand All @@ -191,7 +191,7 @@ mod test {
let b = Series::new("b", [1, 2, 3].as_ref());

let df1 = DataFrame::new(vec![a, b]).unwrap();
assert!(df1.frame_equal(&df1))
assert!(df1.equals(&df1))
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-io/src/avro/mod.rs
Expand Up @@ -39,7 +39,7 @@ mod test {
buf.set_position(0);

let read_df = AvroReader::new(buf).finish()?;
assert!(write_df.frame_equal(&read_df));
assert!(write_df.equals(&read_df));
}

Ok(())
Expand Down Expand Up @@ -67,7 +67,7 @@ mod test {
.with_projection(Some(vec![0, 1]))
.finish()?;

assert!(expected_df.frame_equal(&read_df));
assert!(expected_df.equals(&read_df));

Ok(())
}
Expand All @@ -94,7 +94,7 @@ mod test {
.with_columns(Some(vec!["i64".to_string(), "utf8".to_string()]))
.finish()?;

assert!(expected_df.frame_equal(&read_df));
assert!(expected_df.equals(&read_df));

Ok(())
}
Expand Down

0 comments on commit cf53713

Please sign in to comment.