Skip to content

Commit 03c3a87

Browse files
Copilotmrjf
andauthored
Fix Python code in playground HTML files for pandas 3.0 compatibility
- json.html: Add import io and wrap pd.read_json() string args with io.StringIO() in blocks 1-5 and 7 - stack_unstack.html: Remove deprecated dropna parameter from df.stack() calls; chain .dropna() after stack where needed - csv.html: Fix skiprows=2 to skiprows=range(1, 3) to preserve header row in block 5 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: mrjf <180956+mrjf@users.noreply.github.com>
1 parent 6b7c111 commit 03c3a87

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

playground/csv.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ <h2>5 · Limiting rows</h2>
402402
first3 = pd.read_csv(io.StringIO(csv), nrows=3)
403403
print("nrows=3:", list(first3["val"]))
404404

405-
skip2 = pd.read_csv(io.StringIO(csv), skiprows=2)
405+
skip2 = pd.read_csv(io.StringIO(csv), skiprows=range(1, 3))
406406
print("skiprows=2:", list(skip2["val"]))</textarea>
407407
<div class="playground-output">Click ▶ Run to execute</div>
408408
<div class="playground-hint">Ctrl+Enter to run · Tab to indent</div>

playground/json.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ <h2>1 · Parse records JSON (default)</h2>
216216
console.log("names:", [...df.col("name").values]);
217217
console.log("ages:", [...df.col("age").values]);</textarea>
218218
<textarea class="playground-python" style="display:none">import pandas as pd
219+
import io
219220

220221
json_text = '[{"name": "alice", "age": 30, "score": 88.5}, {"name": "bob", "age": 25, "score": 92.0}, {"name": "carol", "age": 35, "score": 75.3}]'
221222

222-
df = pd.read_json(json_text)
223+
df = pd.read_json(io.StringIO(json_text))
223224
print("shape:", df.shape)
224225
print("columns:", list(df.columns))
225226
print("names:", list(df["name"]))
@@ -259,14 +260,15 @@ <h2>2 · Split orient</h2>
259260
console.log("y:", [...df.col("y").values]);</textarea>
260261
<textarea class="playground-python" style="display:none">import pandas as pd
261262
import json
263+
import io
262264

263265
data = json.dumps({
264266
"columns": ["x", "y"],
265267
"index": [10, 20, 30],
266268
"data": [[1, 4], [2, 5], [3, 6]],
267269
})
268270

269-
df = pd.read_json(data, orient="split")
271+
df = pd.read_json(io.StringIO(data), orient="split")
270272
print("orient auto-detected: split")
271273
print("shape:", df.shape)
272274
print("index:", list(df.index))
@@ -305,14 +307,15 @@ <h2>3 · Index orient</h2>
305307
console.log("b:", [...df.col("b").values]);</textarea>
306308
<textarea class="playground-python" style="display:none">import pandas as pd
307309
import json
310+
import io
308311

309312
data = json.dumps({
310313
"r0": {"a": 1, "b": "foo"},
311314
"r1": {"a": 2, "b": "bar"},
312315
"r2": {"a": 3, "b": "baz"},
313316
})
314317

315-
df = pd.read_json(data, orient="index")
318+
df = pd.read_json(io.StringIO(data), orient="index")
316319
print("shape:", df.shape)
317320
print("index:", list(df.index))
318321
print("a:", list(df["a"]))
@@ -350,13 +353,14 @@ <h2>4 · Columns orient</h2>
350353
console.log("y:", [...df.col("y").values]);</textarea>
351354
<textarea class="playground-python" style="display:none">import pandas as pd
352355
import json
356+
import io
353357

354358
data = json.dumps({
355359
"x": {"0": 10, "1": 20, "2": 30},
356360
"y": {"0": 1.1, "1": 2.2, "2": 3.3},
357361
})
358362

359-
df = pd.read_json(data, orient="columns")
363+
df = pd.read_json(io.StringIO(data), orient="columns")
360364
print("shape:", df.shape)
361365
print("index:", list(df.index))
362366
print("x:", list(df["x"]))
@@ -389,10 +393,11 @@ <h2>5 · Values orient</h2>
389393
console.log("columns:", [...df.columns.values]);
390394
console.log("col 0:", [...df.col("0").values]);</textarea>
391395
<textarea class="playground-python" style="display:none">import pandas as pd
396+
import io
392397

