1515import typing
1616
1717import pandas as pd
18+ import pyarrow as pa
1819import pytest
1920
2021from bigframes import bigquery
2122
23+ _TIMESTAMP_DTYPE = pd .ArrowDtype (pa .timestamp ("us" , tz = "UTC" ))
24+
25+
26+ @pytest .fixture
27+ def int_series (session ):
28+ pd_series = pd .Series ([1 , 2 , 3 , 4 , 5 ])
29+
30+ return session .read_pandas (pd_series ), pd_series
31+
2232
2333def test_unix_seconds (scalars_dfs ):
2434 bigframes_df , pandas_df = scalars_dfs
@@ -33,6 +43,19 @@ def test_unix_seconds(scalars_dfs):
3343 pd .testing .assert_series_equal (actual_res , expected_res )
3444
3545
46+ def test_unix_seconds_after_type_casting (int_series ):
47+ bf_series , pd_series = int_series
48+
49+ actual_res = bigquery .unix_seconds (bf_series .astype (_TIMESTAMP_DTYPE )).to_pandas ()
50+
51+ expected_res = (
52+ pd_series .astype (_TIMESTAMP_DTYPE )
53+ .apply (lambda ts : _to_unix_epoch (ts , "s" ))
54+ .astype ("Int64" )
55+ )
56+ pd .testing .assert_series_equal (actual_res , expected_res , check_index_type = False )
57+
58+
3659def test_unix_seconds_incorrect_input_type_raise_error (scalars_dfs ):
3760 df , _ = scalars_dfs
3861
@@ -53,6 +76,19 @@ def test_unix_millis(scalars_dfs):
5376 pd .testing .assert_series_equal (actual_res , expected_res )
5477
5578
79+ def test_unix_millis_after_type_casting (int_series ):
80+ bf_series , pd_series = int_series
81+
82+ actual_res = bigquery .unix_millis (bf_series .astype (_TIMESTAMP_DTYPE )).to_pandas ()
83+
84+ expected_res = (
85+ pd_series .astype (_TIMESTAMP_DTYPE )
86+ .apply (lambda ts : _to_unix_epoch (ts , "ms" ))
87+ .astype ("Int64" )
88+ )
89+ pd .testing .assert_series_equal (actual_res , expected_res , check_index_type = False )
90+
91+
5692def test_unix_millis_incorrect_input_type_raise_error (scalars_dfs ):
5793 df , _ = scalars_dfs
5894
@@ -73,6 +109,19 @@ def test_unix_micros(scalars_dfs):
73109 pd .testing .assert_series_equal (actual_res , expected_res )
74110
75111
112+ def test_unix_micros_after_type_casting (int_series ):
113+ bf_series , pd_series = int_series
114+
115+ actual_res = bigquery .unix_micros (bf_series .astype (_TIMESTAMP_DTYPE )).to_pandas ()
116+
117+ expected_res = (
118+ pd_series .astype (_TIMESTAMP_DTYPE )
119+ .apply (lambda ts : _to_unix_epoch (ts , "us" ))
120+ .astype ("Int64" )
121+ )
122+ pd .testing .assert_series_equal (actual_res , expected_res , check_index_type = False )
123+
124+
76125def test_unix_micros_incorrect_input_type_raise_error (scalars_dfs ):
77126 df , _ = scalars_dfs
78127
0 commit comments