Skip to content

Commit

Permalink
replace new_from_slice with new
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 5, 2021
1 parent b1bff70 commit 4fd3890
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 77 deletions.
8 changes: 4 additions & 4 deletions polars/polars-core/src/chunked_array/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ pub(crate) mod test {
use crate::prelude::*;

pub(crate) fn create_two_chunked() -> (Int32Chunked, Int32Chunked) {
let mut a1 = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
let a2 = Int32Chunked::new_from_slice("a", &[4, 5, 6]);
let a3 = Int32Chunked::new_from_slice("a", &[1, 2, 3, 4, 5, 6]);
let mut a1 = Int32Chunked::new("a", &[1, 2, 3]);
let a2 = Int32Chunked::new("a", &[4, 5, 6]);
let a3 = Int32Chunked::new("a", &[1, 2, 3, 4, 5, 6]);
a1.append(&a2);
(a1, a3)
}
Expand All @@ -466,7 +466,7 @@ pub(crate) mod test {

#[test]
fn test_power() {
let a = UInt32Chunked::new_from_slice("", &[1, 2, 3]);
let a = UInt32Chunked::new("", &[1, 2, 3]);
let b = a.pow_f64(2.);
println!("{:?}", b);
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod test {
#[cfg(feature = "dtype-categorical")]
fn test_cast_noop() {
// check if we can cast categorical twice without panic
let ca = Utf8Chunked::new_from_slice("foo", &["bar", "ham"]);
let ca = Utf8Chunked::new("foo", &["bar", "ham"]);
let out = ca.cast(&DataType::Categorical).unwrap();
let out = out.cast(&DataType::Categorical).unwrap();
assert_eq!(out.dtype(), &DataType::Categorical)
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ mod test {

#[test]
fn test_bitwise_ops() {
let a = BooleanChunked::new_from_slice("a", &[true, false, false]);
let a = BooleanChunked::new("a", &[true, false, false]);
let b = BooleanChunked::new("b", &[Some(true), Some(true), None]);
assert_eq!(Vec::from(&a | &b), &[Some(true), Some(true), None]);
assert_eq!(Vec::from(&a & &b), &[Some(true), Some(false), Some(false)]);
Expand Down
33 changes: 15 additions & 18 deletions polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ impl<T> ChunkedArray<T> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let mut array = Int32Chunked::new_from_slice("array", &[1, 2]);
/// let array_2 = Int32Chunked::new_from_slice("2nd", &[3]);
/// let mut array = Int32Chunked::new("array", &[1, 2]);
/// let array_2 = Int32Chunked::new("2nd", &[3]);
///
/// array.append(&array_2);
/// assert_eq!(Vec::from(&array), [Some(1), Some(2), Some(3)])
Expand Down Expand Up @@ -615,19 +615,19 @@ pub(crate) mod test {
use crate::prelude::*;

pub(crate) fn get_chunked_array() -> Int32Chunked {
ChunkedArray::new_from_slice("a", &[1, 2, 3])
ChunkedArray::new("a", &[1, 2, 3])
}

#[test]
fn test_sort() {
let a = Int32Chunked::new_from_slice("a", &[1, 9, 3, 2]);
let a = Int32Chunked::new("a", &[1, 9, 3, 2]);
let b = a
.sort(false)
.into_iter()
.map(|opt| opt.unwrap())
.collect::<Vec<_>>();
assert_eq!(b, [1, 2, 3, 9]);
let a = Utf8Chunked::new_from_slice("a", &["b", "a", "c"]);
let a = Utf8Chunked::new("a", &["b", "a", "c"]);
let a = a.sort(false);
let b = a.into_iter().collect::<Vec<_>>();
assert_eq!(b, [Some("a"), Some("b"), Some("c")]);
Expand Down Expand Up @@ -663,10 +663,7 @@ pub(crate) mod test {
fn filter() {
let a = get_chunked_array();
let b = a
.filter(&BooleanChunked::new_from_slice(
"filter",
&[true, false, false],
))
.filter(&BooleanChunked::new("filter", &[true, false, false]))
.unwrap();
assert_eq!(b.len(), 1);
assert_eq!(b.into_iter().next(), Some(Some(1)));
Expand Down Expand Up @@ -716,8 +713,8 @@ pub(crate) mod test {

#[test]
fn slice() {
let mut first = UInt32Chunked::new_from_slice("first", &[0, 1, 2]);
let second = UInt32Chunked::new_from_slice("second", &[3, 4, 5]);
let mut first = UInt32Chunked::new("first", &[0, 1, 2]);
let second = UInt32Chunked::new("second", &[3, 4, 5]);
first.append(&second);
assert_slice_equal(&first.slice(0, 3), &[0, 1, 2]);
assert_slice_equal(&first.slice(0, 4), &[0, 1, 2, 3]);
Expand All @@ -735,7 +732,7 @@ pub(crate) mod test {

#[test]
fn sorting() {
let s = UInt32Chunked::new_from_slice("", &[9, 2, 4]);
let s = UInt32Chunked::new("", &[9, 2, 4]);
let sorted = s.sort(false);
assert_slice_equal(&sorted, &[2, 4, 9]);
let sorted = s.sort(true);
Expand All @@ -762,16 +759,16 @@ pub(crate) mod test {

#[test]
fn reverse() {
let s = UInt32Chunked::new_from_slice("", &[1, 2, 3]);
let s = UInt32Chunked::new("", &[1, 2, 3]);
// path with continuous slice
assert_slice_equal(&s.reverse(), &[3, 2, 1]);
// path with options
let s = UInt32Chunked::new("", &[Some(1), None, Some(3)]);
assert_eq!(Vec::from(&s.reverse()), &[Some(3), None, Some(1)]);
let s = BooleanChunked::new_from_slice("", &[true, false]);
let s = BooleanChunked::new("", &[true, false]);
assert_eq!(Vec::from(&s.reverse()), &[Some(false), Some(true)]);

let s = Utf8Chunked::new_from_slice("", &["a", "b", "c"]);
let s = Utf8Chunked::new("", &["a", "b", "c"]);
assert_eq!(Vec::from(&s.reverse()), &[Some("c"), Some("b"), Some("a")]);

let s = Utf8Chunked::new("", &[Some("a"), None, Some("c")]);
Expand All @@ -780,11 +777,11 @@ pub(crate) mod test {

#[test]
fn test_null_sized_chunks() {
let mut s = Float64Chunked::new_from_slice("s", &Vec::<f64>::new());
s.append(&Float64Chunked::new_from_slice("s2", &[1., 2., 3.]));
let mut s = Float64Chunked::new("s", &Vec::<f64>::new());
s.append(&Float64Chunked::new("s2", &[1., 2., 3.]));
dbg!(&s);

let s = Float64Chunked::new_from_slice("s", &Vec::<f64>::new());
let s = Float64Chunked::new("s", &Vec::<f64>::new());
dbg!(&s.into_iter().next());
}

Expand Down
6 changes: 3 additions & 3 deletions polars/polars-core/src/chunked_array/ndarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl DataFrame {
///
/// ```rust
/// use polars_core::prelude::*;
/// let a = UInt32Chunked::new_from_slice("a", &[1, 2, 3]).into_series();
/// let b = Float64Chunked::new_from_slice("b", &[10., 8., 6.]).into_series();
/// let a = UInt32Chunked::new("a", &[1, 2, 3]).into_series();
/// let b = Float64Chunked::new("b", &[10., 8., 6.]).into_series();
///
/// let df = DataFrame::new(vec![a, b]).unwrap();
/// let ndarray = df.to_ndarray::<Float64Type>().unwrap();
Expand Down Expand Up @@ -124,7 +124,7 @@ mod test {

#[test]
fn test_ndarray_from_ca() -> Result<()> {
let ca = Float64Chunked::new_from_slice("", &[1.0, 2.0, 3.0]);
let ca = Float64Chunked::new("", &[1.0, 2.0, 3.0]);
let ndarr = ca.to_ndarray()?;
assert_eq!(ndarr, ArrayView1::from(&[1.0, 2.0, 3.0]));

Expand Down
8 changes: 4 additions & 4 deletions polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ mod test {
// validated with numpy
// Note that numpy as an argument ddof wich influences results. The default is ddof=0
// we chose ddof=1, which is standard in statistics
let ca1 = Int32Chunked::new_from_slice("", &[5, 8, 9, 5, 0]);
let ca1 = Int32Chunked::new("", &[5, 8, 9, 5, 0]);
let ca2 = Int32Chunked::new(
"",
&[
Expand All @@ -517,10 +517,10 @@ mod test {

#[test]
fn test_agg_float() {
let ca1 = Float32Chunked::new_from_slice("a", &[1.0, f32::NAN]);
let ca2 = Float32Chunked::new_from_slice("b", &[f32::NAN, 1.0]);
let ca1 = Float32Chunked::new("a", &[1.0, f32::NAN]);
let ca2 = Float32Chunked::new("b", &[f32::NAN, 1.0]);
assert_eq!(ca1.min(), ca2.min());
let ca1 = Float64Chunked::new_from_slice("a", &[1.0, f64::NAN]);
let ca1 = Float64Chunked::new("a", &[1.0, f64::NAN]);
let ca2 = Float64Chunked::new_from_slice("b", &[f64::NAN, 1.0]);
assert_eq!(ca1.min(), ca2.min());
println!("{:?}", (ca1.min(), ca2.min()))
Expand Down
8 changes: 4 additions & 4 deletions polars/polars-core/src/chunked_array/ops/is_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,17 @@ mod test {

#[test]
fn test_is_in() -> Result<()> {
let a = Int32Chunked::new_from_slice("a", &[1, 2, 3, 4]);
let b = Int64Chunked::new_from_slice("b", &[4, 5, 1]);
let a = Int32Chunked::new("a", &[1, 2, 3, 4]);
let b = Int64Chunked::new("b", &[4, 5, 1]);

let out = a.is_in(&b.into_series())?;
assert_eq!(
Vec::from(&out),
[Some(true), Some(false), Some(false), Some(true)]
);

let a = Utf8Chunked::new_from_slice("a", &["a", "b", "c", "d"]);
let b = Utf8Chunked::new_from_slice("b", &["d", "e", "c"]);
let a = Utf8Chunked::new("a", &["a", "b", "c", "d"]);
let b = Utf8Chunked::new("b", &["d", "e", "c"]);

let out = a.is_in(&b.into_series())?;
assert_eq!(
Expand Down
16 changes: 8 additions & 8 deletions polars/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub trait ChunkSet<'a, A, B> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let ca = UInt32Chunked::new_from_slice("a", &[1, 2, 3]);
/// let ca = UInt32Chunked::new("a", &[1, 2, 3]);
/// let new = ca.set_at_idx(vec![0, 1], Some(10)).unwrap();
///
/// assert_eq!(Vec::from(&new), &[Some(10), Some(10), Some(3)]);
Expand All @@ -244,7 +244,7 @@ pub trait ChunkSet<'a, A, B> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
/// let ca = Int32Chunked::new("a", &[1, 2, 3]);
/// let new = ca.set_at_idx_with(vec![0, 1], |opt_v| opt_v.map(|v| v - 5)).unwrap();
///
/// assert_eq!(Vec::from(&new), &[Some(-4), Some(-3), Some(3)]);
Expand All @@ -259,8 +259,8 @@ pub trait ChunkSet<'a, A, B> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
/// let mask = BooleanChunked::new_from_slice("mask", &[false, true, false]);
/// let ca = Int32Chunked::new("a", &[1, 2, 3]);
/// let mask = BooleanChunked::new("mask", &[false, true, false]);
/// let new = ca.set(&mask, Some(5)).unwrap();
/// assert_eq!(Vec::from(&new), &[Some(1), Some(5), Some(3)]);
/// ```
Expand All @@ -274,8 +274,8 @@ pub trait ChunkSet<'a, A, B> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let ca = UInt32Chunked::new_from_slice("a", &[1, 2, 3]);
/// let mask = BooleanChunked::new_from_slice("mask", &[false, true, false]);
/// let ca = UInt32Chunked::new("a", &[1, 2, 3]);
/// let mask = BooleanChunked::new("mask", &[false, true, false]);
/// let new = ca.set_with(&mask, |opt_v| opt_v.map(
/// |v| v * 2
/// )).unwrap();
Expand Down Expand Up @@ -590,8 +590,8 @@ pub trait ChunkFilter<T> {
///
/// ```rust
/// # use polars_core::prelude::*;
/// let array = Int32Chunked::new_from_slice("array", &[1, 2, 3]);
/// let mask = BooleanChunked::new_from_slice("mask", &[true, false, true]);
/// let array = Int32Chunked::new("array", &[1, 2, 3]);
/// let mask = BooleanChunked::new("mask", &[true, false, true]);
///
/// let filtered = array.filter(&mask).unwrap();
/// assert_eq!(Vec::from(&filtered), [Some(1), Some(3)])
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/rolling_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test {

#[test]
fn test_rolling() {
let ca = Int32Chunked::new_from_slice("foo", &[1, 2, 3, 2, 1]);
let ca = Int32Chunked::new("foo", &[1, 2, 3, 2, 1]);
let a = ca
.rolling_sum(RollingOptions {
window_size: 2,
Expand Down
18 changes: 9 additions & 9 deletions polars/polars-core/src/chunked_array/ops/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,22 @@ mod test {

#[test]
fn test_set() {
let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
let mask = BooleanChunked::new_from_slice("mask", &[false, true, false]);
let ca = Int32Chunked::new("a", &[1, 2, 3]);
let mask = BooleanChunked::new("mask", &[false, true, false]);
let ca = ca.set(&mask, Some(5)).unwrap();
assert_eq!(Vec::from(&ca), &[Some(1), Some(5), Some(3)]);

let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
let ca = Int32Chunked::new("a", &[1, 2, 3]);
let mask = BooleanChunked::new("mask", &[None, Some(true), None]);
let ca = ca.set(&mask, Some(5)).unwrap();
assert_eq!(Vec::from(&ca), &[Some(1), Some(5), Some(3)]);

let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
let ca = Int32Chunked::new("a", &[1, 2, 3]);
let mask = BooleanChunked::new("mask", &[None, None, None]);
let ca = ca.set(&mask, Some(5)).unwrap();
assert_eq!(Vec::from(&ca), &[Some(1), Some(2), Some(3)]);

let ca = Int32Chunked::new_from_slice("a", &[1, 2, 3]);
let ca = Int32Chunked::new("a", &[1, 2, 3]);
let mask = BooleanChunked::new("mask", &[Some(true), Some(false), None]);
let ca = ca.set(&mask, Some(5)).unwrap();
assert_eq!(Vec::from(&ca), &[Some(5), Some(2), Some(3)]);
Expand All @@ -314,14 +314,14 @@ mod test {
assert!(ca.set_at_idx(vec![0, 10], Some(0)).is_err());

// test booleans
let ca = BooleanChunked::new_from_slice("a", &[true, true, true]);
let mask = BooleanChunked::new_from_slice("mask", &[false, true, false]);
let ca = BooleanChunked::new("a", &[true, true, true]);
let mask = BooleanChunked::new("mask", &[false, true, false]);
let ca = ca.set(&mask, None).unwrap();
assert_eq!(Vec::from(&ca), &[Some(true), None, Some(true)]);

// test utf8
let ca = Utf8Chunked::new_from_slice("a", &["foo", "foo", "foo"]);
let mask = BooleanChunked::new_from_slice("mask", &[false, true, false]);
let ca = Utf8Chunked::new("a", &["foo", "foo", "foo"]);
let mask = BooleanChunked::new("mask", &[false, true, false]);
let ca = ca.set(&mask, Some("bar")).unwrap();
assert_eq!(Vec::from(&ca), &[Some("foo"), Some("bar"), Some("foo")]);
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod test {

#[test]
fn test_shift() {
let ca = Int32Chunked::new_from_slice("", &[1, 2, 3]);
let ca = Int32Chunked::new("", &[1, 2, 3]);

// shift by 0, 1, 2, 3, 4
let shifted = ca.shift_and_fill(0, Some(5));
Expand Down
10 changes: 5 additions & 5 deletions polars/polars-core/src/chunked_array/ops/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ mod test {
#[cfg(feature = "sort_multiple")]
#[cfg_attr(miri, ignore)]
fn test_argsort_multiple() -> Result<()> {
let a = Int32Chunked::new_from_slice("a", &[1, 2, 1, 1, 3, 4, 3, 3]);
let b = Int64Chunked::new_from_slice("b", &[0, 1, 2, 3, 4, 5, 6, 1]);
let c = Utf8Chunked::new_from_slice("c", &["a", "b", "c", "d", "e", "f", "g", "h"]);
let a = Int32Chunked::new("a", &[1, 2, 1, 1, 3, 4, 3, 3]);
let b = Int64Chunked::new("b", &[0, 1, 2, 3, 4, 5, 6, 1]);
let c = Utf8Chunked::new("c", &["a", "b", "c", "d", "e", "f", "g", "h"]);
let df = DataFrame::new(vec![a.into_series(), b.into_series(), c.into_series()])?;

let out = df.sort(&["a", "b", "c"], false)?;
Expand All @@ -808,8 +808,8 @@ mod test {
);

// now let the first sort be a string
let a = Utf8Chunked::new_from_slice("a", &["a", "b", "c", "a", "b", "c"]).into_series();
let b = Int32Chunked::new_from_slice("b", &[5, 4, 2, 3, 4, 5]).into_series();
let a = Utf8Chunked::new("a", &["a", "b", "c", "a", "b", "c"]).into_series();
let b = Int32Chunked::new("b", &[5, 4, 2, 3, 4, 5]).into_series();
let df = DataFrame::new(vec![a, b])?;

let out = df.sort(&["a", "b"], false)?;
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/doc/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//! // Parsing fmt
//! let fmt = "%Y-%m%-d %H:%M:%S";
//! // Create the ChunkedArray
//! let ca = Utf8Chunked::new_from_slice("datetime", datetime_values);
//! let ca = Utf8Chunked::new("datetime", datetime_values);
//! // Parse strings as DateTime objects
//! let date_ca = ca.as_datetime(Some(fmt));
//! ```
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ ChunkedArray: 'Date' [Int32]
]"#,
format!("{:?}", ca)
);
let ca = Utf8Chunked::new_from_slice("name", &["a", "b"]);
let ca = Utf8Chunked::new("name", &["a", "b"]);
println!("{:?}", ca);
assert_eq!(
r#"shape: (2,)
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/frame/groupby/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ mod test {
vec![1, 2, 3, 4, 4, 4, 2, 1, 1],
vec![1, 2, 3, 4, 4, 4],
] {
let ca = UInt32Chunked::new_from_slice("", slice);
let ca = UInt32Chunked::new("", slice);
let split = split_ca(&ca, 4).unwrap();

let a = groupby(ca.into_iter()).into_iter().sorted().collect_vec();
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-core/src/frame/groupby/resample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ mod test {

#[test]
fn test_downsample() -> Result<()> {
let ts = Int64Chunked::new_from_slice(
let ts = Int64Chunked::new(
"ms",
&[
946684800000,
Expand Down Expand Up @@ -487,7 +487,7 @@ mod test {
20210319 23:59:01";
let data: Vec<_> = data.split('\n').collect();

let date = Utf8Chunked::new_from_slice("date", &data);
let date = Utf8Chunked::new("date", &data);
let date = date.as_datetime(None)?.into_series();
let values =
UInt32Chunked::new_from_iter("values", (0..date.len()).map(|v| v as u32)).into_series();
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ impl DataFrame {
/// ```
/// # use polars_core::prelude::*;
/// fn example(df: &DataFrame) -> Result<DataFrame> {
/// let idx = UInt32Chunked::new_from_slice("idx", &[0, 1, 9]);
/// let idx = UInt32Chunked::new("idx", &[0, 1, 9]);
/// df.take(&idx)
/// }
/// ```
Expand Down

0 comments on commit 4fd3890

Please sign in to comment.