如何获取今天与极地日期之间的天数?

问题描述 投票:0回答:1

我的 python 代码遇到了一些问题。我最初使用 pandas 编写它,但我需要更快一点的东西,所以我将其转换为极坐标。

使用

将 mongodb 读入极坐标数据帧后
race = pl.DataFrame(list(race_coll.find()))

并使用

将 'date_of_race' 列转换为 pl.Date 类型
race = race.with_columns(pl.col('date_of_race').str.strptime(pl.Date, format='%d %m %Y').cast(pl.Date))

有效的 pandas 代码是

days_between = (pd.to_datetime('today') - race.date_of_race.values[0]) // np.timedelta64(1,'D')

我尝试过以下方法:

date = pl.DataFrame({"date_of_race": [1], "value": race['date_of_race']})
days_between = (pd.to_datetime('today').normalize() - days_between[0][0]) // np.timedelta64(1,'D')

TypeError: 'int' object is not subscriptable
days_between = (pd.to_datetime('today').normalize() - race['date_of_race']) // np.timedelta64(1,'D')

PanicException: cannot coerce datatypes: ComputeError(ErrString("failed to determine supertype of object and date"))

当我打印日期时,我得到以下信息:

pandas:
print(race.date_of_race.values[0])

2022-10-15T00:00:00.000000000


polars:
print(race['date_of_race'])

shape: (1,)
Series: 'date_of_race' [date]
[
    2022-10-15
]

如有任何帮助,我们将不胜感激

python datetime duration python-polars
1个回答
0
投票

使用 Python 日期时间对象作为参考日期,并

.dt.days()
获取天数差异。例如:

import polars as pl
import pandas as pd

s = pl.Series([
    "2022-10-30T00:00:00",
    "2022-10-30T01:00:00",
    "2022-10-30T02:00:00",
    "2022-10-30T03:00:00",
    "2022-10-30T04:00:00",
    "2022-10-30T05:00:00",
]).str.strptime(pl.Datetime)

diff = pd.to_datetime('today').normalize().to_pydatetime() - s

print(diff)
# Series: '' [duration[μs]]
# [
#   299d
#   298d 23h
#   298d 22h
#   298d 21h
#   298d 20h
#   298d 19h
# ]

print(diff.dt.days())
# Series: '' [i64]
# [
#   299
#   298
#   298
#   298
#   298
#   298
# ]
© www.soinside.com 2019 - 2024. All rights reserved.