fslaborg / Deedle

Easy to use .NET library for data and time series manipulation and for scientific programming
http://fslab.org/Deedle/
BSD 2-Clause "Simplified" License
924 stars 196 forks source link

Wrong exception for empty Series #365

Closed MgSam closed 5 years ago

MgSam commented 7 years ago

When trying to retrieve an item from an empty series, Deedle throws

System.InvalidOperationException: 'OptionalValue.Value: Value is not available'

rather than an IndexOutOfBoundsException. This misleading exception caused me to spend a lot of time trying to find the problem.

Repro using NUnit:

        [TestCase(true, new object[] { "foo", null})]
        public void SeriesBrokenTest(bool shouldThrow, object[] pairs)
        {
            var dict = arrayToDictionary<string, double?>(pairs);

            var series = dict.ToSeries();
            Assert.Throws<Exception>(() =>
            {
                var newSeries = series.Where(k => k.Value != null).Sort();
                var item = newSeries.GetAt(0); //Throws wrong exception here
            });
        }

        private Dictionary<K, V> arrayToDictionary<K, V>(object[] pairs)
        {
            var dict = new Dictionary<K, V>();
            for (var i = 0; i < pairs.Length - 1; i += 2)
            {
                dict.Add((K)pairs[i], (V)pairs[i + 1]);
            }

            return dict;
        }
zyzhu commented 5 years ago

fixed in #437