Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add day_name and month_name #55

Merged
merged 2 commits into from
Jan 12, 2024
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polars_xdt"
version = "0.9.0"
version = "0.10.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 2 additions & 0 deletions docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ API
polars_xdt.workday_count
polars_xdt.ExprXDTNamespace.base_utc_offset
polars_xdt.ExprXDTNamespace.ceil
polars_xdt.ExprXDTNamespace.day_name
polars_xdt.ExprXDTNamespace.dst_offset
polars_xdt.ExprXDTNamespace.format_localized
polars_xdt.ExprXDTNamespace.from_local_datetime
polars_xdt.ExprXDTNamespace.is_workday
polars_xdt.ExprXDTNamespace.month_name
polars_xdt.ExprXDTNamespace.offset_by
polars_xdt.ExprXDTNamespace.to_local_datetime
polars_xdt.ExprXDTNamespace.to_julian_date
86 changes: 86 additions & 0 deletions polars_xdt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,92 @@ def dst_offset(self) -> XDTExpr:
)
return cast(XDTExpr, result)

def day_name(self, locale: str | None = None) -> XDTExpr:
"""
Return day name, in specified locale (if specified).

Returns
-------
Expr
Expression of data type :class:`Utf8`.

See Also
--------
format_localized : format according to locale.

Examples
--------
>>> from datetime import datetime
>>> import polars_xdt # noqa: F401
>>> df = pl.DataFrame(
... {
... "ts": [datetime(2020, 10, 25), datetime(2020, 10, 26)],
... }
... )
>>> df.with_columns(
... english_day_name=pl.col("ts").xdt.day_name(),
... french_day_name=pl.col("ts").xdt.day_name("fr_FR"),
... ukrainian_day_name=pl.col("ts").xdt.day_name("uk_UA"),
... )
shape: (2, 4)
┌─────────────────────┬──────────────────┬─────────────────┬────────────────────┐
│ ts ┆ english_day_name ┆ french_day_name ┆ ukrainian_day_name │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ str ┆ str ┆ str │
╞═════════════════════╪══════════════════╪═════════════════╪════════════════════╡
│ 2020-10-25 00:00:00 ┆ Sunday ┆ dimanche ┆ неділя │
│ 2020-10-26 00:00:00 ┆ Monday ┆ lundi ┆ понеділок │
└─────────────────────┴──────────────────┴─────────────────┴────────────────────┘
"""
if locale is None:
result = self._expr.dt.to_string("%A")
else:
result = self._expr.xdt.format_localized("%A", locale=locale) # type: ignore[attr-defined]
return cast(XDTExpr, result)

def month_name(self, locale: str | None = None) -> XDTExpr:
"""
Return month name, in specified locale (if specified).

Returns
-------
Expr
Expression of data type :class:`Utf8`.

See Also
--------
format_localized : format according to locale.

Examples
--------
>>> from datetime import datetime
>>> import polars_xdt # noqa: F401
>>> df = pl.DataFrame(
... {
... "ts": [datetime(2020, 10, 25), datetime(2020, 11, 26)],
... }
... )
>>> df.with_columns(
... english_month_name=pl.col("ts").xdt.month_name(),
... french_month_name=pl.col("ts").xdt.month_name("fr_FR"),
... ukrainian_month_name=pl.col("ts").xdt.month_name("uk_UA"),
... )
shape: (2, 4)
┌─────────────────────┬────────────────────┬───────────────────┬──────────────────────┐
│ ts ┆ english_month_name ┆ french_month_name ┆ ukrainian_month_name │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ str ┆ str ┆ str │
╞═════════════════════╪════════════════════╪═══════════════════╪══════════════════════╡
│ 2020-10-25 00:00:00 ┆ October ┆ octobre ┆ жовтня │
│ 2020-11-26 00:00:00 ┆ November ┆ novembre ┆ листопада │
└─────────────────────┴────────────────────┴───────────────────┴──────────────────────┘
"""
if locale is None:
result = self._expr.dt.to_string("%B")
else:
result = self._expr.xdt.format_localized("%B", locale=locale) # type: ignore[attr-defined]
return cast(XDTExpr, result)


class XDTExpr(pl.Expr):
@property
Expand Down