Skip to content

Commit 81b3008

Browse files
Anthony Delannoyatbd
Anthony Delannoy
authored andcommitted
API: to_datetime, required unit with numerical (pandas-dev#15836)
* add test_to_datetime_numerical_input * check arg for numerical type
1 parent e50d397 commit 81b3008

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/tests/indexes/datetimes/test_tools.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def test_to_datetime_types(self):
688688

689689
# ints
690690
result = Timestamp(0)
691-
expected = to_datetime(0)
691+
expected = to_datetime(0, unit='ns')
692692
self.assertEqual(result, expected)
693693

694694
# GH 3888 (strings)
@@ -715,6 +715,13 @@ def test_to_datetime_unprocessable_input(self):
715715
)
716716
self.assertRaises(TypeError, to_datetime, [1, '1'], errors='raise')
717717

718+
def test_to_datetime_numerical_input(self):
719+
self.assertRaises(ValueError, to_datetime, arg=int(1000))
720+
self.assertRaises(ValueError, to_datetime, arg=float(1000))
721+
self.assertRaises(ValueError, to_datetime, arg=range(5))
722+
self.assertRaises(ValueError, to_datetime,
723+
arg=np.array(range(5), dtype='d'))
724+
718725
def test_to_datetime_other_datetime64_units(self):
719726
# 5/25/2012
720727
scalar = np.int64(1337904000000000).view('M8[us]')

pandas/tseries/tools.py

+4
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
437437
if arg is None:
438438
return None
439439

440+
check_arg = np.array(arg) if np.iterable(arg) else np.array([arg])
441+
if is_numeric_dtype(check_arg) and unit is None:
442+
raise ValueError("a unit is required in case of numerical arg")
443+
440444
# handle origin
441445
if origin == 'julian':
442446

0 commit comments

Comments
 (0)