Indexing a DataSeries - Maple Help

Indexing a DataSeries

 Calling Sequence ds[idx] ds[idx] := value

Parameters

 ds - a DataSeries object idx - a label, numeric index, range, list, rtable, or Boolean DataSeries value - value to assign to the entries of the DataSeries specified by idx

Description

 • In order to select, rearrange, or reassign entries from a DataSeries object, you can use indexing.

Selecting and Rearranging Entries

 • If you use indexing anywhere other than the left-hand side of an assignment statement, you will obtain a subset or a rearrangement of the data in a DataSeries object. This will never change the DataSeries object itself.
 • The explanations below will refer to the following DataSeries object:
 > ds := DataSeries([101, 102, 103, 104, 105], 'labels' = ['a', 'b', 'c', 2, 12]);
 ${\mathrm{ds}}{≔}\left[\begin{array}{cc}{a}& {101}\\ {b}& {102}\\ {c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (1)

Selecting a Single Value

 • In order to select a single value from a DataSeries object, you can index the DataSeries with the corresponding label or a nonzero integer.
 • Indexing with a positive integer i that is at most the number of entries of the DataSeries will return the ith entry of the DataSeries.
 > ds[2];
 ${102}$ (2)
 • Indexing with a negative integer -i that is at least minus the number of entries of the DataSeries will return the ith entry from the end.
 > ds[-2];
 ${104}$ (3)
 • Indexing with a label that is not one of the forms above will yield the value corresponding to that label.
 > ds[c];
 ${103}$ (4)
 > ds[12];
 ${105}$ (5)

Selecting a Range

 • In order to select a contiguous range of values from a DataSeries object, you can index the DataSeries with a range.  For example, ds[a .. b].
 • If the left-hand side of the range is missing, it will be taken to be the start of the DataSeries. If the right-hand side is missing, it will be taken to be the end of the DataSeries. In particular, if both are missing, such as ds[ .. ], you obtain a copy of the full DataSeries.
 > ds[ .. ];
 $\left[\begin{array}{cc}{a}& {101}\\ {b}& {102}\\ {c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (6)
 • Otherwise, the left and right-hand side are interpreted as single entries (shown above).
 > ds[a .. 2];
 $\left[\begin{array}{cc}{a}& {101}\\ {b}& {102}\end{array}\right]$ (7)
 > ds[c ..];
 $\left[\begin{array}{cc}{c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (8)

Selecting with a Boolean DataSeries

 • In order to select some of the entries of a DataSeries ds, you can use a Boolean DataSeries dsb. A Boolean DataSeries is a DataSeries object where all entries are true, false, or FAIL. (It is a little more efficient if the selected datatype for dsb is either truefalse or truefalseFAIL, but it is not necessary.) This is done by matching the labels of dsb to those of ds, and returning a new DataSeries ds_result consisting of those labels of ds that match a label in dsb, where the corresponding value in dsb is true; the values of ds_result are the corresponding values of ds. All labels are in the order they were in ds.
 > dsb := DataSeries([true, false, true, FAIL, true, true], 'labels' = ['a', 'b', 2, 12, 'coconut', 'c']);
 ${\mathrm{dsb}}{≔}\left[\begin{array}{cc}{a}& {\mathrm{true}}\\ {b}& {\mathrm{false}}\\ {2}& {\mathrm{true}}\\ {12}& {\mathrm{FAIL}}\\ {\mathrm{coconut}}& {\mathrm{true}}\\ {c}& {\mathrm{true}}\end{array}\right]$ (9)
 > ds[dsb];
 $\left[\begin{array}{cc}{a}& {101}\\ {c}& {103}\\ {2}& {104}\end{array}\right]$ (10)
 The values for the labels a, 2, and c in dsb are true, so these values are returned. The value for the label coconut is also true, but it doesn't occur in ds, so it is discarded. Note also that, while the label 2 precedes c in dsb, it is the other way around in ds, and that is reflected in the result.
 Using Boolean DataSeries is particularly useful because you can create them by performing comparison operations on DataSeries. You can also combine Boolean DataSeries by using logical operators.
 > dsb2 := ds >~ 103;
 ${\mathrm{dsb2}}{≔}\left[\begin{array}{cc}{a}& {\mathrm{false}}\\ {b}& {\mathrm{false}}\\ {c}& {\mathrm{false}}\\ {2}& {\mathrm{true}}\\ {12}& {\mathrm{true}}\end{array}\right]$ (11)
 > dsb3 := isprime~(ds);
 ${\mathrm{dsb3}}{≔}\left[\begin{array}{cc}{a}& {\mathrm{true}}\\ {b}& {\mathrm{false}}\\ {c}& {\mathrm{true}}\\ {2}& {\mathrm{false}}\\ {12}& {\mathrm{false}}\end{array}\right]$ (12)
 > dsb4 := dsb2 xor dsb3;
 ${\mathrm{dsb4}}{≔}\left[\begin{array}{cc}{a}& {\mathrm{true}}\\ {b}& {\mathrm{false}}\\ {c}& {\mathrm{true}}\\ {2}& {\mathrm{true}}\\ {12}& {\mathrm{true}}\end{array}\right]$ (13)
 > ds[dsb4];
 $\left[\begin{array}{cc}{a}& {101}\\ {c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (14)
 You can put the previous few computations on a single line, too:
 > ds[ds >~ 103 xor isprime~(ds)];
 $\left[\begin{array}{cc}{a}& {101}\\ {c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (15)

Selecting Arbitrary Entries

 • In order to create a DataSeries with arbitrarily selected and reordered entries, you can index it with a list or Vector (or another 1-dimensional rtable, such as an Array). Each entry of such a list or Vector is interpreted according to rules similar to what is listed above for a single index:
 • If all entries of idx are integers that are in absolute value at most equal to the number of entries of ds, then each entry represents a position. The values and labels corresponding to these positions are gathered up into a new DataSeries. Zero entries are replaced with the value undefined. (These are considered missing values.)
 > ds[[1, 2, 4]];
 $\left[\begin{array}{cc}{a}& {101}\\ {b}& {102}\\ {2}& {104}\end{array}\right]$ (16)
 > ds[<-3, -2, 2>];
 $\left[\begin{array}{cc}{c}& {103}\\ {2}& {104}\\ {b}& {102}\end{array}\right]$ (17)
 • Otherwise, all entries of idx are considered to be labels. Any entry that is not found is replaced with the value undefined. Note that a single value that is not an integer, or is not in the appropriate range, changes the interpretation to this mechanism.
 > ds[[2, 3, a]];
 $\left[\begin{array}{cc}{2}& {104}\\ {0}& {\mathrm{undefined}}\\ {a}& {101}\end{array}\right]$ (18)
 > ds[[2, 3]];
 $\left[\begin{array}{cc}{b}& {102}\\ {c}& {103}\end{array}\right]$ (19)

Reassigning Entries

 • If you use an indexed DataSeries as the left-hand side of an assignment, as in ds[idx] := value, you can modify the entries in the DataSeries.
 • The index idx determines which entries are overwritten, according to the same rules of selecting and rearranging explained above.
 • The labels of the DataSeries are never modified.
 • For each type of indexing other than for modifying a single entry, there is special treatment of value if it is a Vector, an Array, or a DataSeries. This is described below.

Modifying a Single Entry

 • Let n be the number of entries of ds. This case applies if idx is a nonzero integer between -n and n, or a label of ds.
 • In this case, value overwrites the entry selected without any further processing.
 > ds[2] := 99;
 ${{\mathrm{ds}}}_{{2}}{≔}{99}$ (20)
 > ds;
 $\left[\begin{array}{cc}{a}& {101}\\ {b}& {99}\\ {c}& {103}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (21)
 > ds[c] := 104;
 ${{\mathrm{ds}}}_{{c}}{≔}{104}$ (22)
 > ds;
 $\left[\begin{array}{cc}{a}& {101}\\ {b}& {99}\\ {c}& {104}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (23)

Modifying a Range

 • Let n be the number of entries of ds. This case applies if idx is of the form a .. b, where a and b are either missing (so that we have .. b or a .. or  .. ) or nonzero integers between - n and n, or labels of ds.
 • If value is a Vector, an Array, or a DataSeries, then it needs to have exactly the number of elements that the range you are overwriting has. The entries are matched in order. If value is a DataSeries, its labels are ignored.
 • Otherwise, each entry in the range is assigned value.
 > ds[a .. 2] := 3;
 ${{\mathrm{ds}}}_{{a}{..}{2}}{≔}{3}$ (24)
 > ds;
 $\left[\begin{array}{cc}{a}& {3}\\ {b}& {3}\\ {c}& {104}\\ {2}& {104}\\ {12}& {105}\end{array}\right]$ (25)
 > ds[2 .. -2] := <103, 102, 101>;
 ${{\mathrm{ds}}}_{{2}{..}{-2}}{≔}\left[\begin{array}{c}{103}\\ {102}\\ {101}\end{array}\right]$ (26)
 > ds;
 $\left[\begin{array}{cc}{a}& {3}\\ {b}& {103}\\ {c}& {102}\\ {2}& {101}\\ {12}& {105}\end{array}\right]$ (27)

Modifying a DataSeries Using a Boolean DataSeries

 • This case applies if idx is a DataSeries object. In this case, all entries in idx must be true, false, or FAIL.
 • Consider the case where value is a Vector or an Array. For each i, if the ith entry of ds would occur in ds[idx], then ds[i] is overwritten with value[i]. This means that value should typically have at least as many entries as ds does (though strictly speaking, it is only necessary that it has as many entries as the position of the last occurring entry that would occur in ds[idx]).
 • Now consider the case where value is a DataSeries object. For each i, if the ith entry of ds would occur in ds[idx], then ds[i] is overwritten: either with the entry of value that has the same label as ds[i], or if no such value exists, with 0.
 • In all other cases, each entry of ds that would occur in ds[idx] is overwritten with value.
 > dsb;
 $\left[\begin{array}{cc}{a}& {\mathrm{true}}\\ {b}& {\mathrm{false}}\\ {2}& {\mathrm{true}}\\ {12}& {\mathrm{FAIL}}\\ {\mathrm{coconut}}& {\mathrm{true}}\\ {c}& {\mathrm{true}}\end{array}\right]$ (28)
 > ds[dsb] := <5, 4, 3, 2, 1>;
 ${{\mathrm{ds}}}_{{\mathrm{dsb}}}{≔}\left[\begin{array}{c}{5}\\ {4}\\ {3}\\ {2}\\ {1}\end{array}\right]$ (29)
 > ds;
 $\left[\begin{array}{cc}{a}& {5}\\ {b}& {103}\\ {c}& {3}\\ {2}& {2}\\ {12}& {105}\end{array}\right]$ (30)
 > ds2 := DataSeries([101, 102, 103, 104, 105, 106], labels = [b, c, d, 1, 2, 3]);
 ${\mathrm{ds2}}{≔}\left[\begin{array}{cc}{b}& {101}\\ {c}& {102}\\ {d}& {103}\\ {1}& {104}\\ {2}& {105}\\ {3}& {106}\end{array}\right]$ (31)
 > dsb4;
 $\left[\begin{array}{cc}{a}& {\mathrm{true}}\\ {b}& {\mathrm{false}}\\ {c}& {\mathrm{true}}\\ {2}& {\mathrm{true}}\\ {12}& {\mathrm{true}}\end{array}\right]$ (32)
 > ds[dsb4] := ds2;
 ${{\mathrm{ds}}}_{{\mathrm{dsb4}}}{≔}\left[\begin{array}{cc}{b}& {101}\\ {c}& {102}\\ {d}& {103}\\ {1}& {104}\\ {2}& {105}\\ {3}& {106}\end{array}\right]$ (33)
 > ds;
 $\left[\begin{array}{cc}{a}& {0}\\ {b}& {103}\\ {c}& {102}\\ {2}& {105}\\ {12}& {0}\end{array}\right]$ (34)
 > ds[dsb4] := 5;
 ${{\mathrm{ds}}}_{{\mathrm{dsb4}}}{≔}{5}$ (35)
 > ds;
 $\left[\begin{array}{cc}{a}& {5}\\ {b}& {103}\\ {c}& {5}\\ {2}& {5}\\ {12}& {5}\end{array}\right]$ (36)

Modifying Arbitrary Entries

 • This case applies if idx is a Vector, a list, or an Array.
 • Like in the case of selecting or rearranging entries, explained above, the values in idx are interpreted as positions if all of them are integers between -n and n, where n is the number of entries of ds. Otherwise, the values in idx are interpreted as labels. Both zero positions and missing labels are ignored.
 • If value is a Vector or an Array or a DataSeries object, then it must have exactly as many entries as idx. For each i, Maple assigns value[i] to the entry ds[idx[i]], unless idx[i] is a missing value.
 • Otherwise, value is assigned to each entry ds[idx[i]] that is not a missing value.
 > ds[<2, a>] := <105, 106>;
 ${{\mathrm{ds}}}_{⟨{2}{,}{a}⟩}{≔}\left[\begin{array}{c}{105}\\ {106}\end{array}\right]$ (37)
 > ds;
 $\left[\begin{array}{cc}{a}& {106}\\ {b}& {103}\\ {c}& {5}\\ {2}& {105}\\ {12}& {5}\end{array}\right]$ (38)
 > ds[[1, 2]] := ds[[2, 3]];
 ${{\mathrm{ds}}}_{\left[{1}{,}{2}\right]}{≔}\left[\begin{array}{cc}{b}& {103}\\ {c}& {5}\end{array}\right]$ (39)
 > ds;
 $\left[\begin{array}{cc}{a}& {103}\\ {b}& {5}\\ {c}& {5}\\ {2}& {105}\\ {12}& {5}\end{array}\right]$ (40)