Skip to content

Draft implementation #25463

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 2 commits into from
Closed
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
19 changes: 19 additions & 0 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, time
from functools import partial
import numbers

import numpy as np

Expand Down Expand Up @@ -398,6 +399,21 @@ def _adjust_to_origin(arg, origin, unit):
return arg


def _contains_numbers(arg):
"""Returns True if argument is a number or an iterable containing
some numbers.
"""
# deal with case where input is a number or a list of numbers
arr = np.asarray(arg)
if np.issubdtype(arr.dtype, np.number):
return True
elif np.isscalar(arr) and np.issubdtype(arr, np.number):
return True
else:
# inefficient
return any(isinstance(x, numbers.Number) for x in arg)


def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
utc=None, box=True, format=None, exact=True,
unit=None, infer_datetime_format=False, origin='unix',
Expand Down Expand Up @@ -570,6 +586,9 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
if origin != 'unix':
arg = _adjust_to_origin(arg, origin, unit)

if _contains_numbers(arg) and unit is None:
raise ValueError('When supplying numbers the unit must be specified.')

tz = 'utc' if utc else None
convert_listlike = partial(_convert_listlike_datetimes, tz=tz, unit=unit,
dayfirst=dayfirst, yearfirst=yearfirst,
Expand Down