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 67
Expand file tree
/
Copy pathtest_dates.py
More file actions
91 lines (67 loc) · 2.93 KB
/
test_dates.py
File metadata and controls
91 lines (67 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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 datetime
from packaging import version
import pandas as pd
import pandas.testing
import pytest
from bigframes import dtypes
def test_date_diff_between_series(session):
pd_df = pd.DataFrame(
{
"col_1": [datetime.date(2025, 1, 2), datetime.date(2025, 2, 1)],
"col_2": [datetime.date(2024, 1, 2), datetime.date(2026, 1, 30)],
}
).astype(dtypes.DATE_DTYPE)
bf_df = session.read_pandas(pd_df)
actual_result = (bf_df["col_1"] - bf_df["col_2"]).to_pandas()
expected_result = (pd_df["col_1"] - pd_df["col_2"]).astype(dtypes.TIMEDELTA_DTYPE)
pandas.testing.assert_series_equal(
actual_result, expected_result, check_index_type=False
)
def test_date_diff_literal_sub_series(scalars_dfs):
bf_df, pd_df = scalars_dfs
literal = datetime.date(2030, 5, 20)
actual_result = (literal - bf_df["date_col"]).to_pandas()
expected_result = (literal - pd_df["date_col"]).astype(dtypes.TIMEDELTA_DTYPE)
pandas.testing.assert_series_equal(
actual_result, expected_result, check_index_type=False
)
def test_date_diff_series_sub_literal(scalars_dfs):
bf_df, pd_df = scalars_dfs
literal = datetime.date(1980, 5, 20)
actual_result = (bf_df["date_col"] - literal).to_pandas()
expected_result = (pd_df["date_col"] - literal).astype(dtypes.TIMEDELTA_DTYPE)
pandas.testing.assert_series_equal(
actual_result, expected_result, check_index_type=False
)
def test_date_series_diff_agg(scalars_dfs):
bf_df, pd_df = scalars_dfs
actual_result = bf_df["date_col"].diff().to_pandas()
expected_result = pd_df["date_col"].diff().astype(dtypes.TIMEDELTA_DTYPE)
pandas.testing.assert_series_equal(
actual_result, expected_result, check_index_type=False
)
def test_date_can_cast_after_accessor(scalars_dfs):
if version.Version(pd.__version__) <= version.Version("2.1.0"):
pytest.skip("pd timezone conversion bug")
bf_df, pd_df = scalars_dfs
actual_result = bf_df["date_col"].dt.isocalendar().week.astype("Int64").to_pandas()
# convert to pd date type rather than arrow, as pandas doesn't handle arrow date well here
expected_result = (
pd.to_datetime(pd_df["date_col"]).dt.isocalendar().week.astype("Int64")
)
pandas.testing.assert_series_equal(
actual_result, expected_result, check_dtype=False, check_index_type=False
)