Skip to content

Commit f1f6f29

Browse files
Update tutorial_code.ipynb
1 parent 8190985 commit f1f6f29

File tree

1 file changed

+27
-62
lines changed

1 file changed

+27
-62
lines changed

polars-missing-data/tutorial_code.ipynb

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"!python -m pip install polars\n"
18+
"!python -m pip install polars"
1919
]
2020
},
2121
{
@@ -29,7 +29,7 @@
2929
"\n",
3030
"tips = pl.scan_parquet(\"tips.parquet\")\n",
3131
"\n",
32-
"tips.collect()\n"
32+
"tips.collect()"
3333
]
3434
},
3535
{
@@ -39,10 +39,7 @@
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
42-
"(\n",
43-
" tips\n",
44-
" .null_count()\n",
45-
").collect()\n"
42+
"(tips.null_count()).collect()"
4643
]
4744
},
4845
{
@@ -64,12 +61,7 @@
6461
"\n",
6562
"tips = pl.scan_parquet(\"tips.parquet\")\n",
6663
"\n",
67-
"(\n",
68-
" tips\n",
69-
" .filter(\n",
70-
" pl.col(\"total\").is_null() & pl.col(\"tip\").is_null()\n",
71-
" )\n",
72-
").collect()\n"
64+
"(tips.filter(pl.col(\"total\").is_null() & pl.col(\"tip\").is_null())).collect()"
7365
]
7466
},
7567
{
@@ -80,12 +72,10 @@
8072
"outputs": [],
8173
"source": [
8274
"(\n",
83-
" tips\n",
84-
" .drop_nulls(pl.col(\"total\"))\n",
85-
" .filter(\n",
75+
" tips.drop_nulls(pl.col(\"total\")).filter(\n",
8676
" pl.col(\"total\").is_null() & pl.col(\"tip\").is_null()\n",
8777
" )\n",
88-
").collect()\n"
78+
").collect()"
8979
]
9080
},
9181
{
@@ -99,7 +89,7 @@
9989
" tips.drop_nulls(pl.col(\"total\"))\n",
10090
" .with_columns(pl.col(\"tip\").fill_null(0))\n",
10191
" .filter(pl.col(\"tip\").is_null())\n",
102-
").collect()\n"
92+
").collect()"
10393
]
10494
},
10595
{
@@ -121,7 +111,7 @@
121111
"\n",
122112
"tips = pl.scan_parquet(\"tips.parquet\")\n",
123113
"\n",
124-
"(tips.filter(pl.col(\"time\").is_null())).collect()\n"
114+
"(tips.filter(pl.col(\"time\").is_null())).collect()"
125115
]
126116
},
127117
{
@@ -131,12 +121,7 @@
131121
"metadata": {},
132122
"outputs": [],
133123
"source": [
134-
"(\n",
135-
" tips\n",
136-
" .filter(\n",
137-
" pl.col(\"record_id\").is_in([2, 3, 4, 14, 15, 16])\n",
138-
" )\n",
139-
").collect()\n"
124+
"(tips.filter(pl.col(\"record_id\").is_in([2, 3, 4, 14, 15, 16]))).collect()"
140125
]
141126
},
142127
{
@@ -147,12 +132,11 @@
147132
"outputs": [],
148133
"source": [
149134
"(\n",
150-
" tips\n",
151-
" .drop_nulls(\"total\")\n",
135+
" tips.drop_nulls(\"total\")\n",
152136
" .with_columns(pl.col(\"tip\").fill_null(0))\n",
153137
" .with_columns(pl.col(\"time\").fill_null(strategy=\"forward\"))\n",
154138
" .filter(pl.col(\"record_id\").is_in([3, 15]))\n",
155-
").collect()\n"
139+
").collect()"
156140
]
157141
},
158142
{
@@ -174,12 +158,7 @@
174158
"\n",
175159
"tips = pl.scan_parquet(\"tips.parquet\")\n",
176160
"\n",
177-
"(\n",
178-
" tips\n",
179-
" .filter(\n",
180-
" pl.all_horizontal(pl.col(\"total\", \"tip\").is_null())\n",
181-
" )\n",
182-
").collect()\n"
161+
"(tips.filter(pl.all_horizontal(pl.col(\"total\", \"tip\").is_null()))).collect()"
183162
]
184163
},
185164
{
@@ -191,12 +170,7 @@
191170
"source": [
192171
"tips = pl.scan_parquet(\"tips.parquet\")\n",
193172
"\n",
194-
"(\n",
195-
" tips\n",
196-
" .filter(\n",
197-
" ~pl.all_horizontal(pl.col(\"total\", \"tip\").is_null())\n",
198-
" )\n",
199-
").collect()\n"
173+
"(tips.filter(~pl.all_horizontal(pl.col(\"total\", \"tip\").is_null()))).collect()"
200174
]
201175
},
202176
{
@@ -211,13 +185,10 @@
211185
"tips = pl.scan_parquet(\"tips.parquet\")\n",
212186
"\n",
213187
"(\n",
214-
" tips\n",
215-
" .filter(\n",
216-
" ~pl.all_horizontal(pl.col(\"total\", \"tip\").is_null())\n",
217-
" )\n",
188+
" tips.filter(~pl.all_horizontal(pl.col(\"total\", \"tip\").is_null()))\n",
218189
" .with_columns(pl.col(\"tip\").fill_null(0))\n",
219190
" .with_columns(pl.col(\"time\").fill_null(strategy=\"forward\"))\n",
220-
").null_count().collect()\n"
191+
").null_count().collect()"
221192
]
222193
},
223194
{
@@ -247,7 +218,7 @@
247218
" }\n",
248219
")\n",
249220
"\n",
250-
"scientists.collect()\n"
221+
"scientists.collect()"
251222
]
252223
},
253224
{
@@ -263,7 +234,7 @@
263234
" scientists.with_columns(cs.string().fill_null(\"Unknown\")).with_columns(\n",
264235
" cs.integer().fill_null(0)\n",
265236
" )\n",
266-
").collect()\n"
237+
").collect()"
267238
]
268239
},
269240
{
@@ -285,7 +256,7 @@
285256
"\n",
286257
"sales_trends = pl.scan_csv(\"sales_trends.csv\")\n",
287258
"\n",
288-
"sales_trends.collect()\n"
259+
"sales_trends.collect()"
289260
]
290261
},
291262
{
@@ -296,13 +267,12 @@
296267
"outputs": [],
297268
"source": [
298269
"(\n",
299-
" sales_trends\n",
300-
" .with_columns(\n",
270+
" sales_trends.with_columns(\n",
301271
" pl.col(\"next_year\").replace(\n",
302272
" [float(\"inf\"), -float(\"inf\"), float(\"NaN\")], None\n",
303273
" )\n",
304274
" )\n",
305-
").collect()\n"
275+
").collect()"
306276
]
307277
},
308278
{
@@ -313,19 +283,17 @@
313283
"outputs": [],
314284
"source": [
315285
"(\n",
316-
" sales_trends\n",
317-
" .with_columns(\n",
286+
" sales_trends.with_columns(\n",
318287
" pl.col(\"next_year\").replace(\n",
319288
" [float(\"inf\"), -float(\"inf\"), float(\"NaN\")], None\n",
320289
" )\n",
321-
" )\n",
322-
" .with_columns(\n",
290+
" ).with_columns(\n",
323291
" pl.col(\"next_year\").fill_null(\n",
324292
" pl.col(\"current_year\")\n",
325293
" + (pl.col(\"current_year\") - pl.col(\"last_year\"))\n",
326294
" )\n",
327295
" )\n",
328-
").collect()\n"
296+
").collect()"
329297
]
330298
},
331299
{
@@ -347,7 +315,7 @@
347315
"\n",
348316
"episodes = pl.scan_parquet(\"ft_exercise.parquet\")\n",
349317
"\n",
350-
"episodes.null_count().collect()\n"
318+
"episodes.null_count().collect()"
351319
]
352320
},
353321
{
@@ -362,8 +330,7 @@
362330
"episodes = pl.scan_parquet(\"ft_exercise.parquet\")\n",
363331
"\n",
364332
"(\n",
365-
" episodes\n",
366-
" .with_columns(\n",
333+
" episodes.with_columns(\n",
367334
" pl.when(pl.col(\"episode\") == 6)\n",
368335
" .then(pl.col(\"series\").fill_null(strategy=\"forward\"))\n",
369336
" .otherwise(pl.col(\"series\").fill_null(strategy=\"backward\"))\n",
@@ -373,10 +340,8 @@
373340
" .then(pl.col(\"title\").fill_null(\"The Hotel Inspectors\"))\n",
374341
" .otherwise(pl.col(\"title\").fill_null(\"Waldorf Salad\"))\n",
375342
" )\n",
376-
" .with_columns(\n",
377-
" pl.col(\"original_date\").interpolate()\n",
378-
" )\n",
379-
").null_count().collect()\n"
343+
" .with_columns(pl.col(\"original_date\").interpolate())\n",
344+
").null_count().collect()"
380345
]
381346
}
382347
],

0 commit comments

Comments
 (0)