-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
Is your feature request related to a problem?
It would be useful to set a display option that controls how datetimes are formatted. This is useful for both "pretty printing" purposes in situations e.g. Styler doesn't work for some reason, and for easier visual debugging and inspection of the data.
The spirit of this enhancement request is similar to #46744, and perhaps there is some room for unification here, e.g. a display option that takes a dict of dtype_or_lambda -> formatter
functions.
Describe the solution you'd like
One solution is to add a dispaly option, display.date_format
, with behavior analogous to the date_format=
parameter in DataFrame.to_csv
.
Another option is to add an analogous date_format=
parameter to DataFrame.to_string
. In general, is there any reason why display options shouldn't all be mirrored to DataFrame.to_string
?
You can also add a general-purpose display option and/or to_string
parameter that controls output formatting in more detail. It could accepts either a function or a dict. If a function, the function would be applied to format all entries. The default value would be str
, for backward compatibility; if you set this to repr
, it would cover the use case in #46744. If the value is a dict, the keys would be any of a literal dtype object, a column label, or a function that can be used to dispatch on dtype and/or even column name. And the values would be callables that format individual entries in the series. For example:
text_output = my_data.to_string(
formatters={
pandas.api.types.is_datetime64_any_dtype: lambda x: x.dt.strftime('...'), # Format the 'timestamp' column
'': repr, # Handle all other columns with a special dict key
}
)
API breaking implications
None that I know of.
Describe alternatives you've considered
Manually applying .strftime
is ad-hoc, and is undesirable in some cases (e.g. very large data frames).
Additional context
Inspired by a StackOverflow question, and of the same general category as #46744.