# Python vs. SPL 5 – Order-related Operation

We are naturally interested in order-related operations which include calculating the rate of last period or YOY. This article is focused on comparing the order-related operations between Python and SPL.

## Sorting

Sorting is the most common order-related operation, for example:

### Sort stock 000062 by date.

The data of the stock are as follows:

Python

Pandas provides the sort_values function to sort a certain column, and the function is very easy to operate.

SPL

SPL uses the sort(...) function to perform the sorting.

Occasionally we need to sort multiple columns in different directions, for example:

### Sort all the stock data by code in ascending order and price in descending order.

Python

In the sort_values() function, the “by” and “ascending” parameters support multiple columns and sorting in different directions respectively, which is relatively easy.

SPL

SPL still uses the sort(...) function to perform multiple keys sorting in different directions, which is equally convenient.

## The position/index after sorting

The position information after sorting is also very important and may be used sometimes. For example:

### Calculate the rising rate of the three highest stock (code: 000062) prices.

Python

The argsort(...) function in Python returns the position information after sorting. Since neither is there loop function in Python nor can we use the position information to calculate in the loop, we can only first find the three highest stock prices, then find the stoke prices of the day before the three highest ones, and calculate both to get the rising rate, which is a bit convoluted and burdensome.

SPL

The psort(...) function in SPL returns the position information from the smallest to largest, and @z option indicates the reverse order. The calc(...) function is a positioning calculation, which uses the position and relative position of the members to perform the calculation. CLOSING[-1] indicates the previous member of the current member.

We can also use the ptop(...) function to calculate the position information of the three highest stock prices, which spares a full sorting and is more effective. The code is rewritten as follows: A9 is no longer needed; A10=A3.ptop(-3,CLOSING); A11 calculates as normal.

By extension, what do Python and SPL do when we need to use multiple columns of position information in different directions?

### Calculate the position/index after sorting by the stock code in ascending order and stock price in descending order.

Python

Here Python uses another new function, lexsort(...), which is not in Pandas but Numpy library. The normal logic is to first sort by STOCKID in ascending order and then by CLOSING in descending order, usually the order of importance descending from left to right, but Python has to put the first-sorted STOCKID at the end, followed by CLOSING; if three or more columns need to be sorted, the importance will be descended from right to left, which is against the natural logic. In addition, this function has no parameter for reverse order which can only be represented by a minus sign “-”. However, Python dose not support the minus sign on strings, which will report an error if there exists one. Therefore, the lexsort() function will not work if the calculation is to sort by STOCKID in descending order and by CLOSING in ascending order, we have to use another method. You can try the following code and find what will happen in Python:

np.lexsort((stock_data["CLOSING"],-stock_data["STOCKID"]))

SPL

In SPL, the whole calculation is much more easier. Like the sort function, the psort()function is used to return the sorted positions naturally. And SPL supports the minus sign in strings (-STOCKID is sorted in reverse order).

## Adjacent record reference

During data analysis, we often refer to the adjacent records in a certain order to calculate. For example:

### Calculate the daily rate of rising/falling for stock 000062.

Python

The shift(n) function in Python moves n rows of data, and the parameter n can be a positive or negative number, for instance, 1 is to move down one row; -1 is to move up one row. In this way, we can use the moved data to calculate with the current row.

SPL

In SPL, the CLOSING[-1] in the loop function is the CLOSING value of the record that is 1 unit before the current record (i.e., the previous record), CLOSING[-1]=~[-1].CLOSING, which is the same as CLOSING[-1] in the above example.

SPL uses the relative positions to perform such operations and does not move all the rows like Python, which is simple in syntax and effective in performance.

Moving average is also a common operation, for example:

### Calculate the moving average of 5 consecutive trading days for stock 000062.

Python

The rolling() function can calculate the moving average easily, and the result is an object, which needs to be followed by an aggregation function subsequently. If following function is “mean”, it calculates the moving average; if “sum” function follows, it calculates the moving sum. And “object” is a bit difficult to understand. In addition, although calculating the average of the previous row and the 5 adjacent rows is the same kind of operation, Python actually uses two unrelated functions, “shift” and “rolling” respectively, to calculate them.

SPL

SPL still uses the form of CLOSING[...] to implement the calculation except that [-4:0] indicates all the records from the previous 4th rows to the current row. CLOSING[-4:0]=~[-4:0].(CLOSING). The form of adjacent record reference is accordant and very easy to understand and memorize. This is also consistent with the use of ~[...] in sequence, which is described in the “Adjacent reference” section of Python vs. SPL -- Loop Function.

## Summary

There are abundant functions about order-related operations in Python, but they are relatively simple and rough. For instance, there is no relative positions, and the functions have to move the whole data up or down or even use an object to replace them; the functions for calculating positions are also weak, some of which need to be hard-coded or “detoured”, making both coding and executing much less efficient. These functions are designed chaotically and difficult to memorize.

On the contrary, SPL performs professionally in terms of order-related operations. It has plenty of functions for order-related operations and gets both absolute and relative positions easily in the loop. The functions are also elaborately designed such as (sort,psort), (select,pselect), (max,pmax,maxp), (top,ptop), etc., and they are similar in syntax and form, so using them is like building Legos. It is easy to write all kinds of complex code (houses, airplanes and other complex shapes) after knowing a few atomic functions (basic block shapes).