Skip to content

BUG: index to_period - to_timestamp round trip with freq argument fails specifically with period-start frequencies #58974

Open
@fkiraly

Description

@fkiraly

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

freq = "MS"
# works with freq = "ME"

ix = pd.date_range("1/1/1870", periods=20, freq=freq)
ix2 = ix.to_period()

# this line fails for period-start frequencies, but not for period-end or others
ix3 = ix2.to_timestamp(freq=ix.freq)

Issue Description

Converting DateTimeIndex to PeriodIndex and back via a to_period - to_datetime round trip that has the freq argument set in the to_datetime, to the freq of the original DateTimeIndex, fails with ValueError: <MonthBegin> is not supported as period frequency.

This is counterintuitive, and inconsistent with most other frequencies, including period-end frequencies like <MonthEnd>, so I consider it a bug.

Related is this downstream failure in sktime sktime/sktime#6572, which has so far assumed that the above round trip is a trustworthy contract. So, if it is not, a quick confirmation would be appreciated.

Expected Behavior

No failure.

ix3 is assigned identical to ix.

Installed Versions

INSTALLED VERSIONS ------------------ commit : d9cdd2e python : 3.11.5.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22631 machine : AMD64 processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.1252

pandas : 2.2.2
numpy : 1.26.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3.1
Cython : 3.0.6
pytest : 7.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : 0.59.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.4
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.4.1
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions