Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise IndexOutOfRangeException warning when accessing empty series by index #437

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/Deedle/Series.fs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ and
/// can be used when the current series is ordered.
///
/// [category:Accessors and slicing]
member series.GetItems(keys, lookup) =
member series.GetItems(keys, lookup) =
let newIndex = indexBuilder.Create<_>((keys:seq<_>), None)
let cmd = indexBuilder.Reindex(index, newIndex, lookup, Vectors.Return 0, fun addr -> series.Vector.GetValue(addr).HasValue)
let newVector = vectorBuilder.Build(newIndex.AddressingScheme, cmd, [| vector |])
Expand Down Expand Up @@ -279,7 +279,6 @@ and
let newVector = vectorBuilder.Build(newIndex.AddressingScheme, levelCmd, [| vector |])
Series(newIndex, newVector, vectorBuilder, indexBuilder)


/// Attempts to get a value at the specified 'key'
///
/// [category:Accessors and slicing]
Expand All @@ -292,7 +291,7 @@ and

///
/// [category:Accessors and slicing]
member x.GetObservation(key) =
member x.GetObservation(key) =
let addr = index.Locate(key)
if addr = Address.invalid then keyNotFound key
let value = vector.GetValue(addr)
Expand All @@ -301,7 +300,7 @@ and

///
/// [category:Accessors and slicing]
member x.TryGet(key) =
member x.TryGet(key) =
let addr = x.Index.Locate(key)
if addr = Address.invalid then OptionalValue.Missing
else x.Vector.GetValue(addr)
Expand All @@ -322,12 +321,15 @@ and
x.Vector.GetValue(x.Index.AddressAt(int64 index))

/// [category:Accessors and slicing]
member x.GetKeyAt(index : int) =
member x.GetKeyAt(index : int) =
x.Index.KeyAt(x.Index.AddressAt(int64 index))

/// [category:Accessors and slicing]
member x.GetAt(index : int) =
x.TryGetAt(index).Value
member x.GetAt(index : int) =
if x.Index.IsEmpty then
raise (new IndexOutOfRangeException("Series is empty"))
else
x.TryGetAt(index).Value

///
/// [category:Accessors and slicing]
Expand Down
5 changes: 5 additions & 0 deletions tests/Deedle.Tests/Series.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let sortedByVal = series [ 2 => "bye"; 1 => "ciao"; 3 => "hi"; 5 => "nazdar" ]

let ordered = series [ 1 => "hi"; 2 => "bye"; 3 => "ciao"; 5 => "nazdar" ]
let missing = series [ 1 => "hi"; 2 => null; 3 => "ciao"; 5 => "nazdar" ]
let empty:Series<string,int> = Series([])
let usCulture = CultureInfo.GetCultureInfo("en-us")
let parseDateUSA s = DateTime.Parse(s,usCulture)

Expand All @@ -50,6 +51,10 @@ let ``Accessing missing value or using out of range key throws`` () =
(fun () -> missing.[2] |> ignore) |> should throw typeof<MissingValueException>
(fun () -> missing.[7] |> ignore) |> should throw typeof<KeyNotFoundException>

[<Test>]
let ``Accessing empty series by index throws IndexOutOfRangeException`` () =
(fun () -> empty.GetAt(0) |> ignore) |> should throw typeof<IndexOutOfRangeException>

[<Test>]
let ``Can access elements by address`` () =
unordered.GetAt(0) |> shouldEqual "hi"
Expand Down