Skip to content

Add support for extra formats to all .strftime() methods. #23594

@ngnpope

Description

@ngnpope

Code Sample, a copy-pastable example if possible

import datetime
import pandas as pd
x = pd.date_range(datetime.datetime.now(), periods=4, freq='Q')
i0 = x.strftime('%Y-Q%q')
i1 = x.to_period().strftime('%Y-Q%q')
print(i0)
print(i1)

Problem description

i0 will output values such as '2018-Q%q' and i1 will output the expected '2018-Q4'.

Without this, other messy or sub-optimal code is required, e.g.

df['dt'].dt.to_period('Q').dt.strftime('%Y-Q%q')
df['dt'].apply(lambda x: '%d-Q%d' % (x.year, pd.Timestamp(x).quarter))

Expected Output

It would be nice if all .strftime() methods in pandas supported the extra formats that periods do:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Period.strftime.html

(I realise that the other methods defer to Python's datetime.datetime.strftime().)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.15.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.9-arch1-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.utf8
LOCALE: None.None

pandas: 0.23.4
pytest: 3.1.2
pip: 18.1
setuptools: 40.2.0
Cython: 0.25.2
numpy: 1.10.4
scipy: 0.19.0
pyarrow: 0.9.0
xarray: None
IPython: 5.0.0
sphinx: 1.4.4
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: 1.5.1
bottleneck: None
tables: 3.4.2
numexpr: 2.6.2
feather: 0.4.0
matplotlib: 2.2.2
openpyxl: 2.1.4
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: 3.5.0
bs4: 4.6.3
html5lib: 0.999
sqlalchemy: 1.2.12
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions