Skip to content

pd.Timestamp constructor ignores missing arguments #32683

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ class Timestamp(_Timestamp):
cls,
object ts_input=_no_input,
object freq=None,
tz=None,
unit=None,
year=None,
month=None,
Expand All @@ -357,8 +356,9 @@ class Timestamp(_Timestamp):
second=None,
microsecond=None,
nanosecond=None,
tzinfo=None,
*,
tz=None,
Copy link
Contributor

Choose a reason for hiding this comment

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

we cannot change the position of these args

tzinfo=None,
fold=None
):
# The parameter list folds together legacy parameter names (the first
Expand Down Expand Up @@ -470,10 +470,9 @@ class Timestamp(_Timestamp):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
# microsecond[, nanosecond[, tzinfo]]]]]])
ts_input = datetime(ts_input, freq, tz, unit or 0,
year or 0, month or 0, day or 0, fold=fold or 0)
nanosecond = hour
tz = minute
ts_input = datetime(ts_input, freq, unit,
year or 0, month or 0, day or 0, hour or 0, fold=fold or 0)
nanosecond = minute
freq = None

if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/scalar/timestamp/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,32 @@ def test_timestamp_timetz_equivalent_with_datetime_tz(self, tz_naive_fixture):
expected = _datetime.timetz()

assert result == expected


class TestConstructorWithTZ:
def test_timestamp_creating_with_tz_or_tzinfo_only_date(self):
stamp = Timestamp(2019, 1, 1, tzinfo=pytz.timezone("EST5EDT"))
stamp2 = Timestamp(2019, 1, 1, tz="EST5EDT")
expected = datetime(2019, 1, 1, tzinfo=pytz.timezone("EST5EDT"))
assert stamp == expected
assert stamp2 == expected

def test_timestamp_creating_with_tz_or_tzinfo_all_fields(self):
stamp = Timestamp(2019, 1, 1, 1, 1, 1, 1, tzinfo=pytz.timezone("EST5EDT"))
stamp2 = Timestamp(2019, 1, 1, 1, 1, 1, 1, tz="EST5EDT")
expected = datetime(2019, 1, 1, 1, 1, 1, 1, pytz.timezone("EST5EDT"))
assert stamp == expected
assert stamp2 == expected

def test_timestamp_tzinfo_has_affect(self):
stamp = Timestamp(2019, 1, 1, 1, tzinfo=pytz.timezone("EST5EDT"))
stamp2 = Timestamp(2019, 1, 1, 1, tz="UTC")
assert stamp != stamp2

def test_creating_with_nano(self):
stamp = Timestamp(2019, 1, 2, 3, 4, 5, 6, 7, tzinfo=pytz.timezone("UTC"))
assert str(stamp) == "2019-01-02 03:04:05.000006007+00:00"

def test_creating_with_kwargs(self):
stamp = Timestamp(year=2019, month=1, day=2, hour=3, minute=4, second=5, microsecond=6, nanosecond=7, tz="UTC")
assert str(stamp) == "2019-01-02 03:04:05.000006007+00:00"