解析CSV中的日期时间,分配时区并转换为另一个时区 – Polars Python

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

Parse datetime from CSV, assign timezone and convert to another timezone - Polars Python

问题

以下是您要翻译的内容:

I have a column of timestamps in a CSV file, like 2022-01-03 17:59:16.254. As an external information, I know this time is in JST.

I am trying to parse this string into datetime, assign JST timezone (without changing the timestamp), and convert it to CET.

An attempt:

new = pl.scan_csv('test.csv').with_columns(
    [pl.col("timestamp").str.strptime(pl.Datetime, "%Y-%m-%d %H:%M:%S.%f", strict=True),
    ]
).select(
    [pl.col("timestamp").cast(pl.Date).alias("Date"),
     pl.col("timestamp").dt.with_time_zone("Asia/Tokyo").alias("WithTZ"),
     pl.col("timestamp").dt.with_time_zone("Asia/Tokyo").dt.cast_time_zone("Europe/Berlin").alias("WithCastTZ"),
     pl.all(),
    ]
)

new.fetch(10).write_csv("testOut.csv")

as a result, I was expecting the datetime part to not change in WithTZ. However, this is my first line. Casting also did not have any impact.

WithTZ                     |WithCastTZ                 |timestamp
2022-01-04 02:59:16.213 JST|2022-01-04 02:59:16.213 CET|2022-01-03T17:59:16.213000000

I think I am missing something obvious...

英文:

I have a column of timestamps in a CSV file, like 2022-01-03 17:59:16.254. As an external information, I know this time is in JST.

I am trying to parse this string into datetime, assign JST timezone (without changing the timestamp), and convert it to CET.

An attempt:

new = pl.scan_csv('test.csv').with_columns(
    [pl.col("timestamp").str.strptime(pl.Datetime, "%Y-%m-%d %H:%M:%S.%f", strict=True),
    ]
).select(
    [pl.col("timestamp").cast(pl.Date).alias("Date"),
     pl.col("timestamp").dt.with_time_zone("Asia/Tokyo").alias("WithTZ"),
     pl.col("timestamp").dt.with_time_zone("Asia/Tokyo").dt.cast_time_zone("Europe/Berlin").alias("WithCastTZ"),
     pl.all(),
    ]
)

new.fetch(10).write_csv("testOut.csv")

as a result, I was expecting the datetime part to not change in WithTZ. However, this is my first line. Casting also did not have any impact.

WithTZ                     |WithCastTZ                 |timestamp
2022-01-04 02:59:16.213 JST|2022-01-04 02:59:16.213 CET|2022-01-03T17:59:16.213000000

I think I am missing something obvious..

答案1

得分: 1

以下是已翻译的内容:

  • dt.convert_time_zone: 从一个时区转换到另一个时区;
  • dt.replace_time_zone: 设置/取消/更改时区;

所以在这里,听起来你想要后者:

pl.col("timestamp").dt.replace_time_zone("Asia/Tokyo")

然后转换为 Europe/Berlin:

pl.col("timestamp").dt.replace_time_zone("Asia/Tokyo").dt.convert_time_zone("Europe/Berlin")
英文:

The methods for dealing with time zones are:

  • dt.convert_time_zone: convert from one time zone to another;
  • dt.replace_time_zone: set/unset/change time zone;

So here, it sounds like you're after the latter:

pl.col("timestamp").dt.replace_time_zone("Asia/Tokyo")

To then convert to Europe/Berlin:

pl.col("timestamp").dt.replace_time_zone("Asia/Tokyo").dt.convert_time_zone("Europe/Berlin")

huangapple
  • 本文由 发表于 2023年2月10日 05:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404668.html
匿名

发表评论

匿名网友

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

确定