Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date objects serialized by toRecords fail instanceof Date check #236

Closed
davidgovea opened this issue Jun 26, 2024 · 5 comments
Closed

Date objects serialized by toRecords fail instanceof Date check #236

davidgovea opened this issue Jun 26, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@davidgovea
Copy link
Contributor

davidgovea commented Jun 26, 2024

Have you tried latest version of polars?

  • yes

What version of polars are you using?

Observed on 0.10, still present on main branch

What operating system are you using polars on?

Mac / Linux

What node version are you using

Node 20

Describe your bug.

When .toRecords() returns a Date object, it fails instanceof Date checks

What are the steps to reproduce the behavior?

Extended the existing test with a demonstration:
https://github.com/davidgovea/nodejs-polars/pull/3/files

What is the actual behavior?

See test run here: https://github.com/davidgovea/nodejs-polars/actions/runs/9680675801/job/26709383256

serializedDate.constructor.name
=> 'Date' ✅

serializedDate instanceof Date
=> false ❌

What is the expected behavior?

x instanceof Date should be true.


Have not taken a look under the hood yet, but I am happy to do so when I get a free moment. For now, I'm just paving over the issue with an additional new Date(polarsToRecordsDate) wrapper in my code

@davidgovea davidgovea added the bug Something isn't working label Jun 26, 2024
@universalmind303 universalmind303 self-assigned this Jun 26, 2024
@Bidek56
Copy link
Collaborator

Bidek56 commented Jul 7, 2024

toRecords returns a JSON array: [{"date":"2024-07-07T00:00:00.000Z"}]
Why would you expect actual[0].date to be an instance of a Date?
I think you need to convert it to Date to be a Date else it will be a string/object just like everything else in JSON.

@davidgovea
Copy link
Contributor Author

Hey @Bidek56, thanks for taking a look!

I'd be fine with that being the case (everything serialized to ISO date string), but today it seems like we're in a strange middleground.

If it were as you suggest, then the constructor.name would be String, not Date.
Additionally, the object has all of the Date methods available.
Screenshot 2024-07-08 at 11 41 10 AM

If it quacks like a Date, I'd assume it should pass instanceof Date 😆

But yeah making toRecords return standard JSON would make sense too.

Screenshot 2024-07-08 at 11 45 17 AM

@Bidek56
Copy link
Collaborator

Bidek56 commented Jul 8, 2024

If you are looking for schema, I would suggest using df.toDataResource() which starts with df.toRecords() then it adds field type to be:
{"data":[{"date":"2024-07-08T00:00:00.000Z"}],"schema":{"fields":[{"name":"date","type":"string"}]}}

@universalmind303
Copy link
Collaborator

@davidgovea this seems like a bug in jest

Using alternative test runners, it all passes

> node example.js

const pl = require("nodejs-polars");
const { test } = require("node:test");
const assert = require('node:assert/strict');
test("toRecords:date", () => {
	const df = pl
		.DataFrame({
			date: [new Date()],
		})
		.withColumn(pl.col("date").cast(pl.Date).alias("date"));

	const serializedDate = df.toRecords()[0].date;
	assert.strictEqual(serializedDate instanceof Date, true);
});

> bun test example.js

const pl = require("nodejs-polars");

test("toRecords:date", () => {
	const df = pl
		.DataFrame({
			date: [new Date()],
		})
		.withColumn(pl.col("date").cast(pl.Date).alias("date"));

	const serializedDate = df.toRecords()[0].date;
  console.log(serializedDate)
	expect(serializedDate instanceof Date).toEqual(true);
});

additionally, using various versions of node & bun directly return the same result

Welcome to Node.js v22.1.0.
Type ".help" for more information.
> const pl = require("nodejs-polars");
undefined
> const df = pl.DataFrame({
...     date: [new Date("2020-01-01"), new Date("2020-01-02")],
... });
undefined
> df.toRecords()[0].date instanceof Date

@davidgovea
Copy link
Contributor Author

Wow that's a weird one! 🤦 Thanks @universalmind303

Realizing that I only observed this in jest tests as well, not in production. What is it doing under the hood?!

Again really appreciate the response here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants