This is relates to a bug that was introduced in 10.0.0 and fixed in 10.0.1

It also relates to an issue that has yet to be corrected.

After applying GroupBy to a key all of whose "values" are String, is it possile to force the internal type representation to subsequently lookup by "value" rather than Key@"value"?

GroupBy modifies internal type representation:

titanic = ExampleData[{"Dataset", "Titanic"}];

(* ... Vector[Struct[List["class", "age", "sex", "survived"], 
  List[Atom[Enumeration["1st", "2nd", "3rd"]], Atom[Integer], 
   Atom[Enumeration["female", "male"]], Atom[Boolean]]], AnyLength] ... *)

(so does Normal'izing following by casting back to Dataset though AnyLength is replaced by a numerical value, eg 1309.)

Even though the keys (formerly values) are String:

titanic[GroupBy[Key@"sex"]][Keys, # -> Head[#] &]

(* {"female" -> String, "male" -> String} *)

This lookup fails:


(* ... Part female is not applicable to <|(Alternatives[Enumeration[female, \
male]] -> _List)..|> ... *)

Note: lookup query Key@"female" works. Strangely, works also without Key when KeyDrop is applied before GroupBy:

titanic[All, KeyDrop["survived"]][GroupBy[Key@"sex"]]["female"]

(* ... {<|"class" -> "1st", "age" -> 29, "sex" -> "female"|>, ... *)

Conversely, if Span substitutes KeyDrop, the data are the same

titanic[All, 1 ;; 3][GroupBy[Key@"sex"]][Keys, # -> Head[#] &]

(* {"female" -> String, "male" -> String} *)

but lookup also fails even using Key:

titanic[All, 1 ;; 3][GroupBy[Key@"sex"]][Key@"female"]

(* ... Key Key["female"] is not one of {}. ... *)

I'm not sure how to interpret that message: b/c the internal the representation is AnyType:

 Vector[Assoc[Atom[String], Atom[AnyType], 3], AnyLength], AnyLength]

How to handle these various cases uniformly?


The main issue here is already fixed in 10.0.1 (probably as a side-effect of fixes for some of the other typesystem bugs that have already been reported by you and others!).

The span issue is separate, I've reported that (thanks!)

| improve this answer | |
  • 1
    $\begingroup$ Should we tag this question as a bug in that case? $\endgroup$ – Mr.Wizard Aug 26 '14 at 8:37
  • $\begingroup$ @Mr.Wizard sure. How does one do that? $\endgroup$ – Taliesin Beynon Aug 26 '14 at 20:08
  • $\begingroup$ If you "hover" the mouse pointer next to the existing tags, which presently are dataset and query you should see "edit tags" appear. If you click that you can edit the tags. I shall add bugs now. Please add this tag to any question/report about a confirmed bug that you see. $\endgroup$ – Mr.Wizard Aug 27 '14 at 0:17

Until this bug is fixed, a work-around is to avoid the operator form of GroupBy, e.g.:

titanic[GroupBy[#, Key@"sex"]& /* "female"]

dataset screenshot

This same bug affects many operator forms, but this work-around frequently dodges it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.