BUG: Timestamp constructor silently ignoring arguments (GH#31930) - #66535
Open
jbrockmendel wants to merge 1 commit into
Open
BUG: Timestamp constructor silently ignoring arguments (GH#31930)#66535jbrockmendel wants to merge 1 commit into
jbrockmendel wants to merge 1 commit into
Conversation
Timestamp takes either a value-form first argument (datetime-like, str or epoch number) or the by-component arguments, but mixing them silently dropped arguments: - a component passed alongside a value was ignored - in the positional datetime-like form the parameter names are shifted by one, so a keyword argument landed one field early - the eighth positional argument, pydatetime's tzinfo, was dropped The first two now raise ValueError; the third is attached as pydatetime does. That last one also makes copy.replace keep the timezone, since CPython reconstructs datetime subclasses through the eight-argument form. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #31930
Timestamptakes either a value-form first argument (datetime-like, str or epoch number) or the by-component arguments, but mixing the two silently dropped arguments. Three parts:Timestamp(datetime(2020, 12, 31), hour=5)gave2020-12-31 00:00:00. Now raisesValueError, naming the arguments that were dropped.datetime-like form the parameter names are shifted by one (the parameter namedhourisdatetime'sminute), so a keyword argument landed one field early —Timestamp(2020, 12, 31, hour=5)gave00:05:00. A gap in the positional arguments now raisesValueError, as does leaving the same gap with an explicitNone.datetime'stzinfoand was dropped. It is now attached asdatetimedoes, rather than routed throughtz=, which would localize instead. This also fixescopy.replace, which reconstructs aTimestampthrough that eight-argument form and so previously returned a timezone-naive result for timezone-aware input. This resolves themicrosecond/second/minutehalf of DEPR: make Timestamp arguments (especially unit) keyword-only #45307.Two mixed calls cannot be detected and are unchanged: an
intfirst argument is indistinguishable from ayear, and a keyword that exactly fills the next free positional slot is indistinguishable from passing it positionally. Both are documented in the class docstring and the whatsnew. Null-like input remains NaT-in/NaT-out for every combination of the components.read_pickleneedsValueErrorinexcs_to_catch: pandas <= 1.2 pickledTimestampas(value, freq, tz), so those args now raise out of the constructor, and without this the compat unpickler added in GH-61792 is never reached. This is also what makes that fix cover a legacy pickle with no module renames — on main such a file still loads throughpickle.loaddirectly and loses its timezone.Constructor microbenchmarks show no regressions; several paths get faster, mostly from dropping an unconditional list allocation the new check replaced (
Timestamp(ts)passthrough -26%, string parsing -13%).