diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index b37bf02a6b8e7..ca54993712439 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -808,11 +808,15 @@ 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 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): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 8525b877618c9..96e18c6a60cac 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -683,17 +683,44 @@ 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"The 'datetime64' dtype has no unit\. Please pass in" - r" 'datetime64\[ns\]' instead\.") - with pytest.raises(ValueError, match=msg): - Series(dates, dtype='datetime64') + expected = Series([ + datetime(2013, 1, 1), + datetime(2013, 1, 2), + datetime(2013, 1, 3), + ], dtype='datetime64[ns]') + + result = Series( + Series(dates).astype(np.int64) / 1000000, dtype='M8[ms]') + tm.assert_series_equal(result, expected) + + result = Series(dates, dtype='datetime64[ns]') + tm.assert_series_equal(result, expected) + + expected = Series([ + 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) + + 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)]) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index d8046c4944afc..735b8553b14d3 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)