Optimal use of datetime related objects in python-polars – should I be using pl.date or other alternatives?

huangapple go评论96阅读模式
英文:

Optimal use of datetime related objects in python-polars - should I be using pl.date or other alternatives?

问题

我正在使用许多相当大的极地数据框(1百万至2亿条记录),其中许多是使用日期作为时间索引。

澄清一下,我从不关心时区或日期的时间部分,所有数据都是按日记录的。

到目前为止,我一直在使用 pl.Date() 列类型,因为这似乎是最佳选择,但现在我开始怀疑自己。

自 Polars 1.6 起,不再可以将 pl.Date() 列与日期字符串(例如 '2020-01-01')进行比较。

所以我将重新设计大量代码,明确使用 np.datetime64 对象,或 pl.datepl.datetime 对象。

有没有任何理由更偏好其中一种?它们在我进行的 .filter 操作的基准测试中都相当可比。

英文:

I am working with many reasonably large polar dataframes (1m - 200m records) and many of these are time-indexed using dates.

To clarify, I never care about timezones or the time component of the date, all the data is daily.

So far I've been using the pl.Date() column type as this seems optimal, but now I am second guessing myself.

As of polars 1.6 it's no longer possible to compare pl.Date() columns to date strings (eg. '2020-01-01')

So I'll be refactoring a fair amount of my code to explicitly use either np.datetime64 objects, or pl.date or pl.datetime objects.

Is there any good reason to prefer one of these over another? They are all fairly comparable in the benchmarking I've done for .filter operations.

答案1

得分: 1

你可以直接与 datetime.date 进行比较 - pl.Date 应该没问题

例如:

import datetime as dt

df.filter(pl.col('date') > dt.date(2000, 1, 1))

而不是

df.filter(pl.col('date') > '2000-01-01')
英文:

You can just compare with datetime.date - pl.Date should be fine

e.g.

import datetime as dt

df.filter(pl.col('date') > dt.date(2000, 1, 1))

instead of

df.filter(pl.col('date') > '2000-01-01')

huangapple
  • 本文由 发表于 2023年2月6日 22:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定