Skip to content

Commit

Permalink
Reorg params, change test, add attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Feb 26, 2018
1 parent 98e0b0c commit b8f836c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
20 changes: 12 additions & 8 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ cdef class _Timestamp(datetime):
cdef readonly:
int64_t value, nanosecond
object freq # frequency reference
list _date_attributes

def __hash__(_Timestamp self):
if self.nanosecond:
Expand Down Expand Up @@ -425,10 +426,10 @@ class Timestamp(_Timestamp):
.. versionadded:: 0.19.0
hour, minute, second, microsecond : int, optional, default 0
.. versionadded:: 0.19.0
tzinfo : datetime.tzinfo, optional, default None
.. versionadded:: 0.19.0
nanosecond : int, optional, default 0
.. versionadded:: 0.23.0
tzinfo : datetime.tzinfo, optional, default None
.. versionadded:: 0.19.0
Notes
-----
Expand Down Expand Up @@ -558,7 +559,7 @@ class Timestamp(_Timestamp):
object freq=None, tz=None, unit=None,
year=None, month=None, day=None,
hour=None, minute=None, second=None, microsecond=None,
tzinfo=None, nanosecond=None):
nanosecond=None, tzinfo=None):
# The parameter list folds together legacy parameter names (the first
# four) and positional and keyword parameter names from pydatetime.
#
Expand All @@ -582,6 +583,9 @@ class Timestamp(_Timestamp):

cdef _TSObject ts

_date_attributes = [year, month, day, hour, minute, second,
microsecond, nanosecond]

if tzinfo is not None:
if not PyTZInfo_Check(tzinfo):
# tzinfo must be a datetime.tzinfo object, GH#17690
Expand All @@ -593,9 +597,9 @@ class Timestamp(_Timestamp):
if is_string_object(ts_input):
# User passed a date string to parse.
# Check that the user didn't also pass a date attribute kwarg.
date_attrs = [year, month, day, hour, minute, second, microsecond,
nanosecond]
if any(arg is not None for arg in date_attrs):
#date_attrs = [year, month, day, hour, minute, second, microsecond,
# nanosecond]
if any(arg is not None for arg in _date_attributes):
raise ValueError('Cannot pass a date attribute keyword '
'argument when passing a date string')

Expand All @@ -611,10 +615,10 @@ class Timestamp(_Timestamp):
elif is_integer_object(freq):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
# microsecond[, tzinfo]]]]])
# microsecond[, nanosecond[, tzinfo]]]]]])
return Timestamp(datetime(ts_input, freq, tz, unit or 0,
year or 0, month or 0, day or 0,
hour), nanosecond=minute, tz=hour)
minute), nanosecond=hour, tz=minute)

if tzinfo is not None:
# User passed tzinfo instead of tz; avoid silently ignoring
Expand Down
14 changes: 8 additions & 6 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,16 @@ def test_constructor_fromordinal(self):
assert ts.to_pydatetime() == dt_tz

@pytest.mark.parametrize('result', [
Timestamp(datetime(2000, 1, 1), nanosecond=1),
Timestamp(year=2000, month=1, day=1, nanosecond=1),
Timestamp(year=2000, month=1, day=1, tz='UTC', nanosecond=1),
Timestamp(2000, 1, 1, 0, 0, 0, 0, None, 1),
Timestamp(2000, 1, 1, 0, 0, 0, 0, pytz.UTC, 1)])
Timestamp(datetime(2000, 1, 2, 3, 4, 5, 6), nanosecond=1),
Timestamp(year=2000, month=1, day=2, hour=3, minute=4, second=5,
microsecond=6, nanosecond=1),
Timestamp(year=2000, month=1, day=2, hour=3, minute=4, second=5,
microsecond=6, nanosecond=1, tz='UTC'),
Timestamp(2000, 1, 2, 3, 4, 5, 6, 1, None),
Timestamp(2000, 1, 2, 3, 4, 5, 6, 1, pytz.UTC)])
def test_constructor_nanosecond(self, result):
# GH 18898
expected = Timestamp(datetime(2000, 1, 1), tz=result.tz)
expected = Timestamp(datetime(2000, 1, 2, 3, 4, 5, 6), tz=result.tz)
expected = expected + Timedelta(nanoseconds=1)
assert result == expected

Expand Down

0 comments on commit b8f836c

Please sign in to comment.