Skip to content

Instantly share code, notes, and snippets.

@jason19970210
Last active October 4, 2023 06:17
Show Gist options
  • Select an option

  • Save jason19970210/d5fb96ba4afe3df712a093676722532e to your computer and use it in GitHub Desktop.

Select an option

Save jason19970210/d5fb96ba4afe3df712a093676722532e to your computer and use it in GitHub Desktop.
Test timestamp & datetime convert with Polars
import polars as pl
df = pl.DataFrame({
"epoch_seconds": [1648457740, 1648457740 + 10]
})
# print(df)
df = df.with_columns(
pl.from_epoch("epoch_seconds", time_unit="s")
)
print(df)
"""
shape: (2, 1)
┌─────────────────────┐
│ epoch_seconds │
│ --- │
│ datetime[μs] │
╞═════════════════════╡
│ 2022-03-28 08:55:40 │
│ 2022-03-28 08:55:50 │
└─────────────────────┘
"""
import polars as pl
df = pl.DataFrame({
"epoch_seconds": [1648457740, 1648457740 + 10]
})
# print(df)
df = df.with_columns(
pl.from_epoch("epoch_seconds").cast(pl.Date)
)
print(df)
"""
shape: (2, 1)
┌───────────────┐
│ epoch_seconds │
│ --- │
│ date │
╞═══════════════╡
│ 2022-03-28 │
│ 2022-03-28 │
└───────────────┘
"""
import polars as pl
df = pl.DataFrame({
"epoch_seconds": [1648457740, 1648457740 + 10]
})
# print(df)
df = df.with_columns(
pl.from_epoch("epoch_seconds", time_unit="ms")
)
print(df)
"""
shape: (2, 1)
┌─────────────────────────┐
│ epoch_seconds │
│ --- │
│ datetime[ms] │
╞═════════════════════════╡
│ 1970-01-20 01:54:17.740 │
│ 1970-01-20 01:54:17.750 │
└─────────────────────────┘
"""
import polars as pl
df = pl.DataFrame({
"epoch_seconds": [1648457740, 1648457740 + 10]
})
# print(df)
df = df.with_columns(
pl.from_epoch("epoch_seconds", time_unit="d")
)
print(df)
"""
thread '<unnamed>' panicked at /home/runner/work/polars/polars/crates/nano-arrow/src/temporal_conversions.rs:40:30:
out-of-range date
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
File "/home/user/Desktop/Binance/test.py", line 13, in <module>
print(df)
File "/home/user/Desktop/Binance/venv/lib/python3.10/site-packages/polars/dataframe/frame.py", line 1502, in __str__
return self._df.as_str()
pyo3_runtime.PanicException: out-of-range date
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment