Skip to content

Commit

Permalink
TST: resolve issues with test_constructor_dtype_datetime64 (#24868)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and jreback committed Mar 13, 2019
1 parent 58a4151 commit 69ae24b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
8 changes: 6 additions & 2 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
49 changes: 38 additions & 11 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 69ae24b

Please sign in to comment.