Skip to content

Commit ffd9b28

Browse files
committed
CLN: Worked on Code Reveiew
1 parent 978f0d2 commit ffd9b28

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

doc/source/whatsnew/v0.19.0.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ New features
2424

2525
.. _whatsnew_0190.dev_api:
2626

27-
to_datetime can be used with Offset
28-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29-
``pd.to_datetime`` has a new parameter, ``origin``, to define an offset for ``DatetimeIndex``.
27+
to_datetime has gained an origin kwarg
28+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
``pd.to_datetime`` has a new parameter, ``origin``, to define reference date for ``DatetimeIndex``.
3030

3131
.. ipython:: python
3232

3333
to_datetime([1,2,3], unit='D', origin=pd.Timestamp('1960-01-01'))
3434

35-
The above code would return days with offset from origin as defined by timestamp set by origin.
35+
The above code would parse the numeric values as number of days (unit='D')
36+
since origin as reference date.
3637

3738
pandas development API
3839
^^^^^^^^^^^^^^^^^^^^^^

pandas/tseries/tests/test_timeseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def test_to_datetime_origin(self):
796796
epoch_timestamp_convertible = [epoch_1960, epoch_1960.to_datetime(),
797797
epoch_1960.to_datetime64(),
798798
str(epoch_1960)]
799-
invalid_origins = ['random_string', '13-24-1990']
799+
invalid_origins = ['random_string', '13-24-1990', '0001-01-01']
800800
units_from_epoch = [0, 1, 2, 3, 4]
801801

802802
for unit in units:

pandas/tseries/tools.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,15 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
425425
except (ValueError, TypeError):
426426
raise e
427427

428-
def result_without_offset(arg):
428+
def intermediate_result(arg):
429429
if origin == 'julian':
430430
if unit != 'D':
431431
raise ValueError("unit must be 'D' for origin='julian'")
432-
arg = arg - tslib.Timestamp(0).to_julian_date()
432+
try:
433+
arg = arg - tslib.Timestamp(0).to_julian_date()
434+
except:
435+
raise ValueError("incompatible 'arg' type for given "
436+
"'origin'='julian'")
433437
if arg is None:
434438
return arg
435439
elif isinstance(arg, tslib.Timestamp):
@@ -446,10 +450,10 @@ def result_without_offset(arg):
446450
return _convert_listlike(arg, box, format)
447451
return _convert_listlike(np.array([arg]), box, format)[0]
448452

449-
result = result_without_offset(arg)
453+
result = intermediate_result(arg)
450454

451455
offset = None
452-
if origin != 'epoch' and origin != 'julian':
456+
if origin not in ['epoch','julian']:
453457
try:
454458
offset = tslib.Timestamp(origin) - tslib.Timestamp(0)
455459
except ValueError:

0 commit comments

Comments
 (0)