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
2 changes: 1 addition & 1 deletion narwhals/_arrow/series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def unit(self) -> TimeUnit: # NOTE: Unsafe (native).
def time_zone(self) -> str | None: # NOTE: Unsafe (narwhals).
return cast("Datetime", self.compliant.dtype).time_zone

def to_string(self: Self, format: str) -> ArrowSeries: # noqa: A002
def to_string(self: Self, format: str) -> ArrowSeries:
# PyArrow differs from other libraries in that %S also prints out
# the fractional part of the second...:'(
# https://arrow.apache.org/docs/python/generated/pyarrow.compute.strftime.html
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_arrow/series_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def split(self: Self, by: str) -> ArrowSeries:
split_series = pc.split_pattern(self.native, by) # type: ignore[call-overload]
return self.from_native(split_series)

def to_datetime(self: Self, format: str | None) -> ArrowSeries: # noqa: A002
def to_datetime(self: Self, format: str | None) -> ArrowSeries:
format = parse_datetime_format(self.native) if format is None else format
strptime: Incomplete = pc.strptime
timestamp_array: pa.Array[pa.TimestampScalar[Any, Any]] = strptime(
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_compliant/any_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_categories(self) -> CompliantT_co: ...


class DateTimeNamespace(_StoresCompliant[CompliantT_co], Protocol[CompliantT_co]):
def to_string(self, format: str) -> CompliantT_co: ... # noqa: A002
def to_string(self, format: str) -> CompliantT_co: ...
def replace_time_zone(self, time_zone: str | None) -> CompliantT_co: ...
def convert_time_zone(self, time_zone: str) -> CompliantT_co: ...
def timestamp(self, time_unit: TimeUnit) -> CompliantT_co: ...
Expand Down Expand Up @@ -78,7 +78,7 @@ def ends_with(self, suffix: str) -> CompliantT_co: ...
def contains(self, pattern: str, *, literal: bool) -> CompliantT_co: ...
def slice(self, offset: int, length: int | None) -> CompliantT_co: ...
def split(self, by: str) -> CompliantT_co: ...
def to_datetime(self, format: str | None) -> CompliantT_co: ... # noqa: A002
def to_datetime(self, format: str | None) -> CompliantT_co: ...
def to_lowercase(self) -> CompliantT_co: ...
def to_uppercase(self) -> CompliantT_co: ...

Expand Down
4 changes: 2 additions & 2 deletions narwhals/_compliant/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def get_categories(self) -> EagerExprT:
class EagerExprDateTimeNamespace(
EagerExprNamespace[EagerExprT], DateTimeNamespace[EagerExprT], Generic[EagerExprT]
):
def to_string(self, format: str) -> EagerExprT: # noqa: A002
def to_string(self, format: str) -> EagerExprT:
return self.compliant._reuse_series_namespace("dt", "to_string", format=format)

def replace_time_zone(self, time_zone: str | None) -> EagerExprT:
Expand Down Expand Up @@ -990,7 +990,7 @@ def slice(self, offset: int, length: int | None) -> EagerExprT:
def split(self, by: str) -> EagerExprT:
return self.compliant._reuse_series_namespace("str", "split", by=by)

def to_datetime(self, format: str | None) -> EagerExprT: # noqa: A002
def to_datetime(self, format: str | None) -> EagerExprT:
return self.compliant._reuse_series_namespace("str", "to_datetime", format=format)

def to_lowercase(self) -> EagerExprT:
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_dask/expr_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def weekday(self: Self) -> DaskExpr:
"weekday",
)

def to_string(self: Self, format: str) -> DaskExpr: # noqa: A002
def to_string(self: Self, format: str) -> DaskExpr:
return self._compliant_expr._from_call(
lambda _input, format: _input.dt.strftime(format.replace("%.f", ".%f")), # noqa: A006
lambda _input, format: _input.dt.strftime(format.replace("%.f", ".%f")),
"strftime",
format=format,
)
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_dask/expr_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def split(self: Self, by: str) -> DaskExpr:
by=by,
)

def to_datetime(self: Self, format: str | None) -> DaskExpr: # noqa: A002
def to_datetime(self: Self, format: str | None) -> DaskExpr:
return self._compliant_expr._from_call(
lambda _input, format: dd.to_datetime(_input, format=format), # noqa: A006
lambda _input, format: dd.to_datetime(_input, format=format),
"to_datetime",
format=format,
)
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_duckdb/expr_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def nanosecond(self: Self) -> DuckDBExpr:
"nanosecond",
)

def to_string(self: Self, format: str) -> DuckDBExpr: # noqa: A002
def to_string(self: Self, format: str) -> DuckDBExpr:
return self._compliant_expr._from_call(
lambda _input: FunctionExpression("strftime", _input, lit(format)),
"to_string",
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_duckdb/expr_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def replace(self: Self, pattern: str, value: str, *, literal: bool, n: int) -> N
msg = "`replace` is currently not supported for DuckDB"
raise NotImplementedError(msg)

def to_datetime(self: Self, format: str | None) -> DuckDBExpr: # noqa: A002
def to_datetime(self: Self, format: str | None) -> DuckDBExpr:
if format is None:
msg = "Cannot infer format with DuckDB backend, please specify `format` explicitly."
raise NotImplementedError(msg)
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def total_nanoseconds(self: Self) -> PandasLikeSeries:
s_abs = s_abs.astype(int_dtype_mapper(s.dtype))
return self._compliant_series._from_native_series(s_abs * s_sign)

def to_string(self: Self, format: str) -> PandasLikeSeries: # noqa: A002
def to_string(self: Self, format: str) -> PandasLikeSeries:
# Polars' parser treats `'%.f'` as pandas does `'.%f'`
# PyArrow interprets `'%S'` as "seconds, plus fractional seconds"
# and doesn't support `%f`
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/series_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def split(self: Self, by: str) -> PandasLikeSeries:
self._compliant_series._native_series.str.split(pat=by),
)

def to_datetime(self: Self, format: str | None) -> PandasLikeSeries: # noqa: A002
def to_datetime(self: Self, format: str | None) -> PandasLikeSeries:
if format is not None and any(x in format for x in ("%z", "Z")):
# We know that the inputs are timezone-aware, so we can directly pass
# `utc=True` for better performance.
Expand Down
6 changes: 3 additions & 3 deletions narwhals/_spark_like/expr_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def to_lowercase(self: Self) -> SparkLikeExpr:
self._compliant_expr._F.lower, "to_lowercase"
)

def to_datetime(self: Self, format: str | None) -> SparkLikeExpr: # noqa: A002
def to_datetime(self: Self, format: str | None) -> SparkLikeExpr:
F = self._compliant_expr._F # noqa: N806
if not format:
function = F.to_timestamp
Expand All @@ -123,11 +123,11 @@ def to_datetime(self: Self, format: str | None) -> SparkLikeExpr: # noqa: A002
)


def is_naive_format(format: str) -> bool: # noqa: A002
def is_naive_format(format: str) -> bool:
return not any(x in format for x in ("%s", "%z", "Z"))


def strptime_to_pyspark_format(format: str) -> str: # noqa: A002
def strptime_to_pyspark_format(format: str) -> str:
"""Converts a Python strptime datetime format string to a PySpark datetime format string."""
# Mapping from Python strptime format to PySpark format

Expand Down
2 changes: 1 addition & 1 deletion narwhals/expr_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def total_nanoseconds(self: Self) -> ExprT:
self._expr._metadata,
)

def to_string(self: Self, format: str) -> ExprT: # noqa: A002
def to_string(self: Self, format: str) -> ExprT:
"""Convert a Date/Time/Datetime column into a String column with the given format.

Arguments:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/expr_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def tail(self: Self, n: int = 5) -> ExprT:
self._expr._metadata,
)

def to_datetime(self: Self, format: str | None = None) -> ExprT: # noqa: A002
def to_datetime(self: Self, format: str | None = None) -> ExprT:
"""Convert to Datetime dtype.

Notes:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/series_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def total_nanoseconds(self: Self) -> SeriesT:
self._narwhals_series._compliant_series.dt.total_nanoseconds()
)

def to_string(self: Self, format: str) -> SeriesT: # noqa: A002
def to_string(self: Self, format: str) -> SeriesT:
"""Convert a Date/Time/Datetime series into a String series with the given format.

Arguments:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/series_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def to_lowercase(self: Self) -> SeriesT:
self._narwhals_series._compliant_series.str.to_lowercase()
)

def to_datetime(self: Self, format: str | None = None) -> SeriesT: # noqa: A002
def to_datetime(self: Self, format: str | None = None) -> SeriesT:
"""Parse Series with strings to a Series with Datetime dtype.

Notes:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ ignore = [
"TD004", # missing-todo-colon
]

[tool.ruff.lint.flake8-builtins]
builtins-ignorelist = ["format"]

[tool.ruff.lint.per-file-ignores]
"narwhals/expr_dt.py" = ["RUF002"]
"tests/*" = ["S101"]
Expand Down
2 changes: 1 addition & 1 deletion tests/expr_and_series/str/to_datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_pyarrow_infer_datetime_raise_inconsistent_date_fmt(
def test_to_datetime_tz_aware(
constructor: Constructor,
request: pytest.FixtureRequest,
format: str | None, # noqa: A002
format: str | None,
) -> None:
if "pyarrow_table" in str(constructor) and PYARROW_VERSION < (13,):
# bugged
Expand Down
Loading