393398
json_text = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
394399

395-
df = pd.read_json(json_text, orient="values")
400+
df = pd.read_json(io.StringIO(json_text), orient="values")
396401
print("shape:", df.shape)
397402
print("columns:", list(df.columns))
398403
print("col 0:", list(df[0]))</textarea>
@@ -474,14 +479,15 @@ <h2>7 · Round-trip</h2>
474479
console.log("label restored:", [...restored.col("label").values]);
475480
console.log("shapes match:", JSON.stringify(original.shape) === JSON.stringify(restored.shape));</textarea>
476481
<textarea class="playground-python" style="display:none">import pandas as pd
482+
import io
477483

478484
original = pd.DataFrame({
479485
"x": [1, 2, 3, 4, 5],
480486
"label": ["a", "b", "c", "d", "e"],
481487
})
482488

483489
json_text = original.to_json(orient="split")
484-
restored = pd.read_json(json_text, orient="split")
490+
restored = pd.read_json(io.StringIO(json_text), orient="split")
485491

486492
print("JSON:", json_text)
487493
print("x restored:", list(restored["x"]))

playground/stack_unstack.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ <h2>1 · Basic stack</h2>
215215
index=["x", "y", "z"],
216216
)
217217

218-
s = df.stack(dropna=False)
218+
s = df.stack()
219219
print("index:", list(s.index))
220220
print("values:", list(s.values))</textarea>
221221
<div class="playground-output">Click ▶ Run to execute</div>
@@ -261,13 +261,13 @@ <h2>2 · stack drops null by default</h2>
261261
index=["a", "b", "c"],
262262
)
263263

264-
# default: dropna=True
265-
s = df.stack(dropna=True)
264+
# default: stack preserves NaN, chain dropna() to remove
265+
s = df.stack().dropna()
266266
print("dropna=True index:", list(s.index))
267267
print("dropna=True values:", list(s.values))
268268

269-
# keep nulls
270-
s_full = df.stack(dropna=False)
269+
# keep nulls (default in pandas 3.0)
270+
s_full = df.stack()
271271
print("dropna=False index:", list(s_full.index))
272272
print("dropna=False values:", list(s_full.values))</textarea>
273273
<div class="playground-output">Click ▶ Run to execute</div>
@@ -308,7 +308,7 @@ <h2>3 · unstack: recover the original DataFrame</h2>
308308
index=["x", "y"],
309309
)
310310

311-
s = df.stack(dropna=False)
311+
s = df.stack()
312312
recovered = s.unstack()
313313

314314
print("index:", list(recovered.index))
@@ -355,8 +355,8 @@ <h2>4 · unstack fills missing cells</h2>
355355
index=["jan", "feb", "mar"],
356356
)
357357

358-
# stack drops nulls by default
359-
s = df.stack(dropna=True)
358+
# stack then drop nulls
359+
s = df.stack().dropna()
360360

361361
# unstack with fill_value=0
362362
recovered = s.unstack(fill_value=0)
@@ -400,7 +400,7 @@ <h2>5 · Custom separator</h2>
400400
)
401401

402402
# pandas uses MultiIndex natively, no separator needed
403-
s = df.stack(dropna=False)
403+
s = df.stack()
404404
print("stacked index:", list(s.index))
405405

406406
recovered = s.unstack()
@@ -455,8 +455,8 @@ <h2>6 · Reshape workflow: stack → filter → unstack</h2>
455455
index=["Q1", "Q2", "Q3", "Q4"],
456456
)
457457

458-
# Stack (drops nulls), filter to values > 80, then unstack
459-
s = df.stack(dropna=True)
458+
# Stack then drop nulls, filter to values > 80, then unstack
459+
s = df.stack().dropna()
460460
filtered = s[s > 80]
461461
result = filtered.unstack(fill_value=0)
462462

0 commit comments

Comments
 (0)