17
$\begingroup$

Bug introduced in 10.0.0 and fixed in 10.1 or earlier


The Documentation Center item, Computation with Structured Datasets, implies that Append has been extended in V10 to work with datasets. But not on my computer. (I am running on OS X)

Here is what I tried:

ds = 
  Dataset[{<|"a" -> 1, "b" -> "x"|>, <|"a" -> 2, "b" -> "y"|>, 
           <|"a" -> 3, "b" -> "z"|>, <|"a" -> 4, "b" -> "x"|>}]

dataset

Append[ds, <|"a" -> 5, "b" -> y|>]

failure

However, insertion

Insert[ds, <|"a" -> 5, "b" -> y|>, -1]

works fine.

insert

I don't know whether I've found a bug in Appendin combination with a dataset or whether I am misreading the documentation. Are others experiencing what I am? Or can someone explain what I did wrong?

Update

I should have pointed out that I already knew that appending Append[ds, <|"a" -> 5, "b" -> "y"|>] worked. But why does it fail when given an unbound symbol? And why should Insert behave differently when given an unbound symbol? Is V10 attempting to enforce some kind of a type consistency on the values of associations? That would be very un-Mathematica like.

$\endgroup$
2
  • 1
    $\begingroup$ It looks buggy to me given that AppendTo[ds, <|"a" -> 5, "b" -> y|>] works fine $\endgroup$
    – mfvonh
    Jul 24, 2014 at 3:52
  • 1
    $\begingroup$ Problem remains in version 10.0.1 $\endgroup$
    – Mr.Wizard
    Sep 17, 2014 at 11:20

2 Answers 2

16
$\begingroup$

This is interesting:

If you replace the symbol y with the string "y", Append works fine.

Append[ds, <|"a" -> 5, "b" -> "y"|>]

Mathematica graphics

Also, if you start out with Symbols, then it works fine:

ds = Dataset[{<|"a" -> 1, "b" -> x|>, <|"a" -> 2, "b" -> y|>, <|
    "a" -> 3, "b" -> z|>, <|"a" -> 4, "b" -> x|>}]

Mathematica graphics

Then:

Append[ds, <|"a" -> 5, "b" -> y|>]

Mathematica graphics

Using the Undocumented second argument we can force the Dataset on creation to expect AnyType of type:

Needs["TypeSystem`"];

dst = Dataset[{<|"a" -> 1, "b" -> "x"|>, <|"a" -> 2, "b" -> "y"|>, <|
    "a" -> 3, "b" -> "z"|>, <|"a" -> 4, "b" -> "x"|>}, 
  Vector[Struct[{"a", "b"}, {Atom[Integer], AnyType}]]]

Mathematica graphics

Now Append works even if we feed it a Symbol instead of a String:

Append[dst, <|"a" -> 5, "b" -> y|>]

Mathematica graphics

$\endgroup$
6
  • 1
    $\begingroup$ I already knew that "y" would work, but your find concerning y working if the other values of field "b" were symbols is new to me. +1 $\endgroup$
    – m_goldberg
    Jul 24, 2014 at 5:37
  • $\begingroup$ Your discussion of the undocumented 2nd does indicate an intent to enforce type consistency. But the implementation seem inconsistent, since Append enforces type consistency and Insert ignores it. $\endgroup$
    – m_goldberg
    Jul 24, 2014 at 8:58
  • $\begingroup$ @m_goldberg. I agree. Dataset is clearly still buggy but seems to be a useful thing to have. $\endgroup$
    – RunnyKine
    Jul 24, 2014 at 9:02
  • 1
    $\begingroup$ @m_goldberg, I learned about it from Taliesin Beynon in this answer $\endgroup$
    – RunnyKine
    Jul 24, 2014 at 13:07
  • 5
    $\begingroup$ @m_goldberg This should 'coerce' the type of that column to become AnyType, rather than fail. I'll fix this, thanks for finding! $\endgroup$ Aug 1, 2014 at 18:53
0
$\begingroup$

very strange.

one way around is as follows:

list = {<|"a" -> 1, "b" -> "x"|>, <|"a" -> 2, "b" -> "y"|>, <|
    "a" -> 3, "b" -> "z"|>, <|"a" -> 4, "b" -> "x"|>};
Dataset[Append[list, <|"a" -> 5, "b" -> y|>]]

If it works in this way then it should work directly with Append[ds, <|"a" -> 5, "b" -> y|>]

maybe it is a bug.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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