Skip to content

Commit

Permalink
Added to_numo method to Series
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 21, 2023
1 parent 2cea2cf commit 541e97f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1 (unreleased)

- Added `to_numo` method to `Series`

## 0.3.0 (2023-02-15)

- Updated Polars to 0.27.1
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem "rake-compiler"
gem "minitest"
gem "activerecord"
gem "sqlite3"
gem "numo-narray"

# https://github.com/lsegal/yard/issues/1321
gem "yard", require: false
31 changes: 29 additions & 2 deletions lib/polars/series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1823,8 +1823,35 @@ def is_utf8
# def view
# end

# def to_numo
# end
# Convert this Series to a Numo array. This operation clones data but is completely safe.
#
# @return [Numo::NArray]
#
# @example
# s = Polars::Series.new("a", [1, 2, 3])
# s.to_numo
# # =>
# # Numo::Int64#shape=[3]
# # [1, 2, 3]
def to_numo
if !has_validity && is_numeric
# TODO make more efficient
{
UInt8 => Numo::UInt8,
UInt16 => Numo::UInt16,
UInt32 => Numo::UInt32,
UInt64 => Numo::UInt64,
Int8 => Numo::Int8,
Int16 => Numo::Int16,
Int32 => Numo::Int32,
Int64 => Numo::Int64,
Float32 => Numo::SFloat,
Float64 => Numo::DFloat
}.fetch(dtype).cast(to_a)
else
raise Todo
end
end

# Set masked values.
#
Expand Down
15 changes: 15 additions & 0 deletions test/numo_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative "test_helper"

class NumoTest < Minitest::Test
def test_series_int
s = Polars::Series.new([1, 2, 3])
assert_kind_of Numo::Int64, s.to_numo
assert_equal s.to_a, s.to_numo.to_a
end

def test_series_float
s = Polars::Series.new([1.5, 2.5, 3.5])
assert_kind_of Numo::Float64, s.to_numo
assert_equal s.to_a, s.to_numo.to_a
end
end

0 comments on commit 541e97f

Please sign in to comment.