Skip to content

Commit

Permalink
Add Series#getRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
pityka committed Jan 29, 2024
1 parent 2026e81 commit 253b95a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions saddle-core/src/main/scala/org/saddle/Series.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ class Series[X: ST: ORD, @spec(Int, Long, Double) T: ST](
if (loc == -1) NA else at(loc)
}

/** If a key exists, get the value associated with the
* first occurrence of that key.
* If the key does not exists, then throw.
* @return
*/
def getRaw(key: X): T = {
val loc = index.getFirst(key)
raw(loc)
}

/** Get the last value of the Series
*/
def last: Scalar[T] = values.last
Expand Down
13 changes: 13 additions & 0 deletions saddle-core/src/test/scala/org/saddle/SeriesCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ class SeriesCheck extends Specification with ScalaCheck {
}
}
}
"get raw (key) works" in {
implicit val ser = Arbitrary(SeriesArbitraries.dupSeriesDoubleWithNA)

forAll { (s: Series[Int, Double]) =>
(s.length > 0) ==> {
val loc = Gen.choose(0, s.length - 1)
forAll(loc) { i =>
val idx = s.index.raw(i)
s.getRaw(idx).toString must_== s.values.raw(s.index.findOne(_ == idx)).toString
}
}
}
}

"last (key) works" in {
implicit val ser = Arbitrary(SeriesArbitraries.dupSeriesDoubleWithNA)
Expand Down

0 comments on commit 253b95a

Please sign in to comment.