From 437603602c0195499645c3b7c5a70200653fee15 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 21 Jan 2019 19:06:36 +0000 Subject: [PATCH 1/7] np bug no more --- pandas/tests/series/test_constructors.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index d92ca48751d0a..098c56a5504e8 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -688,10 +688,15 @@ def test_constructor_dtype_datetime64(self): pytest.raises(TypeError, lambda x: Series( Series(dates).astype('int') / 1000000, dtype='M8[ms]')) - msg = (r"The 'datetime64' dtype has no unit\. Please pass in" - r" 'datetime64\[ns\]' instead\.") - with pytest.raises(ValueError, match=msg): - Series(dates, dtype='datetime64') + dates_not_np = [ + datetime(2013, 1, 1), + datetime(2013, 1, 2), + datetime(2013, 1, 3), + ] + + expected = Series(dates_not_np) + result = Series(dates, dtype='datetime64[ns]') + tm.assert_series_equal(result, expected) # invalid dates can be help as object result = Series([datetime(2, 1, 1)]) From 5418c759965ee6edb507eb46e9c271fe0ba14138 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 21 Jan 2019 21:07:41 +0000 Subject: [PATCH 2/7] add np.nan case --- pandas/tests/series/test_constructors.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 098c56a5504e8..8073bad73153c 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -688,16 +688,22 @@ def test_constructor_dtype_datetime64(self): pytest.raises(TypeError, lambda x: Series( Series(dates).astype('int') / 1000000, dtype='M8[ms]')) - dates_not_np = [ + expected = Series([ datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3), - ] - - expected = Series(dates_not_np) + ]) result = Series(dates, dtype='datetime64[ns]') tm.assert_series_equal(result, expected) + expected = Series([ + pd.NaT, + datetime(2013, 1, 2), + datetime(2013, 1, 3), + ]) + result = Series([np.nan] + dates[1:], dtype='datetime64[ns]') + tm.assert_series_equal(result, expected) + # invalid dates can be help as object result = Series([datetime(2, 1, 1)]) assert result[0] == datetime(2, 1, 1, 0, 0) From b31f17193ce0d6b873faaf5e948996fb052676ae Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Mon, 21 Jan 2019 21:58:25 +0000 Subject: [PATCH 3/7] update test_astype_generic_timestamp_no_frequency --- pandas/tests/series/test_dtypes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index e29974f56967f..68a8d1ca30f56 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -415,7 +415,9 @@ def test_astype_generic_timestamp_no_frequency(self, dtype): data = [1] s = Series(data) - msg = "dtype has no unit. Please pass in" + msg = ((r"The '{dtype}' dtype has no unit\. " + r"Please pass in '{dtype}\[ns\]' instead.") + .format(dtype=dtype.__name__)) with pytest.raises(ValueError, match=msg): s.astype(dtype) From 57962f754347d58f96da8badd2574c456f7e5d01 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 12 Mar 2019 20:28:58 +0000 Subject: [PATCH 4/7] troubleshoot - results of ci no longer available --- pandas/tests/series/test_constructors.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 1823c288c37fb..11e1ae323c133 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -683,12 +683,10 @@ def test_constructor_dtype_datetime64(self): assert s.dtype == 'M8[ns]' # GH3414 related - # msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" - # r" \[int32\]") - # with pytest.raises(TypeError, match=msg): - # Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]') - pytest.raises(TypeError, lambda x: Series( - Series(dates).astype('int') / 1000000, dtype='M8[ms]')) + msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" + r" \[int32\]") + with pytest.raises(TypeError, match=msg): + Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]') expected = Series([ datetime(2013, 1, 1), From 89e8758624d18fc9d39341cd701921a060933868 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 12 Mar 2019 21:23:15 +0000 Subject: [PATCH 5/7] astype('int') fails on windows --- pandas/tests/series/test_constructors.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 11e1ae323c133..c0deb172095a1 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -683,16 +683,16 @@ def test_constructor_dtype_datetime64(self): assert s.dtype == 'M8[ns]' # GH3414 related - msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" - r" \[int32\]") - with pytest.raises(TypeError, match=msg): - Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]') - expected = Series([ datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3), - ]) + ], dtype='datetime64[ns]') + + result = Series( + Series(dates).astype(np.intp) / 1000000, dtype='M8[ms]') + tm.assert_series_equal(result, expected) + result = Series(dates, dtype='datetime64[ns]') tm.assert_series_equal(result, expected) @@ -700,7 +700,7 @@ def test_constructor_dtype_datetime64(self): pd.NaT, datetime(2013, 1, 2), datetime(2013, 1, 3), - ]) + ], dtype='datetime64[ns]') result = Series([np.nan] + dates[1:], dtype='datetime64[ns]') tm.assert_series_equal(result, expected) From 968f0488b1be9792692e7b448bf50282395af238 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 13 Mar 2019 13:12:44 +0000 Subject: [PATCH 6/7] add astype exception message checks --- pandas/tests/frame/test_dtypes.py | 6 +++++- pandas/tests/series/test_constructors.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index b37bf02a6b8e7..e21a3bd0adbf9 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -808,9 +808,13 @@ def test_astype_to_incorrect_datetimelike(self, unit): other = "m8[{}]".format(unit) df = DataFrame(np.array([[1, 2, 3]], dtype=dtype)) - with pytest.raises(TypeError): + msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" + r" \[timedelta64\[{}\]\]").format(unit) + with pytest.raises(TypeError, match=msg): df.astype(other) + msg = (r"cannot astype a datetimelike from \[timedelta64\[ns\]\] to" + r" \[datetime64\[{}\]\]").format(unit) df = DataFrame(np.array([[1, 2, 3]], dtype=other)) with pytest.raises(TypeError): df.astype(dtype) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index c0deb172095a1..96e18c6a60cac 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -690,7 +690,7 @@ def test_constructor_dtype_datetime64(self): ], dtype='datetime64[ns]') result = Series( - Series(dates).astype(np.intp) / 1000000, dtype='M8[ms]') + Series(dates).astype(np.int64) / 1000000, dtype='M8[ms]') tm.assert_series_equal(result, expected) result = Series(dates, dtype='datetime64[ns]') @@ -704,6 +704,24 @@ def test_constructor_dtype_datetime64(self): result = Series([np.nan] + dates[1:], dtype='datetime64[ns]') tm.assert_series_equal(result, expected) + dts = Series(dates, dtype='datetime64[ns]') + + # valid astype + dts.astype('int64') + + # invalid casting + msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to" + r" \[int32\]") + with pytest.raises(TypeError, match=msg): + dts.astype('int32') + + # ints are ok + # we test with np.int64 to get similar results on + # windows / 32-bit platforms + result = Series(dts, dtype=np.int64) + expected = Series(dts.astype(np.int64)) + tm.assert_series_equal(result, expected) + # invalid dates can be help as object result = Series([datetime(2, 1, 1)]) assert result[0] == datetime(2, 1, 1, 0, 0) From 6425dcd56772a7f5529ebfdfb31dd639ca20198c Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 13 Mar 2019 13:15:59 +0000 Subject: [PATCH 7/7] fix msg in test_astype_to_incorrect_datetimelike --- pandas/tests/frame/test_dtypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index e21a3bd0adbf9..ca54993712439 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -813,10 +813,10 @@ def test_astype_to_incorrect_datetimelike(self, unit): with pytest.raises(TypeError, match=msg): df.astype(other) - msg = (r"cannot astype a datetimelike from \[timedelta64\[ns\]\] to" + msg = (r"cannot astype a timedelta from \[timedelta64\[ns\]\] to" r" \[datetime64\[{}\]\]").format(unit) df = DataFrame(np.array([[1, 2, 3]], dtype=other)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): df.astype(dtype) def test_timedeltas(self):