Skip to content

Commit c390da1

Browse files
fix: Fix internal type errors with temporal accessors (#2125)
1 parent 4607f86 commit c390da1

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

bigframes/operations/datetimes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
from bigframes import dataframe, dtypes, series
2525
from bigframes.core import log_adapter
26-
from bigframes.core.reshape import concat
2726
import bigframes.operations as ops
2827
import bigframes.operations.base
2928

@@ -79,13 +78,12 @@ def month(self) -> series.Series:
7978
return self._apply_unary_op(ops.month_op)
8079

8180
def isocalendar(self) -> dataframe.DataFrame:
82-
years = self._apply_unary_op(ops.iso_year_op)
83-
weeks = self._apply_unary_op(ops.iso_week_op)
84-
days = self._apply_unary_op(ops.iso_day_op)
85-
86-
result = concat.concat([years, weeks, days], axis=1)
87-
result.columns = pandas.Index(["year", "week", "day"])
88-
return result
81+
iso_ops = [ops.iso_year_op, ops.iso_week_op, ops.iso_day_op]
82+
labels = pandas.Index(["year", "week", "day"])
83+
block = self._block.project_exprs(
84+
[op.as_expr(self._value_column) for op in iso_ops], labels, drop=True
85+
)
86+
return dataframe.DataFrame(block)
8987

9088
# Time accessors
9189
@property

tests/system/small/operations/test_dates.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import datetime
1717

18+
from packaging import version
1819
import pandas as pd
1920
import pandas.testing
21+
import pytest
2022

2123
from bigframes import dtypes
2224

@@ -71,3 +73,19 @@ def test_date_series_diff_agg(scalars_dfs):
7173
pandas.testing.assert_series_equal(
7274
actual_result, expected_result, check_index_type=False
7375
)
76+
77+
78+
def test_date_can_cast_after_accessor(scalars_dfs):
79+
if version.Version(pd.__version__) <= version.Version("2.1.0"):
80+
pytest.skip("pd timezone conversion bug")
81+
bf_df, pd_df = scalars_dfs
82+
83+
actual_result = bf_df["date_col"].dt.isocalendar().week.astype("Int64").to_pandas()
84+
# convert to pd date type rather than arrow, as pandas doesn't handle arrow date well here
85+
expected_result = (
86+
pd.to_datetime(pd_df["date_col"]).dt.isocalendar().week.astype("Int64")
87+
)
88+
89+
pandas.testing.assert_series_equal(
90+
actual_result, expected_result, check_dtype=False, check_index_type=False
91+
)

third_party/bigframes_vendored/ibis/expr/operations/strings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ class ExtractFragment(ExtractURLField):
364364
class StringLength(StringUnary):
365365
"""Compute the length of a string."""
366366

367-
dtype = dt.int32
367+
dtype = dt.int64
368368

369369

370370
@public
371371
class StringAscii(StringUnary):
372372
"""Compute the ASCII code of the first character of a string."""
373373

374-
dtype = dt.int32
374+
dtype = dt.int64
375375

376376

377377
@public

third_party/bigframes_vendored/ibis/expr/operations/temporal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ExtractTemporalField(Unary):
105105
"""Extract a field from a temporal value."""
106106

107107
arg: Value[dt.Temporal]
108-
dtype = dt.int32
108+
dtype = dt.int64
109109

110110

111111
@public

0 commit comments

Comments
 (0)