This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add unix_seconds, unix_millis and unix_micros for timestamp series. #1297
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2643582
feat: add UNIX_SECONDS, UNIX_MILLIS and UNIX_MICROS functions for tim…
sycai 2a9661e
add docstring
sycai 0d662a0
fix format
sycai 3efd605
fix mypy error
sycai 5ac8030
Merge branch 'main' into b388906290-unix-epoch
sycai 2efc7ec
fix type error
sycai 47c6408
Merge branch 'main' into b388906290-unix-epoch
sycai 5db5340
Merge branch 'main' into b388906290-unix-epoch
sycai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from bigframes import operations as ops | ||
| from bigframes import series | ||
|
|
||
|
|
||
| def unix_seconds(input: series.Series) -> series.Series: | ||
| """Converts a timestmap series to unix epoch seconds | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import pandas as pd | ||
| >>> import bigframes.pandas as bpd | ||
| >>> import bigframes.bigquery as bbq | ||
| >>> bpd.options.display.progress_bar = None | ||
|
|
||
| >>> s = bpd.Series([pd.Timestamp("1970-01-02", tz="UTC"), pd.Timestamp("1970-01-03", tz="UTC")]) | ||
| >>> bbq.unix_seconds(s) | ||
| 0 86400 | ||
| 1 172800 | ||
| dtype: Int64 | ||
|
|
||
| Args: | ||
| input (bigframes.pandas.Series): | ||
| A timestamp series. | ||
|
|
||
| Returns: | ||
| bigframes.pandas.Series: A new series of unix epoch in seconds. | ||
|
|
||
| """ | ||
| return input._apply_unary_op(ops.UnixSeconds()) | ||
|
|
||
|
|
||
| def unix_millis(input: series.Series) -> series.Series: | ||
| """Converts a timestmap series to unix epoch milliseconds | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import pandas as pd | ||
| >>> import bigframes.pandas as bpd | ||
| >>> import bigframes.bigquery as bbq | ||
| >>> bpd.options.display.progress_bar = None | ||
|
|
||
| >>> s = bpd.Series([pd.Timestamp("1970-01-02", tz="UTC"), pd.Timestamp("1970-01-03", tz="UTC")]) | ||
| >>> bbq.unix_millis(s) | ||
| 0 86400000 | ||
| 1 172800000 | ||
| dtype: Int64 | ||
|
|
||
| Args: | ||
| input (bigframes.pandas.Series): | ||
| A timestamp series. | ||
|
|
||
| Returns: | ||
| bigframes.pandas.Series: A new series of unix epoch in milliseconds. | ||
|
|
||
| """ | ||
| return input._apply_unary_op(ops.UnixMillis()) | ||
|
|
||
|
|
||
| def unix_micros(input: series.Series) -> series.Series: | ||
| """Converts a timestmap series to unix epoch microseconds | ||
|
|
||
| **Examples:** | ||
|
|
||
| >>> import pandas as pd | ||
| >>> import bigframes.pandas as bpd | ||
| >>> import bigframes.bigquery as bbq | ||
| >>> bpd.options.display.progress_bar = None | ||
|
|
||
| >>> s = bpd.Series([pd.Timestamp("1970-01-02", tz="UTC"), pd.Timestamp("1970-01-03", tz="UTC")]) | ||
| >>> bbq.unix_micros(s) | ||
| 0 86400000000 | ||
| 1 172800000000 | ||
| dtype: Int64 | ||
|
|
||
| Args: | ||
| input (bigframes.pandas.Series): | ||
| A timestamp series. | ||
|
|
||
| Returns: | ||
| bigframes.pandas.Series: A new series of unix epoch in microseconds. | ||
|
|
||
| """ | ||
| return input._apply_unary_op(ops.UnixMicros()) |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import typing | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from bigframes import bigquery | ||
|
|
||
|
|
||
| def test_unix_seconds(scalars_dfs): | ||
| bigframes_df, pandas_df = scalars_dfs | ||
|
|
||
| actual_res = bigquery.unix_seconds(bigframes_df["timestamp_col"]).to_pandas() | ||
|
|
||
| expected_res = ( | ||
| pandas_df["timestamp_col"] | ||
| .apply(lambda ts: _to_unix_epoch(ts, "s")) | ||
| .astype("Int64") | ||
| ) | ||
| pd.testing.assert_series_equal(actual_res, expected_res) | ||
|
|
||
|
|
||
| def test_unix_millis(scalars_dfs): | ||
| bigframes_df, pandas_df = scalars_dfs | ||
|
|
||
| actual_res = bigquery.unix_millis(bigframes_df["timestamp_col"]).to_pandas() | ||
|
|
||
| expected_res = ( | ||
| pandas_df["timestamp_col"] | ||
| .apply(lambda ts: _to_unix_epoch(ts, "ms")) | ||
| .astype("Int64") | ||
| ) | ||
| pd.testing.assert_series_equal(actual_res, expected_res) | ||
|
|
||
|
|
||
| def test_unix_micros(scalars_dfs): | ||
| bigframes_df, pandas_df = scalars_dfs | ||
|
|
||
| actual_res = bigquery.unix_micros(bigframes_df["timestamp_col"]).to_pandas() | ||
|
|
||
| expected_res = ( | ||
| pandas_df["timestamp_col"] | ||
| .apply(lambda ts: _to_unix_epoch(ts, "us")) | ||
| .astype("Int64") | ||
| ) | ||
| pd.testing.assert_series_equal(actual_res, expected_res) | ||
|
|
||
|
|
||
| def _to_unix_epoch( | ||
| ts: pd.Timestamp, unit: typing.Literal["s", "ms", "us"] | ||
| ) -> typing.Optional[int]: | ||
| if pd.isna(ts): | ||
| return None | ||
| return (ts - pd.Timestamp("1970-01-01", tz="UTC")) // pd.Timedelta(1, unit) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we support these via
sql_scalaradded in #1293?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sql_scalar is cool, though I think well-defined operations such as epoch conversions should have their own op nodes because their behaviors are well-defined, and there's no need for dry runs to validate the syntax and return type. Plus, it might benefit from future tree optimizations.