Code: Select all
In [1]: import polars
In [2]: polars.__version__
Out[2]: '0.16.9'
In [3]: df = (
...: polars.DataFrame(
...: data={
...: "timestamp": ["1970-01-01 00:00:00+01:00", "1970-01-01 01:00:00+01:00"],
...: "value": [1, 1],
...: }
...: )
...: .with_columns(
...: polars.col("timestamp").str.strptime(
...: polars.Datetime, fmt="%Y-%m-%d %H:%M:%S%:z"
...: )
...: )
...: .with_columns(
...: polars.col("timestamp").dt.convert_time_zone("UTC").alias("timestamp_utc")
...: )
...: )
In [4]: df
Out[4]:
shape: (2, 3)
┌────────────────────────────┬───────┬─────────────────────────┐
│ timestamp ┆ value ┆ timestamp_utc │
│ --- ┆ --- ┆ --- │
│ datetime[μs, +01:00] ┆ i64 ┆ datetime[μs, UTC] │
╞════════════════════════════╪═══════╪═════════════════════════╡
│ 1970-01-01 00:00:00 +01:00 ┆ 1 ┆ 1969-12-31 23:00:00 UTC │
│ 1970-01-01 01:00:00 +01:00 ┆ 1 ┆ 1970-01-01 00:00:00 UTC │
└────────────────────────────┴───────┴─────────────────────────┘
In [5]: df.groupby_dynamic(
...: index_column="timestamp", every="1d", closed="left"
...: ).agg(polars.col("value").count())
Out[5]:
shape: (2, 2)
┌────────────────────────────┬───────┐
│ timestamp ┆ value │
│ --- ┆ --- │
│ datetime[μs, +01:00] ┆ u32 │
╞════════════════════════════╪═══════╡
│ 1969-12-31 01:00:00 +01:00 ┆ 1 │
│ 1970-01-01 01:00:00 +01:00 ┆ 1 │
└────────────────────────────┴───────┘
In [6]: df.groupby_dynamic(
...: index_column="timestamp_utc", every="1d", closed="left"
...: ).agg(polars.col("value").count())
Out[6]:
shape: (2, 2)
┌─────────────────────────┬───────┐
│ timestamp_utc ┆ value │
│ --- ┆ --- │
│ datetime[μs, UTC] ┆ u32 │
╞═════════════════════════╪═══════╡
│ 1969-12-31 00:00:00 UTC ┆ 1 │
│ 1970-01-01 00:00:00 UTC ┆ 1 │
└─────────────────────────┴───────┘
Bei der erneuten Abtastung nach Tag ist die Zählung zwar korrekt, wenn es um die UTC-Zeitzone geht (Spalte timestamp_utc), aber ich denke, dass die mit der Zeitstempelspalte nicht so ist, wie sie die beiden Zeilen zum 01.01.1970 hätte aggregieren sollen 00:00:00+01:00.
Verstehe ich etwas falsch?
Vielen Dank im Voraus!
Mobile version