Skip to content

BUG/COMPAT: to_datetime #13052

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

Merged
merged 1 commit into from
May 1, 2016
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ In addition to this error change, several others have been made as well:
``to_datetime`` error changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Bugs in ``pd.to_datetime()`` when passing a ``unit`` with convertible entries and ``errors='coerce'`` or non-convertible with ``errors='ignore'`` (:issue:`11758`)
Bugs in ``pd.to_datetime()`` when passing a ``unit`` with convertible entries and ``errors='coerce'`` or non-convertible with ``errors='ignore'`` (:issue:`11758`, :issue:`13052`)

Previous behaviour:

Expand Down
10 changes: 10 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ def test_to_datetime_unit(self):
seconds=t) for t in range(20)] + [NaT])
assert_series_equal(result, expected)

result = to_datetime([1, 2, 'NaT', pd.NaT, np.nan], unit='D')
expected = DatetimeIndex([Timestamp('1970-01-02'),
Timestamp('1970-01-03')] + ['NaT'] * 3)
tm.assert_index_equal(result, expected)

with self.assertRaises(ValueError):
to_datetime([1, 2, 'foo'], unit='D')
with self.assertRaises(ValueError):
to_datetime([1, 2, 111111111], unit='D')

def test_series_ctor_datetime64(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50', freq='10s')
dates = np.asarray(rng)
Expand Down
55 changes: 27 additions & 28 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
ndarray[float64_t] fvalues
ndarray mask
bint is_ignore=errors=='ignore', is_coerce=errors=='coerce', is_raise=errors=='raise'
bint need_to_iterate=True
ndarray[int64_t] iresult
ndarray[object] oresult

Expand All @@ -2006,33 +2007,28 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):

if is_raise:

# we can simply raise if there is a conversion
# issue; but we need to mask the nulls
# we need to guard against out-of-range conversions
# to i8
# try a quick conversion to i8
# if we have nulls that are not type-compat
# then need to iterate
try:
iresult = values.astype('i8')
mask = iresult == iNaT
iresult[mask] = 0
fvalues = iresult.astype('f8') * m
need_to_iterate=False
except:
pass

# we have nulls embedded
from pandas import isnull

values = values.astype('object')
mask = isnull(values)
values[mask] = 0
iresult = values.astype('i8')
# check the bounds
if not need_to_iterate:

fvalues = iresult.astype('f8') * m
if (fvalues < _NS_LOWER_BOUND).any() or (fvalues > _NS_UPPER_BOUND).any():
raise ValueError("cannot convert input with unit: {0}".format(unit))
result = (values*m).astype('M8[ns]')
iresult = result.view('i8')
iresult[mask] = iNaT
return result
if (fvalues < _NS_LOWER_BOUND).any() or (fvalues > _NS_UPPER_BOUND).any():
raise ValueError("cannot convert input with unit: {0}".format(unit))
result = (iresult*m).astype('M8[ns]')
iresult = result.view('i8')
iresult[mask] = iNaT
return result

# coerce or ignore
result = np.empty(n, dtype='M8[ns]')
iresult = result.view('i8')

Expand All @@ -2051,7 +2047,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
try:
iresult[i] = cast_from_unit(val, unit)
except:
if is_ignore:
if is_ignore or is_raise:
raise
iresult[i] = NPY_NAT

Expand All @@ -2063,24 +2059,27 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
try:
iresult[i] = cast_from_unit(float(val), unit)
except:
if is_ignore:
if is_ignore or is_raise:
raise
iresult[i] = NPY_NAT

else:

if is_ignore:
raise Exception
if is_ignore or is_raise:
raise ValueError
iresult[i] = NPY_NAT

return result

except:
pass
except (OverflowError, ValueError) as e:

# we cannot process and are done
if is_raise:
raise ValueError("cannot convert input with the unit: {0}".format(unit))

# we have hit an exception
# and are in ignore mode
# redo as object
# we have hit an exception
# and are in ignore mode
# redo as object

oresult = np.empty(n, dtype=object)
for i in range(n):
Expand Down