-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG: index to_period
- to_timestamp
round trip with freq
argument fails specifically with period-start
frequencies
#58974
Comments
Thanks for the report! What I find weirder is that if I instead do ValueError("for Period, please use 'MS' instead of 'MS'") cc @natmokval |
I believe passing strings stopped working in the relevant places since Are you not testing the various round trips and conversions? I have no good view of the testing framework, but I would just test Including:
We have seen so many changes of behaviour around Very related, imo a key issue is "unified API" across dfferent frequencies. E.g., if you do the round trip and pass |
thanks @fkiraly for the report Just for a bit of context: for offsets, pandas distinguishes between the start of the period and the end. See #9586 for further discussion As for this issue...let's take a look. The docs for to_timestamp say
So, it looks correct (or at least, as-documented) that the input should be a Period alias, and that The start and end time for a month Period are at the beginning and end of the month:
Based on that, I'd have expected
But, it doesn't - it outputs
This can be observed even going back to pandas 2.0.3, before #9586 started to be addressed
Based on the docstring of The road forwardsIt looks like
Ah this makes no sense, you're right, thanks. @natmokval fancy taking a look at this error message? |
@fkiraly if you want something which round-trips, I think this should do the trick: ix = pd.date_range("1/1/1870", periods=20, freq=freq)
ix2 = ix.to_period()
ix3 = ix2.to_timestamp() + pd.tseries.frequencies.to_offset(f'0{freq}')
ix3.freq = freq though I'd suggest considering whether you really need Period at all |
Yeah, this is odd behavior. I missed this, unfortunately.
Why In the meantime, maybe we can update the documentation for |
thanks for fixing the error message so quickly. |
yeah maybe - let's take this discussion over to #59371, and leave this one for how to round-trip |
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
Issue Description
Converting
DateTimeIndex
toPeriodIndex
and back via ato_period
-to_datetime
round trip that has thefreq
argument set in theto_datetime
, to thefreq
of the originalDateTimeIndex
, fails withValueError: <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 toix
.Installed Versions
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
The text was updated successfully, but these errors were encountered: