Last active
October 4, 2023 06:17
-
-
Save jason19970210/d5fb96ba4afe3df712a093676722532e to your computer and use it in GitHub Desktop.
Test timestamp & datetime convert with Polars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 │ | |
| └─────────────────────┘ | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 │ | |
| └───────────────┘ | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 │ | |
| └─────────────────────────┘ | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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