-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
133 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::prelude::*; | ||
use ndarray::prelude::*; | ||
|
||
impl<T> ChunkedArray<T> | ||
where | ||
T: PolarsNumericType, | ||
{ | ||
/// If data is aligned in a single chunk and has no Null values a zero copy view is returned | ||
/// as an `ndarray` | ||
pub fn to_ndarray(&self) -> Result<ArrayView1<T::Native>> { | ||
let slice = self.cont_slice()?; | ||
Ok(aview1(slice)) | ||
} | ||
} | ||
|
||
impl DataFrame { | ||
/// Create a 2D `ndarray::Array` from this `DataFrame`. This requires all columns in the | ||
/// `DataFrame` to be non-null and numeric. They will be casted to the same data type | ||
/// (if they aren't already). | ||
/// | ||
/// ```rust | ||
/// use polars::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 df = DataFrame::new(vec![a, b]).unwrap(); | ||
/// let ndarray = df.to_ndarray::<Float64Type>().unwrap(); | ||
/// println!("{:?}", ndarray); | ||
/// ``` | ||
/// Outputs: | ||
/// ```text | ||
/// [[1.0, 10.0], | ||
/// [2.0, 8.0], | ||
/// [3.0, 6.0]], shape=[3, 2], strides=[2, 1], layout=C (0x1), const ndim=2/ | ||
/// ``` | ||
pub fn to_ndarray<N>(&self) -> Result<Array2<N::Native>> | ||
where | ||
N: PolarsNumericType, | ||
N::Native: num::Zero + Copy, | ||
{ | ||
let mut ndarr = Array2::zeros(self.shape()); | ||
for (col_idx, series) in self.get_columns().iter().enumerate() { | ||
if series.null_count() != 0 { | ||
return Err(PolarsError::HasNullValues); | ||
} | ||
// this is an Arc clone if already of type N | ||
let series = series.cast::<N>()?; | ||
let ca = series.unpack::<N>()?; | ||
|
||
ca.into_no_null_iter() | ||
.enumerate() | ||
.for_each(|(row_idx, val)| { | ||
*&mut ndarr[[row_idx, col_idx]] = val; | ||
}) | ||
} | ||
Ok(ndarr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters