Skip to content

BUG: strftime format string validation is inconsistent between Timestamp and DatetimeIndex #48588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
kandersolar opened this issue Sep 16, 2022 · 1 comment
Closed
2 of 3 tasks
Labels
Bug datetime.date stdlib datetime.date support Needs Triage Issue that has not been reviewed by a pandas team member Timestamp pd.Timestamp and associated methods

Comments

@kandersolar
Copy link
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
fmt = '%Y-%m-%d %H:%M%'  # note the trailing % character
dt = '2019-11-01 00:00:00-07:00'
pd.to_datetime([dt]).strftime(fmt)  # no error
pd.to_datetime(dt).strftime(fmt)  # ValueError: Invalid format string

Issue Description

On Windows, the validation performed on the format string is not the same between Timestamp.strftime and DatetimeIndex.strftime. With the included reproducible example, DatetimeIndex.strftime executes without error (although the correctness of the returned string is questionable):

In [11]: pd.to_datetime([dt]).strftime(fmt)
Out[11]: Index(['2019-11-01 00:00:00-07:00'], dtype='object')

Whereas the Timestamp equivalent raises an error:

In [12]: pd.to_datetime(dt).strftime(fmt)
Traceback (most recent call last):

  File "C:\Users\KANDERSO\AppData\Local\Temp\1\ipykernel_21592\2314342370.py", line 1, in <module>
    pd.to_datetime(dt).strftime(fmt)  # ValueError: Invalid format string

  File "pandas\_libs\tslibs\timestamps.pyx", line 1164, in pandas._libs.tslibs.timestamps.Timestamp.strftime

ValueError: Invalid format string

I believe the format string in the above example is invalid due to the trailing % character (although I'm not positive since format codes are platform-specific to some degree), but regardless I'd expect consistent validation behavior between Timestamp.strftime and DatetimeIndex.strftime, all else equal.

On Linux with this particular format string I get no difference in behavior between Timestamp and DatetimeIndex, although I note that the returned string is different from on Windows: '2019-11-01 00:00%' and Index(['2019-11-01 00:00%'], dtype='object'), respectively.

Expected Behavior

For the same format string and underlying timestamp, I expected Timestamp.strftime and DatetimeIndex.strftime to have equivalent format validation behavior, i.e. either both of them raise an error or neither does.

Installed Versions

Details (Windows)

INSTALLED VERSIONS

commit : ca60aab
python : 3.10.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.4
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Details (Linux)

INSTALLED VERSIONS

commit : ca60aab
python : 3.10.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-76051900-generic
Version : #202207312230166078056622.04~9d60db1 SMP PREEMPT_DYNAMIC Thu A
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.4
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@kandersolar kandersolar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 16, 2022
@Dr-Irv Dr-Irv added datetime.date stdlib datetime.date support Timestamp pd.Timestamp and associated methods labels Sep 16, 2022
@mroeschke
Copy link
Member

Looks like this has been fixed on main so closing

In [10]: pd.to_datetime([dt]).strftime(fmt)
Out[10]: Index(['2019-11-01 00:00%'], dtype='object')

In [11]: pd.to_datetime(dt).strftime(fmt)
Out[11]: '2019-11-01 00:00%'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug datetime.date stdlib datetime.date support Needs Triage Issue that has not been reviewed by a pandas team member Timestamp pd.Timestamp and associated methods
Projects
None yet
Development

No branches or pull requests

3 participants