From 58d3d196c31b10cb546bc792106bfa79cb71f88b Mon Sep 17 00:00:00 2001 From: Alexey Pakhomchik Date: Thu, 4 Aug 2016 22:23:23 +0300 Subject: [PATCH] BUG: pd.to_datetime doesn't raises AttributeError with specific inputs when errors='ignore'(#12424) --- doc/source/whatsnew/v0.19.0.txt | 1 + pandas/tests/test_algos.py | 6 ++++++ pandas/tslib.pyx | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.19.0.txt b/doc/source/whatsnew/v0.19.0.txt index f68fa957df133..c1e69b0bcae13 100644 --- a/doc/source/whatsnew/v0.19.0.txt +++ b/doc/source/whatsnew/v0.19.0.txt @@ -858,6 +858,7 @@ Bug Fixes - Bug in ``factorize`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13750`) - Bug in ``.set_index`` raises ``AmbiguousTimeError`` if new index contains DST boundary and multi levels (:issue:`12920`) - Bug in ``pd.read_hdf()`` returns incorrect result when a ``DataFrame`` with a ``categorical`` column and a query which doesn't match any values (:issue:`13792`) +- Bug in ``pd.to_datetime()`` raise ``AttributeError`` with NaN and the other string is not valid when errors='ignore' (:issue:`12424`) - Bug in ``Series`` comparison operators when dealing with zero dim NumPy arrays (:issue:`13006`) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 94c67ac7dd61a..490f4fe81ecbd 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -568,6 +568,12 @@ def test_value_counts_datetime_outofbounds(self): exp = pd.Series([3, 2, 1], index=exp_index) tm.assert_series_equal(res, exp) + # GH 12424 + res = pd.to_datetime(pd.Series(['2362-01-01', np.nan]), + errors='ignore') + exp = pd.Series(['2362-01-01', np.nan], dtype=object) + tm.assert_series_equal(res, exp) + def test_categorical(self): s = Series(pd.Categorical(list('aaabbc'))) result = s.value_counts() diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 56a007bfa352c..32b2bf075991b 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -2396,10 +2396,10 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise', # set as nan except if its a NaT if _checknull_with_nat(val): - if val.view('i8') == NPY_NAT: - oresult[i] = NaT - else: + if PyFloat_Check(val): oresult[i] = np.nan + else: + oresult[i] = NaT elif util.is_datetime64_object(val): if get_datetime64_value(val) == NPY_NAT: oresult[i] = NaT