Skip to content

Commit

Permalink
Added to_numo method to DataFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 21, 2023
1 parent be5e32b commit b1ab5e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.3.1 (unreleased)

- Added `to_numo` method to `Series`
- Added `to_numo` method to `Series` and `DataFrame`

## 0.3.0 (2023-02-15)

Expand Down
20 changes: 20 additions & 0 deletions ext/polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use polars::io::mmap::ReaderBytes;
use polars::io::RowCount;
use polars::prelude::pivot::{pivot, pivot_stable};
use polars::prelude::*;
use polars_core::utils::try_get_supertype;
use std::cell::RefCell;
use std::io::{BufWriter, Cursor};
use std::ops::Deref;
Expand Down Expand Up @@ -493,6 +494,25 @@ impl RbDataFrame {
.into()
}

pub fn to_numo(&self) -> Option<Value> {
let mut st = None;
for s in self.df.borrow().iter() {
let dt_i = s.dtype();
match st {
None => st = Some(dt_i.clone()),
Some(ref mut st) => {
*st = try_get_supertype(st, dt_i).ok()?;
}
}
}
let st = st?;

match st {
// TODO
_ => None,
}
}

pub fn write_parquet(
&self,
rb_f: Value,
Expand Down
1 change: 1 addition & 0 deletions ext/polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn init() -> RbResult<()> {
class.define_method("write_ipc", method!(RbDataFrame::write_ipc, 2))?;
class.define_method("row_tuple", method!(RbDataFrame::row_tuple, 1))?;
class.define_method("row_tuples", method!(RbDataFrame::row_tuples, 0))?;
class.define_method("to_numo", method!(RbDataFrame::to_numo, 0))?;
class.define_method("write_parquet", method!(RbDataFrame::write_parquet, 5))?;
class.define_method("add", method!(RbDataFrame::add, 1))?;
class.define_method("sub", method!(RbDataFrame::sub, 1))?;
Expand Down
22 changes: 20 additions & 2 deletions lib/polars/data_frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,26 @@ def to_hashes
end
end

# def to_numo
# end
# Convert DataFrame to a 2D Numo array.
#
# This operation clones data.
#
# @return [Numo::NArray]
#
# @example
# df = Polars::DataFrame.new(
# {"foo" => [1, 2, 3], "bar" => [6, 7, 8], "ham" => ["a", "b", "c"]}
# )
# df.to_numo.class
# # => Numo::RObject
def to_numo
out = _df.to_numo
if out.nil?
Numo::NArray.vstack(width.times.map { |i| to_series(i).to_numo }).transpose
else
out
end
end

# no to_pandas

Expand Down
6 changes: 6 additions & 0 deletions test/numo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ def test_series_str
assert_kind_of Numo::RObject, s.to_numo
assert_equal s.to_a, s.to_numo.to_a
end

def test_data_frame
df = Polars::DataFrame.new({"a" => [1, 2, 3], "b" => ["one", "two", "three"]})
assert_kind_of Numo::RObject, df.to_numo
assert_equal [[1, "one"], [2, "two"], [3, "three"]], df.to_numo.to_a
end
end

0 comments on commit b1ab5e4

Please sign in to comment.