Processing Cartesian Products with PyODPS DataFrame

By Ji Sheng

PyODPS provides DataFrame APIs to analyze and preprocess large-scale data with interfaces like Pandas. This article mainly introduces how to use PyODPS to perform Cartesian product operations.

The most common scenario of Cartesian products is the comparison or operation of two collections. Taking the computation of geographic distance as an example, assume that the big table Coordinates1 stores the longitude and latitude coordinates of destination points, with a total of M rows of data, while the small table Coordinates2 stores the longitude and latitude coordinates of departure points, with a total of N rows of data. Now, the coordinates of all the departure points closest to each of the destination points need to be computed. For a destination point, we need to compute the distance from all the departure points to the destination point and find the minimum distance. Therefore, the entire intermediate process requires M * N data records, which results in a Cartesian product.

Haversine Formula

First, let’s have a brief understanding of the background. Given the longitude and latitude of the coordinate points of two geographical locations, the haversine formula can be used to compute the distance between the two points. The Python expression is as follows:


Currently, MapJoin is the most recommended method. In PyODPS, the method of using MapJoin is simple. You only need to specify mapjoin=True when two dataframes join, and the MapJoin will be performed on the table on the right during execution.

We can see that the _x and _y suffixes are added to the duplicate columns by default during Join operations. The suffixes can be customized by passing a binary tuple in the suffixes parameter. After the joined table is obtained, the distance can be computed by using the user-defined function DataFrame in PyODPS, which is very simple, clear and efficient.

df3 already has M * N data records. Next, if we need to know the minimum distance, we can directly call groupby to df3, followed by the min aggregate function, to get the minimum distance of each target point.

DataFrame User-defined Function

If we need to know the city corresponding to the minimum distance point, that is, the corresponding ID in the table, we can call MapReduce after MapJoin, but we can also use the apply method of DataFrame. To use user-defined functions (UDFs) for one row, we can use the apply method. The axis parameter must be 1 to indicate that the operation works on the row.

Table Resources

Note that “apply” is a UDF executed on the server, so expressions similar to df=o.get_table('table_name').to_df() cannot be used in the function to obtain table data. The specific principle is covered in Where is the Code for PyODPS DataFrame Executed?. Take the case in this article as an example. To compute all records in Tables 1 and 2, we need to use table 2 as a resource table, and then reference the table resource in customization. It is also very convenient to use table resources in PyODPS. You only need to pass a collection into the resources parameter. A collection is an iteratable object, not a DataFrame object, and cannot directly call the DataFrame interface. Each iteration value is a namedtuple, and the corresponding value can be obtained by the field name or offset.

In a UDF, the table resource is looped to be read into the Pandas DataFrame, and the row corresponding to the minimum value can be easily found by using the Pandas loc, thus obtaining the ID of the closest departure point.

Global Variable

When the data size of a small table is small, we can even use the data in the small table as global variables in a UDF.

When the function is uploaded, the global variables (df in the above code) used in the function will be pickled into the UDF. However, note that applicable use cases for this method are very limited, because the size of files to be uploaded in ODPS is limited. Therefore, if there is too much data, the resources generated by the UDF will be too large to be uploaded. In addition, for this method, it is better to ensure that the client version of the third-party package is consistent with that of the server. Otherwise, serialization problems are very likely to occur, so we recommend that you only use this method when dealing with small data volumes.


PyODPS can be used to solve Cartesian product problems in two ways: One is MapJoin, which is intuitive and has good performance. Generally, we recommend MapJoin to solve problems that can be solved by MapJoin, and it is better to compute using built-in functions to maximize efficiency. The problem is that it is not flexible enough. The other is the DataFrame UDF, which is relatively flexible, but its performance is relatively poor (Pandas or NumPy can be used to improve performance). By using table resources, the small table is passed into the DataFrame UDF as a table resource to complete the Cartesian product operation.

Original Source

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store