GT.fmt_time(
columns=None,
rows=None,
time_style="iso",
pattern="{x}",
locale=None,
)
Format input values to time values using one of 25 preset time styles. Input can be in the form of time values, or strings in the ISO 8601 forms of HH:MM:SS or YYYY-MM-DD HH:MM:SS. For datetime values (and datetime strings), the time part is formatted. Dates without a time (e.g., "2024-01-05") raise an error; use fmt_date() for those.
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.
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.
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).
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.
Adapting Output To A Specific locale
This formatting method can adapt outputs according to a provided locale value. Examples include "en" for English (United States) and "fr" for French (France). Note that a locale value provided here will override any global locale setting performed in GT()’s own locale argument (it is settable there as a value received by all other methods that have a locale argument).
Examples
Let’s use the exibble dataset to create a simple, two-column table (keeping only the date and time columns). With the fmt_time() method, we’ll format 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_time(columns="time", time_style="h_m_s_p")
)
| date |
time |
| 2015-01-15 |
1:35:00 PM |
| 2015-02-15 |
2:40:00 PM |
| 2015-03-15 |
3:45:00 PM |
| 2015-04-15 |
4:50:00 PM |
| 2015-05-15 |
5:55:00 PM |
| 2015-06-15 |
None |
| None |
7:10:00 PM |
| 2015-08-15 |
8:20:00 PM |
A flexible time style adapts to the locale. Here, the "hm" style is used to format the times for a table in Japanese.
(
GT(exibble_mini, locale="ja")
.fmt_time(columns="time", time_style="hm")
)
| date |
time |
| 2015-01-15 |
午後1:35 |
| 2015-02-15 |
午後2:40 |
| 2015-03-15 |
午後3:45 |
| 2015-04-15 |
午後4:50 |
| 2015-05-15 |
午後5:55 |
| 2015-06-15 |
None |
| None |
午後7:10 |
| 2015-08-15 |
午後8:20 |