Skip to content

Commit a6ab81a

Browse files
Fix transpose with option columnNames working with arrays (#195)
* Fix slice for columnNames in transpose * Add return type for transpose
1 parent dda9327 commit a6ab81a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

__tests__/dataframe.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,22 @@ describe("dataframe", () => {
11041104
const actual = df.transpose({ includeHeader: false, columnNames: "abc" });
11051105
expect(actual).toFrameEqual(expected);
11061106
});
1107+
test("transpose:columnNames:array", () => {
1108+
const expected = pl.DataFrame({
1109+
a: [1, 1],
1110+
b: [2, 2],
1111+
c: [3, 3],
1112+
});
1113+
const df = pl.DataFrame({
1114+
a: [1, 2, 3],
1115+
b: [1, 2, 3],
1116+
});
1117+
const actual = df.transpose({
1118+
includeHeader: false,
1119+
columnNames: ["a", "b", "c"],
1120+
});
1121+
expect(actual).toFrameEqual(expected);
1122+
});
11071123
test("transpose:columnNames:generator", () => {
11081124
const expected = pl.DataFrame({
11091125
col_0: [1, 1],

polars/dataframe.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ export interface DataFrame
15941594
includeHeader?: boolean;
15951595
headerName?: string;
15961596
columnNames?: Iterable<string>;
1597-
});
1597+
}): DataFrame;
15981598
/**
15991599
* Drop duplicate rows from this DataFrame.
16001600
* Note that this fails if there is a column of type `List` in the DataFrame.
@@ -2521,7 +2521,7 @@ export const _DataFrame = (_df: any): DataFrame => {
25212521
const headeName = options?.headerName ?? "column";
25222522
const keep_names_as = includeHeader ? headeName : undefined;
25232523
if (options?.columnNames) {
2524-
function takeNItems(iterable: Iterable<string>, n) {
2524+
function takeNItems(iterable: Iterable<string>, n: number) {
25252525
const result: Array<string> = [];
25262526
let i = 0;
25272527
for (const item of iterable) {
@@ -2534,7 +2534,7 @@ export const _DataFrame = (_df: any): DataFrame => {
25342534
return result;
25352535
}
25362536
options.columnNames = Array.isArray(options.columnNames)
2537-
? options.columnNames.slice(this.height)
2537+
? options.columnNames.slice(0, this.height)
25382538
: takeNItems(options.columnNames, this.height);
25392539
}
25402540
if (!options?.columnNames) {

0 commit comments

Comments
 (0)