pandas.Period.strftime — pandas 3.0.0.dev0+2104.ge637b4290d documentation (original) (raw)
Period.strftime(fmt)#
Returns a formatted string representation of the Period.
fmt
must be None
or a string containing one or several directives. When None
, the format will be determined from the frequency of the Period. The method recognizes the same directives as the time.strftime()function of the standard Python distribution, as well as the specific additional directives %f
, %F
, %q
, %l
, %u
, %n
. (formatting & docs originally from scikits.timeries).
The strftime method provides a way to represent a Periodobject as a string in a specified format. This is particularly useful when displaying date and time data in different locales or customized formats, suitable for reports or user interfaces. It extends the standard Python string formatting capabilities with additional directives specific to pandas, accommodating features like fiscal years and precise sub-second components.
Parameters:
fmtstr or None
String containing the desired format directives. If None
, the format is determined based on the Period’s frequency.
See also
Return a formatted string of the Timestamp.
Convert argument to datetime.
Format a time object as a string according to a specified format string in the standard Python library.
Notes
- The
%f
directive is the same as%y
if the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute. - The
%F
directive is the same as%Y
if the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute. - The
%p
directive only affects the output hour field if the%I
directive is used to parse the hour. - The range really is
0
to61
; this accounts for leap seconds and the (very rare) double leap seconds. - The
%U
and%W
directives are only used in calculations when the day of the week and the year are specified.
Examples
from pandas import Period a = Period(freq='Q-JUL', year=2006, quarter=1) a.strftime('%F-Q%q') '2006-Q1'
Output the last month in the quarter of this date
a.strftime('%b-%Y') 'Oct-2005'
a = Period(freq='D', year=2001, month=1, day=1) a.strftime('%d-%b-%Y') '01-Jan-2001' a.strftime('%b. %d, %Y was a %A') 'Jan. 01, 2001 was a Monday'