Skip to content

Commit 05b976c

Browse files
sinhrksjreback
authored andcommitted
TST: add tests for Timestamp.toordinal/fromordinal
follow-up for pandas-dev#13593 Author: sinhrks <[email protected]> Closes pandas-dev#13610 from sinhrks/depr_timestamp_offset2 and squashes the following commits: 28f8d41 [sinhrks] TST: add tests for Timestamp.toordinal
1 parent c9a27ed commit 05b976c

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

pandas/tseries/tests/test_tslib.py

+27
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ def test_constructor_keyword(self):
255255
hour=1, minute=2, second=3, microsecond=999999)),
256256
repr(Timestamp('2015-11-12 01:02:03.999999')))
257257

258+
def test_constructor_fromordinal(self):
259+
base = datetime.datetime(2000, 1, 1)
260+
261+
ts = Timestamp.fromordinal(base.toordinal(), freq='D')
262+
self.assertEqual(base, ts)
263+
self.assertEqual(ts.freq, 'D')
264+
self.assertEqual(base.toordinal(), ts.toordinal())
265+
266+
ts = Timestamp.fromordinal(base.toordinal(), tz='US/Eastern')
267+
self.assertEqual(pd.Timestamp('2000-01-01', tz='US/Eastern'), ts)
268+
self.assertEqual(base.toordinal(), ts.toordinal())
269+
258270
def test_constructor_offset_depr(self):
259271
# GH 12160
260272
with tm.assert_produces_warning(FutureWarning,
@@ -270,6 +282,21 @@ def test_constructor_offset_depr(self):
270282
with tm.assertRaisesRegexp(TypeError, msg):
271283
Timestamp('2011-01-01', offset='D', freq='D')
272284

285+
def test_constructor_offset_depr_fromordinal(self):
286+
# GH 12160
287+
base = datetime.datetime(2000, 1, 1)
288+
289+
with tm.assert_produces_warning(FutureWarning,
290+
check_stacklevel=False):
291+
ts = Timestamp.fromordinal(base.toordinal(), offset='D')
292+
self.assertEqual(pd.Timestamp('2000-01-01'), ts)
293+
self.assertEqual(ts.freq, 'D')
294+
self.assertEqual(base.toordinal(), ts.toordinal())
295+
296+
msg = "Can only specify freq or offset, not both"
297+
with tm.assertRaisesRegexp(TypeError, msg):
298+
Timestamp.fromordinal(base.toordinal(), offset='D', freq='D')
299+
273300
def test_conversion(self):
274301
# GH 9255
275302
ts = Timestamp('2000-01-01')

pandas/tslib.pyx

+18-3
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,14 @@ class Timestamp(_Timestamp):
235235
----------
236236
ts_input : datetime-like, str, int, float
237237
Value to be converted to Timestamp
238-
offset : str, DateOffset
238+
freq : str, DateOffset
239239
Offset which Timestamp will have
240240
tz : string, pytz.timezone, dateutil.tz.tzfile or None
241241
Time zone for time which Timestamp will have.
242242
unit : string
243243
numpy unit used for conversion, if ts_input is int or float
244+
offset : str, DateOffset
245+
Deprecated, use freq
244246
245247
The other two forms mimic the parameters from ``datetime.datetime``. They
246248
can be passed by either position or keyword, but not both mixed together.
@@ -262,8 +264,21 @@ class Timestamp(_Timestamp):
262264

263265
@classmethod
264266
def fromordinal(cls, ordinal, freq=None, tz=None, offset=None):
265-
""" passed an ordinal, translate and convert to a ts
266-
note: by definition there cannot be any tz info on the ordinal itself """
267+
"""
268+
passed an ordinal, translate and convert to a ts
269+
note: by definition there cannot be any tz info on the ordinal itself
270+
271+
Parameters
272+
----------
273+
ordinal : int
274+
date corresponding to a proleptic Gregorian ordinal
275+
freq : str, DateOffset
276+
Offset which Timestamp will have
277+
tz : string, pytz.timezone, dateutil.tz.tzfile or None
278+
Time zone for time which Timestamp will have.
279+
offset : str, DateOffset
280+
Deprecated, use freq
281+
"""
267282
return cls(datetime.fromordinal(ordinal), freq=freq, tz=tz, offset=offset)
268283

269284
@classmethod

0 commit comments

Comments
 (0)