Format values as datetimes.
GT.fmt_datetime(
columns=None,
rows=None,
date_style="iso",
time_style="iso",
format_str=None,
sep=" ",
pattern="{x}",
locale=None,
)
Format input values to datetime values using one of 41 preset date styles and one of 25 preset time styles, or with a format string. Input can be in the form of datetime values, or strings in the ISO 8601 forms of YYYY-MM-DD HH:MM:SS or YYYY-MM-DD.
Parameters
columns: SelectExpr = None
-
The columns to target. Can either be a single column name or a series of column names provided in a list.
rows: RowSelectExpr = None
-
In conjunction with columns=, we can specify which of their rows should undergo formatting. The default is all rows, resulting in all rows in targeted columns being formatted. Alternatively, we can supply a row index, a list of row indices, or (for Polars DataFrames) a Polars expression such as pl.col("x") > 0.
date_style: DateStyle = "iso"
-
The date style to use. By default this is the short name "iso" which corresponds to ISO 8601 date formatting. There are 41 date styles in total.
time_style: TimeStyle = "iso"
-
The time style to use. By default this is the short name "iso" which corresponds to how times are formatted within ISO 8601 datetime values. There are 25 time styles in total.
format_str: str | None = None
-
A format string to use instead of the preset styles. This can be a CLDR datetime pattern (e.g., "EEEE, d MMMM y"), which is localized using locale= (or the table locale), or a strftime() format string (any string containing %, e.g., "%d.%m.%Y"), which isn’t localized. If format_str= is provided, the date_style=, time_style=, and sep= arguments are ignored.
sep: str = " "
-
A string that separates the date and time components of the datetime string. The default is a space character (" "). This is ignored if format_str= is provided.
pattern: str = "{x}"
-
A formatting pattern that allows for decoration of the formatted value. The formatted value is represented by the {x} (which can be used multiple times, if needed) and all other characters will be interpreted as string literals.
locale: str | None = None
-
An optional locale identifier that can be used for formatting values according the locale’s rules. Examples include
"en" for English (United States) and "fr" for French (France). This can’t be used with a strftime() format string in format_str=, since that isn’t localized (with one, a table locale set in GT() is ignored).
Returns
GT
-
The GT object is returned. This is the same object that the method is called on so that we can facilitate method chaining.
Examples
Let’s use the exibble dataset to create a simple, two-column table (keeping only the date and time columns). With the fmt_datetime() method, we’ll format the date column to display dates formatted with the "month_day_year" date style and the time column to display times formatted with the "h_m_s_p" time style.
from great_tables import GT, exibble
exibble_mini = exibble[["date", "time"]]
(
GT(exibble_mini)
.fmt_datetime(
columns="date",
date_style="month_day_year",
time_style="h_m_s_p"
)
)
| date |
time |
| January 15, 2015 12:00:00 AM |
13:35 |
| February 15, 2015 12:00:00 AM |
14:40 |
| March 15, 2015 12:00:00 AM |
15:45 |
| April 15, 2015 12:00:00 AM |
16:50 |
| May 15, 2015 12:00:00 AM |
17:55 |
| June 15, 2015 12:00:00 AM |
None |
| None |
19:10 |
| August 15, 2015 12:00:00 AM |
20:20 |
With a CLDR pattern in format_str=, we choose the fields and their order ourselves. The names of the weekdays and months are still localized, here for a table in French.
(
GT(exibble_mini, locale="fr")
.fmt_datetime(columns="date", format_str="EEEE d MMMM y")
)
| date |
time |
| jeudi 15 janvier 2015 |
13:35 |
| dimanche 15 février 2015 |
14:40 |
| dimanche 15 mars 2015 |
15:45 |
| mercredi 15 avril 2015 |
16:50 |
| vendredi 15 mai 2015 |
17:55 |
| lundi 15 juin 2015 |
None |
| None |
19:10 |
| samedi 15 août 2015 |
20:20 |