Skip to content

Commit dbc164d

Browse files
authored
Fix pivot() to correctly accept options object as first parameter (#318)
Fix pivot() to correctly accept options object as first parameter to close #317
1 parent 9812d84 commit dbc164d

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

__tests__/dataframe.test.ts

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ Series: 'attributes' [struct[5]]
14121412
rows.map((e) => e["attributes"]),
14131413
);
14141414
});
1415-
test("pivot", () => {
1415+
test("pivot:values-with-options", () => {
14161416
{
14171417
const df = pl.DataFrame({
14181418
a: pl.Series([1, 2, 3]).cast(pl.Int32),
@@ -1507,6 +1507,115 @@ Series: 'attributes' [struct[5]]
15071507
separator: "/",
15081508
});
15091509

1510+
const expected = pl.DataFrame({
1511+
ix: [1, 2],
1512+
"foo/a": [1, 4],
1513+
"foo/b": [7, 1],
1514+
"bar/a": [2, 0],
1515+
"bar/b": [9, 4],
1516+
});
1517+
expect(actual).toFrameEqual(expected, true);
1518+
}
1519+
});
1520+
test("pivot:options-only", () => {
1521+
{
1522+
const df = pl.DataFrame({
1523+
a: pl.Series([1, 2, 3]).cast(pl.Int32),
1524+
b: pl
1525+
.Series([
1526+
[1, 1],
1527+
[2, 2],
1528+
[3, 3],
1529+
])
1530+
.cast(pl.List(pl.Int32)),
1531+
});
1532+
1533+
const expected = pl
1534+
.DataFrame({
1535+
a: pl.Series([1, 2, 3]).cast(pl.Int32),
1536+
"1": pl.Series([[1, 1], null, null]).cast(pl.List(pl.Int32)),
1537+
"2": pl.Series([null, [2, 2], null]).cast(pl.List(pl.Int32)),
1538+
"3": pl.Series([null, null, [3, 3]]).cast(pl.List(pl.Int32)),
1539+
})
1540+
.select("a", "1", "2", "3");
1541+
1542+
const actual = df.pivot({
1543+
values: "b",
1544+
index: "a",
1545+
on: "a",
1546+
aggregateFunc: "first",
1547+
sortColumns: true,
1548+
});
1549+
1550+
expect(actual).toFrameEqual(expected, true);
1551+
}
1552+
1553+
{
1554+
const df = pl.DataFrame({
1555+
a: ["beep", "bop"],
1556+
b: ["a", "b"],
1557+
c: ["s", "f"],
1558+
d: [7, 8],
1559+
e: ["x", "y"],
1560+
});
1561+
const actual = df.pivot({
1562+
values: ["a", "e"],
1563+
index: "b",
1564+
on: ["b"],
1565+
aggregateFunc: "first",
1566+
separator: "|",
1567+
maintainOrder: true,
1568+
});
1569+
1570+
const expected = pl.DataFrame({
1571+
b: ["a", "b"],
1572+
"a|a": ["beep", null],
1573+
"a|b": [null, "bop"],
1574+
"e|a": ["x", null],
1575+
"e|b": [null, "y"],
1576+
});
1577+
expect(actual).toFrameEqual(expected, true);
1578+
}
1579+
{
1580+
const df = pl.DataFrame({
1581+
foo: ["A", "A", "B", "B", "C"],
1582+
N: [1, 2, 2, 4, 2],
1583+
bar: ["k", "l", "m", "n", "o"],
1584+
});
1585+
const actual = df.pivot({
1586+
values: ["N"],
1587+
index: "foo",
1588+
on: "bar",
1589+
aggregateFunc: "first",
1590+
});
1591+
1592+
const expected = pl.DataFrame({
1593+
foo: ["A", "B", "C"],
1594+
k: [1, null, null],
1595+
l: [2, null, null],
1596+
m: [null, 2, null],
1597+
n: [null, 4, null],
1598+
o: [null, null, 2],
1599+
});
1600+
1601+
expect(actual).toFrameEqual(expected, true);
1602+
}
1603+
{
1604+
const df = pl.DataFrame({
1605+
ix: [1, 1, 2, 2, 1, 2],
1606+
col: ["a", "a", "a", "a", "b", "b"],
1607+
foo: [0, 1, 2, 2, 7, 1],
1608+
bar: [0, 2, 0, 0, 9, 4],
1609+
});
1610+
1611+
const actual = df.pivot({
1612+
values: ["foo", "bar"],
1613+
index: "ix",
1614+
on: "col",
1615+
aggregateFunc: "sum",
1616+
separator: "/",
1617+
});
1618+
15101619
const expected = pl.DataFrame({
15111620
ix: [1, 2],
15121621
"foo/a": [1, 4],

polars/dataframe.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,9 @@ export const _DataFrame = (_df: any): DataFrame => {
23482348
.map((d) => mapFn(_DataFrame(d)));
23492349
},
23502350
pivot(values, options?) {
2351+
if (values && !options) {
2352+
options = values;
2353+
}
23512354
let {
23522355
values: values_,
23532356
index,

0 commit comments

Comments
 (0)