Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions bigframes/operations/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from bigframes import dataframe, dtypes, series
from bigframes.core import log_adapter
from bigframes.core.reshape import concat
import bigframes.operations as ops
import bigframes.operations.base

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

def isocalendar(self) -> dataframe.DataFrame:
years = self._apply_unary_op(ops.iso_year_op)
weeks = self._apply_unary_op(ops.iso_week_op)
days = self._apply_unary_op(ops.iso_day_op)

result = concat.concat([years, weeks, days], axis=1)
result.columns = pandas.Index(["year", "week", "day"])
return result
iso_ops = [ops.iso_year_op, ops.iso_week_op, ops.iso_day_op]
labels = pandas.Index(["year", "week", "day"])
block = self._block.project_exprs(
[op.as_expr(self._value_column) for op in iso_ops], labels, drop=True
)
return dataframe.DataFrame(block)

# Time accessors
@property
Expand Down
18 changes: 18 additions & 0 deletions tests/system/small/operations/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import datetime

from packaging import version
import pandas as pd
import pandas.testing
import pytest

from bigframes import dtypes

Expand Down Expand Up @@ -71,3 +73,19 @@ def test_date_series_diff_agg(scalars_dfs):
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
)
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,14 @@ class ExtractFragment(ExtractURLField):
class StringLength(StringUnary):
"""Compute the length of a string."""

dtype = dt.int32
dtype = dt.int64


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

dtype = dt.int32
dtype = dt.int64


@public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ExtractTemporalField(Unary):
"""Extract a field from a temporal value."""

arg: Value[dt.Temporal]
dtype = dt.int32
dtype = dt.int64


@public
Expand Down