Skip to content

API: Period.to_timestamp default to microsecond unit#63760

Merged
jorisvandenbossche merged 10 commits into
pandas-dev:mainfrom
jbrockmendel:api-to_timestamp
Jan 21, 2026
Merged

API: Period.to_timestamp default to microsecond unit#63760
jorisvandenbossche merged 10 commits into
pandas-dev:mainfrom
jbrockmendel:api-to_timestamp

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.
  • If I used AI to develop this pull request, I prompted it to follow AGENTS.md.

cc @jorisvandenbossche do we want to get this in under the wire?

@jorisvandenbossche jorisvandenbossche added Period Period data type Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods labels Jan 19, 2026
Copy link
Copy Markdown
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that would be good!

Comment thread pandas/_libs/tslibs/period.pyx Outdated
elif freq == FR_DAY:
dta = periodarr.base.view("M8[D]")
return astype_overflowsafe(dta, dtype=DT64NS_DTYPE)
# GH#??? give microseconds for everything other than freq="ns"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# GH#??? give microseconds for everything other than freq="ns"
# give microseconds for everything other than freq="ns"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update with the GH ref

Comment thread pandas/core/arrays/period.py Outdated
return self.to_timestamp(how="start") + adjust
else:
adjust = Timedelta(1, "ns")
adjust = Timedelta(1, unit=unit).as_unit(unit)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
adjust = Timedelta(1, unit=unit).as_unit(unit)
adjust = Timedelta(1, unit=unit)

Is that still needed now? (I thought we follow the input unit for integers)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you're right, will update

result = obj.to_timestamp("H", "end")
exp_index = _get_with_delta(delta)
exp_index = exp_index + Timedelta(1, "h") - Timedelta(1, "ns")
exp_index = exp_index + np.timedelta64(1, "h") - np.timedelta64(1, "us")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason you are switching here from Timedelta to np.timedelta64 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i started this branch before we changed the Timedelta behavior. will revert this bit

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the same change is used in some other files as well)

@@ -42,10 +41,8 @@ def test_to_timestamp_out_of_bounds(self):
# GH#19643, used to incorrectly give Timestamp in 1754
with tm.assert_produces_warning(FutureWarning, match=bday_msg):
per = Period("0001-01-01", freq="B")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would change the freq to use ns, then it would keep raising the out of bounds?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean the freq we pass to the Period constructor in this example? that would raise at construction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's what I meant, but indeed not helping to test this

@jorisvandenbossche jorisvandenbossche added this to the 3.0 milestone Jan 20, 2026
@jorisvandenbossche
Copy link
Copy Markdown
Member

Some typing failures

@jorisvandenbossche jorisvandenbossche merged commit 7cff0f4 into pandas-dev:main Jan 21, 2026
42 checks passed
@jorisvandenbossche
Copy link
Copy Markdown
Member

Thanks @jbrockmendel!

@jbrockmendel jbrockmendel deleted the api-to_timestamp branch January 21, 2026 16:52
vkverma9534 pushed a commit to vkverma9534/pandas that referenced this pull request Jan 30, 2026
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
vkverma9534 pushed a commit to vkverma9534/pandas that referenced this pull request May 2, 2026
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Non-Nano datetime64/timedelta64 with non-nanosecond resolution Period Period data type Timestamp pd.Timestamp and associated methods

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants