【问题标题】:Is there a way to convert a pl.Series of type pl.Datetime from one timezone to another?有没有办法将 pl.Datetime 类型的 pl.Series 从一个时区转换为另一个时区?
【发布时间】:2022-10-23 16:02:12
【问题描述】:

我正在阅读带有polars 的镶木地板文件,并希望将名为datetime 的列从datetime[ms, America/New_York] 类型转换为datetime[ns,UTC]。 我可以将列取出并在pandas 中执行,使用tz_convert 并将列添加回polars 数据框,但如果有办法在polars 中执行它会很好:)

【问题讨论】:

  • 您还想根据时区差异改变时间还是只设置不同的时区?
  • 我还需要改变时间:例如2021-05-04 19:00:00 EDT 应该变成2021-05-04 23:00:00 UTC。从文档中我看到可以使用pl.col("timezone").dt.with_time_zone 更改时区,但我想要做的是不同的。此外,在时区感知列上调用 dt.epoch() 会返回该时区中的 unix 纪元(我认为它应该始终是 UTC 时代)
  • * pl.col("datetime").dt.with_time_zone

标签: python datetime python-polars


【解决方案1】:

截至polars 0.14.14有:

pl.col("datetime").dt.with_time_zone 设置时区而不修改底层时间戳。

pl.col("datetime").dt.cast_time_zone 通过从当前时区更正给定时区来修改底层时间戳。

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 2015-02-21
    • 1970-01-01
    • 2012-01-30
    • 2015-05-06
    • 2021-01-23
    • 2015-03-29
    • 2013-12-03
    相关资源
    最近更新 更多