Skip to content

BUG: Timestamp constructor silently ignoring arguments (GH#31930) - #66535

Open
jbrockmendel wants to merge 1 commit into
pandas-dev:mainfrom
jbrockmendel:bug-31930-2
Open

BUG: Timestamp constructor silently ignoring arguments (GH#31930)#66535
jbrockmendel wants to merge 1 commit into
pandas-dev:mainfrom
jbrockmendel:bug-31930-2

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

closes #31930

Timestamp takes 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:

  1. A component passed alongside a value was ignored — Timestamp(datetime(2020, 12, 31), hour=5) gave 2020-12-31 00:00:00. Now raises ValueError, naming the arguments that were dropped.
  2. In the positional datetime-like form the parameter names are shifted by one (the parameter named hour is datetime's minute), so a keyword argument landed one field early — Timestamp(2020, 12, 31, hour=5) gave 00:05:00. A gap in the positional arguments now raises ValueError, as does leaving the same gap with an explicit None.
  3. The eighth positional argument is datetime's tzinfo and was dropped. It is now attached as datetime does, rather than routed through tz=, which would localize instead. This also fixes copy.replace, which reconstructs a Timestamp through that eight-argument form and so previously returned a timezone-naive result for timezone-aware input. This resolves the microsecond/second/minute half of DEPR: make Timestamp arguments (especially unit) keyword-only #45307.

Two mixed calls cannot be detected and are unchanged: an int first argument is indistinguishable from a year, 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_pickle needs ValueError in excs_to_catch: pandas <= 1.2 pickled Timestamp as (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 through pickle.load directly 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%).

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Error Reporting Incorrect or improved errors from pandas Timestamp pd.Timestamp and associated methods Timezones Timezone data dtype

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pd.Timestamp constructor ignores missing arguments

1 participant