Skip to content

BUG: PeriodIndex.to_timestamp with explicit freq ignores how="start"#65017

Draft
jbrockmendel wants to merge 2 commits intopandas-dev:mainfrom
jbrockmendel:bug-59371
Draft

BUG: PeriodIndex.to_timestamp with explicit freq ignores how="start"#65017
jbrockmendel wants to merge 2 commits intopandas-dev:mainfrom
jbrockmendel:bug-59371

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

  • fixes BUG: PeriodIndex.to_timestamp inconsistent with its docstring #59371
  • When an explicit freq coarser than daily (e.g. 'M', 'Q', 'Y') was passed to PeriodIndex.to_timestamp or Period.to_timestamp, the result always produced end-of-period timestamps, ignoring how="start" (the default)
  • Root cause: get_unix_date in period.pyx hardcodes is_end=True for the sub-daily frequency conversion path
  • Fix: when the target freq base is coarser than daily, convert to the daily base via asfreq before passing to periodarr_to_dt64arr/period_ordinal_to_dt64, matching the existing freq=None codepath

Before:

>>> pd.period_range('2000', periods=3, freq='M').to_timestamp('M')
DatetimeIndex(['2000-01-31', '2000-02-29', '2000-03-31'], dtype='datetime64[ns]', freq='ME')

After:

>>> pd.period_range('2000', periods=3, freq='M').to_timestamp('M')
DatetimeIndex(['2000-01-01', '2000-02-01', '2000-03-01'], dtype='datetime64[us]', freq='MS')

Test plan

  • Added tests for array path (monthly and quarterly to_timestamp with explicit freq)
  • Added test for scalar Period.to_timestamp path
  • All existing to_timestamp tests pass (154 tests)
  • All period index tests pass (466 tests)
  • All scalar period tests pass (417 tests)
  • Pre-commit hooks pass

🤖 Generated with Claude Code

jbrockmendel and others added 2 commits April 2, 2026 08:45
…andas-dev#59371)

When an explicit freq coarser than daily was passed to to_timestamp,
the result always produced end-of-period timestamps due to a hardcoded
is_end=True in get_unix_date. Fix by converting to daily base first,
matching the existing freq=None codepath.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Update expected values in doctests and tests that relied on the old
(buggy) behavior where to_timestamp with an explicit freq coarser
than daily always produced end-of-period timestamps.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: PeriodIndex.to_timestamp inconsistent with its docstring

1 participant