Skip to content

Commit 80c28ff

Browse files
authored
chore: Move _is_naive_format helper function into utils.py (#2568)
chore: Move _is_naive_format helper function into utils.py
1 parent 8d3de06 commit 80c28ff

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

narwhals/_ibis/expr_str.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from ibis.expr.datatypes import Timestamp
88

9+
from narwhals.utils import _is_naive_format
910
from narwhals.utils import not_implemented
1011

1112
if TYPE_CHECKING:
@@ -103,7 +104,3 @@ def to_datetime(self, format: str | None) -> IbisExpr:
103104
return self._compliant_expr._with_callable(fn(format))
104105

105106
replace = not_implemented()
106-
107-
108-
def _is_naive_format(format_: str) -> bool:
109-
return not any(x in format_ for x in ("%s", "%z", "Z"))

narwhals/_spark_like/expr_str.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from functools import partial
44
from typing import TYPE_CHECKING
55

6+
from narwhals.utils import _is_naive_format
7+
68
if TYPE_CHECKING:
79
from sqlframe.base.column import Column
810

@@ -100,7 +102,7 @@ def to_datetime(self, format: str | None) -> SparkLikeExpr:
100102
F = self._compliant_expr._F # noqa: N806
101103
if not format:
102104
function = F.to_timestamp
103-
elif is_naive_format(format):
105+
elif _is_naive_format(format):
104106
function = partial(
105107
F.to_timestamp_ntz, format=F.lit(strptime_to_pyspark_format(format))
106108
)
@@ -112,10 +114,6 @@ def to_datetime(self, format: str | None) -> SparkLikeExpr:
112114
)
113115

114116

115-
def is_naive_format(format: str) -> bool:
116-
return not any(x in format for x in ("%s", "%z", "Z"))
117-
118-
119117
def strptime_to_pyspark_format(format: str) -> str:
120118
"""Converts a Python strptime datetime format string to a PySpark datetime format string."""
121119
# Mapping from Python strptime format to PySpark format

narwhals/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,24 @@ def unstable(fn: _Fn, /) -> _Fn:
17451745
return fn
17461746

17471747

1748+
def _is_naive_format(format: str) -> bool:
1749+
"""Determines if a datetime format string is 'naive', i.e., does not include timezone information.
1750+
1751+
A format is considered naive if it does not contain any of the following
1752+
1753+
- '%s': Unix timestamp
1754+
- '%z': UTC offset
1755+
- 'Z' : UTC timezone designator
1756+
1757+
Arguments:
1758+
format: The datetime format string to check.
1759+
1760+
Returns:
1761+
bool: True if the format is naive (does not include timezone info), False otherwise.
1762+
"""
1763+
return not any(x in format for x in ("%s", "%z", "Z"))
1764+
1765+
17481766
if TYPE_CHECKING:
17491767
import sys
17501768

0 commit comments

Comments
 (0)