Skip to content

TST: add tests for Timestamp.toordinal/fromordinal #13610

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
27 changes: 27 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ def test_constructor_keyword(self):
hour=1, minute=2, second=3, microsecond=999999)),
repr(Timestamp('2015-11-12 01:02:03.999999')))

def test_constructor_fromordinal(self):
base = datetime.datetime(2000, 1, 1)

ts = Timestamp.fromordinal(base.toordinal(), freq='D')
self.assertEqual(base, ts)
self.assertEqual(ts.freq, 'D')
self.assertEqual(base.toordinal(), ts.toordinal())

ts = Timestamp.fromordinal(base.toordinal(), tz='US/Eastern')
self.assertEqual(pd.Timestamp('2000-01-01', tz='US/Eastern'), ts)
self.assertEqual(base.toordinal(), ts.toordinal())

def test_constructor_offset_depr(self):
# GH 12160
with tm.assert_produces_warning(FutureWarning,
Expand All @@ -270,6 +282,21 @@ def test_constructor_offset_depr(self):
with tm.assertRaisesRegexp(TypeError, msg):
Timestamp('2011-01-01', offset='D', freq='D')

def test_constructor_offset_depr_fromordinal(self):
# GH 12160
base = datetime.datetime(2000, 1, 1)

with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
ts = Timestamp.fromordinal(base.toordinal(), offset='D')
self.assertEqual(pd.Timestamp('2000-01-01'), ts)
self.assertEqual(ts.freq, 'D')
self.assertEqual(base.toordinal(), ts.toordinal())

msg = "Can only specify freq or offset, not both"
with tm.assertRaisesRegexp(TypeError, msg):
Timestamp.fromordinal(base.toordinal(), offset='D', freq='D')

def test_conversion(self):
# GH 9255
ts = Timestamp('2000-01-01')
Expand Down
21 changes: 18 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ class Timestamp(_Timestamp):
----------
ts_input : datetime-like, str, int, float
Value to be converted to Timestamp
offset : str, DateOffset
freq : str, DateOffset
Copy link
Contributor

Choose a reason for hiding this comment

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

let's list offset (and say it's deprecated)

Copy link
Contributor

Choose a reason for hiding this comment

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

as well

Copy link
Member Author

Choose a reason for hiding this comment

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

updated docstrings of class and fromordinal.

Offset which Timestamp will have
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
unit : string
numpy unit used for conversion, if ts_input is int or float
offset : str, DateOffset
Deprecated, use freq

The other two forms mimic the parameters from ``datetime.datetime``. They
can be passed by either position or keyword, but not both mixed together.
Expand All @@ -262,8 +264,21 @@ class Timestamp(_Timestamp):

@classmethod
def fromordinal(cls, ordinal, freq=None, tz=None, offset=None):
""" passed an ordinal, translate and convert to a ts
note: by definition there cannot be any tz info on the ordinal itself """
"""
passed an ordinal, translate and convert to a ts
note: by definition there cannot be any tz info on the ordinal itself

Parameters
----------
ordinal : int
date corresponding to a proleptic Gregorian ordinal
freq : str, DateOffset
Offset which Timestamp will have
tz : string, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time which Timestamp will have.
offset : str, DateOffset
Deprecated, use freq
"""
return cls(datetime.fromordinal(ordinal), freq=freq, tz=tz, offset=offset)

@classmethod
Expand Down