pandas.Period.strftime — pandas 3.0.0rc0+33.g1fd184de2a 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
%fdirective is the same as%yif the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute. - The
%Fdirective is the same as%Yif the frequency is not quarterly. Otherwise, it corresponds to the ‘fiscal’ year, as defined by the qyear attribute. - The
%pdirective only affects the output hour field if the%Idirective is used to parse the hour. - The range really is
0to61; this accounts for leap seconds and the (very rare) double leap seconds. - The
%Uand%Wdirectives 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'