Skip to content

Commit b90aa44

Browse files
committed
Test and Fix Median and Mean
1 parent a5618ee commit b90aa44

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public override double Median()
228228
PrimitiveDataFrameColumn<long> sortIndices = GetAscendingSortIndices(out Int64DataFrameColumn _);
229229
long middle = sortIndices.Length / 2;
230230
double middleValue = (double)Convert.ChangeType(this[sortIndices[middle].Value].Value, typeof(double));
231-
if (Length % 2 == 0)
231+
if (sortIndices.Length % 2 == 0)
232232
{
233233
double otherMiddleValue = (double)Convert.ChangeType(this[sortIndices[middle - 1].Value].Value, typeof(double));
234234
return (middleValue + otherMiddleValue) / 2;
@@ -243,7 +243,7 @@ public override double Mean()
243243
{
244244
if (Length == 0)
245245
return 0;
246-
return (double)Convert.ChangeType((T)Sum(), typeof(double)) / Length;
246+
return (double)Convert.ChangeType((T)Sum(), typeof(double)) / (Length - NullCount);
247247
}
248248

249249
protected internal override void Resize(long length)

test/Microsoft.Data.Analysis.Tests/DataFrameTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,5 +3323,15 @@ public void GetColumnTests()
33233323
Assert.Throws<ArgumentException>(() => dataFrame.Columns.GetSingleColumn("Ushort"));
33243324

33253325
}
3326+
3327+
[Fact]
3328+
public void TestMeanMedian()
3329+
{
3330+
DataFrame df = MakeDataFrameWithNumericColumns(10, true, 0);
3331+
3332+
Assert.Equal(40.0 / 9.0, df["Decimal"].Mean());
3333+
Assert.Equal(4, df["Decimal"].Median());
3334+
3335+
}
33263336
}
33273337
}

0 commit comments

Comments
 (0)