# Median of Dataset in Mathematica 10

Bug introduced in 10.0 and fixed in 10.3

I'm having trouble calculating the median of a Dataset[] in Mathematica 10.

The situation is as follows. Consider a dataset that was defined as follows:

dataset = Dataset[{<|"a"->1,"b"->2|>,<|"a"->3,"b"->4|>}];


The mean and variance of columns a and b can now be calculated by

mean = dataset[Mean, {"a","b"}]
var = dataset[Variance, {"a","b"}]


That works pefectly, but

med = dataset[Median, {"a","b"}]


returns a Failure[]! Somehow, Median[] is not compatible with a list of associations as its argument and the other functions are.

Can someone explain why this happens and maybe help with a solution?

• I found a workaround. Entering AssociationThread[Keys[#[[1]]], Median[Values /@ #]]& instead of Median helps. But I must say that I don't like this at all, there should be a better way. – Thijs Aug 13 '14 at 12:10
• A couple of questions. What OS are you using? Also, what do you mean by "crash"? Does the kernel go down? Or, do you get a Failure[...]? On Mac OS, I get a failure. – rcollyer Aug 13 '14 at 12:11
• You're right. It returns a Failure. I updated the question. I'm on OS X as well. – Thijs Aug 13 '14 at 12:14
• Looks like a bug to me. Have you reported this to support@wolfram.com? – Sjoerd C. de Vries Aug 13 '14 at 15:39
• It's not that Median isn't compatible with associations, its that the type here is recognized as a Struct instead of an Assoc. That's not wrong, per se, but it does mean that the type inference scheme, which is a vast undertaking that can never really be finished, doesn't 'know' that this should be allowed. There is a fairly straightforward fix that may make it into 10.0.1 (and will fix many similar issues), but for now, writing Median[#]& instead of Median should get around this problem. Edit: I'm wrong, Median doesn't work properly on associations of vectors. – Taliesin Beynon Aug 13 '14 at 17:58

Working since version 10.3:

dataset = Dataset[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}];

med = dataset[Median, {"a", "b"}]


Median itself doesn't work on associations of vectors:

In[9]:= Median[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]
During evaluation of In[9]:= Median::rectn: Rectangular array of real numbers is expected at position 1 in Median[{<|a->1,b->2|>,<|a->3,b->4|>}]. >>
Out[9]= Median[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]

• This kind of makes sense because the median is defined based on ordering of the elements. Vectors cannot be ordered, thus mathematically the median of vectors makes no sense. Median does have the convenient behaviour that for a list of vectors Median[list] gives Median /@ Transpose[list] though. – Szabolcs Jul 2 '15 at 6:19
• @Szabolcs yes I agree. Reported as 297160. – Taliesin Beynon Jul 2 '15 at 7:06