How can I use strftime
in nodejs-polars
to format dates?
Let’s assume I have a nodejs-polars wrapped date:
import pl from 'nodejs-polars';
const yesterday = new Date(Date.now());
yesterday.setDate(yesterday.getDate() - 1);
const startOfYesterday = pl.lit(yesterday).cast(pl.Datetime('ms'));
Then:
> startOfYesterday
2024-09-11 00:19:59.059.cast(Datetime(Milliseconds, None))
Unlike Polars for Python, nodejs-polars
does not map date helpers to dt
namespace on the expression object, but it goes to date namespace.
To format a date that is in the form of Polars expression Expr
you can do:
// Create Expr that uses strftime in date namespace
const formattedExpr = startOfYesterday.date.strftime("%Y-%m-%d");
// Runs the expression (there is no Series/DataFrame in this case
const data = pl.select(formattedExpr);
The resulting pl.select()
is an object with one row and one value:
Proxy [
shape: (1, 1)
┌────────────┐
│ literal │
│ --- │
│ str │
╞════════════╡
│ 2024-09-11 │
└────────────┘,
{
get: [Function: get],
set: [Function: set],
has: [Function: has],
ownKeys: [Function: ownKeys],
getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor]
}
]
From this you can extra the formatted date from the first cell of first row:
console.log(data[0][0])
Produces:
2024-09-11