Indexing a DataSeries - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Statistics and Data Analysis : DataFrames and DataSeries : DataSeries/indexing

Indexing a DataSeries

 

Calling Sequence

Parameters

Description

Selecting and Rearranging Entries

Reassigning Entries

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]);

ds:=a101b102c103210412105

(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[ .. ];

a101b102c103210412105

(6)
• 

Otherwise, the left and right-hand side are interpreted as single entries (shown above).

ds[a .. 2];

a101b102

(7)

ds[c ..];

c103210412105

(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']);

dsb:=atruebfalse2true12FAILcoconuttruectrue

(9)

ds[dsb];

a101c1032104

(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;

dsb2:=afalsebfalsecfalse2true12true

(11)

dsb3 := isprime~(ds);

dsb3:=atruebfalsectrue2false12false

(12)

dsb4 := dsb2 xor dsb3;

dsb4:=atruebfalsectrue2true12true

(13)

ds[dsb4];

a101c103210412105

(14)
  

You can put the previous few computations on a single line, too:

ds[ds >~ 103 xor isprime~(ds)];

a101c103210412105

(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]];

a101b1022104

(16)

ds[<-3, -2, 2>];

c1032104b102

(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]];

21040undefineda101

(18)

ds[[2, 3]];

b102c103

(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;

ds2:=99

(20)

ds;

a101b99c103210412105

(21)

ds[c] := 104;

dsc:=104

(22)

ds;

a101b99c104210412105

(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;

dsa..2:=3

(24)

ds;

a3b3c104210412105

(25)

ds[2 .. -2] := <103, 102, 101>;

ds2..2:=103102101

(26)

ds;

a3b103c102210112105

(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;

atruebfalse2true12FAILcoconuttruectrue

(28)

ds[dsb] := <5, 4, 3, 2, 1>;

dsdsb:=54321

(29)

ds;

a5b103c32212105

(30)

ds2 := DataSeries([101, 102, 103, 104, 105, 106], labels = [b, c, d, 1, 2, 3]);

ds2:=b101c102d103110421053106

(31)

dsb4;

atruebfalsectrue2true12true

(32)

ds[dsb4] := ds2;

dsdsb4:=b101c102d103110421053106

(33)

ds;

a0b103c1022105120

(34)

ds[dsb4] := 5;

dsdsb4:=5

(35)

ds;

a5b103c525125

(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>;

ds2&comma;a:=105106

(37)

ds;

a106b103c52105125

(38)

ds[[1, 2]] := ds[[2, 3]];

ds1&comma;2:=b103c5

(39)

ds;

a103b5c52105125

(40)

See Also

DataFrame

DataFrame,Guide

DataSeries

DataSeries,constructor