Skip to content

Commit 43d206c

Browse files
authored
enh: support replace_time_zone(None) (#2576)
* enh: support replace_time_zone(None) * fix typo
1 parent ede1bd3 commit 43d206c

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

narwhals/_duckdb/expr_dt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,13 @@ def truncate(self, every: str) -> DuckDBExpr:
120120
)
121121
)
122122

123+
def replace_time_zone(self, time_zone: str | None) -> DuckDBExpr:
124+
if time_zone is None:
125+
return self._compliant_expr._with_callable(
126+
lambda _input: _input.cast("timestamp")
127+
)
128+
else: # pragma: no cover
129+
msg = "`replace_time_zone` with non-null `time_zone` not yet implemented for duckdb"
130+
raise NotImplementedError(msg)
131+
123132
total_nanoseconds = not_implemented()

narwhals/_ibis/expr_dt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ def truncate(self, every: str) -> IbisExpr:
8585
fn = self._truncate(UNITS_DICT_TRUNCATE[unit])
8686
return self._compliant_expr._with_callable(fn)
8787

88+
def replace_time_zone(self, time_zone: str | None) -> IbisExpr:
89+
if time_zone is None:
90+
return self._compliant_expr._with_callable(
91+
lambda _input: _input.cast("timestamp")
92+
)
93+
else: # pragma: no cover
94+
msg = "`replace_time_zone` with non-null `time_zone` not yet implemented for Ibis"
95+
raise NotImplementedError(msg)
96+
8897
nanosecond = not_implemented()
8998
total_minutes = not_implemented()
9099
total_seconds = not_implemented()

narwhals/_spark_like/expr_dt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,12 @@ def _truncate(expr: Column) -> Column:
8080
return self._compliant_expr._F.date_trunc(format, expr)
8181

8282
return self._compliant_expr._with_callable(_truncate)
83+
84+
def replace_time_zone(self, time_zone: str | None) -> SparkLikeExpr:
85+
if time_zone is None:
86+
return self._compliant_expr._with_callable(
87+
lambda _input: _input.cast("timestamp_ntz")
88+
)
89+
else: # pragma: no cover
90+
msg = "`replace_time_zone` with non-null `time_zone` not yet implemented for spark-like"
91+
raise NotImplementedError(msg)

tests/expr_and_series/dt/replace_time_zone_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def test_replace_time_zone_none(
5656
or ("pyarrow_table" in str(constructor) and PYARROW_VERSION < (12,))
5757
):
5858
pytest.skip()
59-
if any(x in str(constructor) for x in ("duckdb", "pyspark", "ibis")):
59+
if any(x in str(constructor) for x in ("pyspark",)):
60+
# pyspark: needs `to_string`
6061
request.applymarker(pytest.mark.xfail)
6162
data = {
6263
"a": [

0 commit comments

Comments
 (0)