nodejs-polars icon indicating copy to clipboard operation
nodejs-polars copied to clipboard

`toRecords` should support empty header fields

Open ad-si opened this issue 1 year ago • 2 comments

Have you tried latest version of polars?

  • [yes]

What version of polars are you using?

0.14

What operating system are you using polars on?

macOS 14.6.1

What node version are you using

bun 1.1.21

Describe your bug

Using toRecords() on a dataframe with an empty header cell should work, but currently it throws an Error.

What are the steps to reproduce the behavior?

import pl from "nodejs-polars"

const csv =`"name",""
"John","green"
"Anna","red"
`
pl.readCSV(csv, { quoteChar: "\"" }).toRecords()
474 |             _df.writeCsv(writeStream, dest ?? options);
475 |             writeStream.end("");
476 |             return Buffer.concat(buffers);
477 |         },
478 |         toRecords() {
479 |             return _df.toObjects();
                             ^
error: Failed to set property with field ``
 code: "InvalidArg"

ad-si avatar Aug 22 '24 02:08 ad-si

As a workaround you can add a .withColumnRenamed("", "__EMPTY")

Edit: No, you can't because it will throw an error if the column name does not exist. 😮‍💨 Use this instead:

if (df.columns.includes("")) {
  df = df.withColumnRenamed("", "__EMPTY")
}

ad-si avatar Aug 22 '24 03:08 ad-si

Example above works fine using nodejs-polars 0.21.0 and bun 1.2.20

[
  {
    name: "John",
    "": "green",
  }, {
    name: "Anna",
    "": "red",
  }
]

Bidek56 avatar Aug 14 '25 21:08 Bidek56