learning-it by example

#137 Python for data 6.3 min

Polars for pandas Users: One Report, Translated

One made-up report in pandas and Polars: same 4 rows, South at 6,814.5 in both.

1. In the pandas to Polars translation guide, a pandas boolean mask such as orders[orders['qty'] >= 5] becomes which Polars method, and what does assign become?
2. A column of whole numbers with some missing values (None) is loaded into pandas and into Polars. What type does each library give it in the pandas and Polars order report?
3. In the 1,000 made-up orders report, which regions hit their target, and what did South make?
4. Why does the Polars chain end with .sort('region') after the group by and the join?
5. What does the translation video show about the Polars lazy frame?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Predict a missed target

    Copy the code folder first. Before running anything, look at the report in the video: South made 6,814.5 against a target of 6500. In data.py change South's target from 6500 to 7000. Predict which columns in the South row change and which stay the same. Then run python run.py and check your prediction. This needs pandas and Polars as in the video's requirements.

    A hint

    The revenue and orders columns come from the orders, not from the targets.

  2. Remove the coupon gaps

    In data.py change rng.choice([5, 10, None]) to rng.choice([5, 10]) so no order lacks a coupon. Predict the two coupon dtype lines that run.py prints, given what the video said about whole numbers with gaps. Then run python run.py and compare. Does the report change?

    A hint

    The random choice now draws from two values, so the other made-up columns can change as well.

  3. Change the size filter

    In both pandas_way.py and polars_way.py change the quantity filter from 5 to 8. Predict whether each region's order count goes up or down, and whether pandas and Polars still agree. Run python run.py to check.

    A hint

    Change both files, or the agree line will show False for a reason that has nothing to do with Polars.

  4. Sort before the join

    In polars_way.py remove the .sort('region') line at the end and add .sort('region') right after the .agg(...) line, before the join. The video says an earlier draft like this made the lazy result come back in a different order. Predict what the two 'agree' lines will print, run python run.py, and say what you saw. The video also warns that the order is not promised, so the result may differ on another machine.

    A hint

    Put the line back where it was afterwards.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Made-up data of a thousand rows, five common steps only, and no timing, so it makes no speed or memory claim.

Up next in Python for data

More in Python for data

All Python for data videos

Related videos