Skip to content

Commit 69ae24b

Browse files
simonjayhawkinsjreback
authored andcommitted
TST: resolve issues with test_constructor_dtype_datetime64 (#24868)
1 parent 58a4151 commit 69ae24b

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

pandas/tests/frame/test_dtypes.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,15 @@ def test_astype_to_incorrect_datetimelike(self, unit):
808808
other = "m8[{}]".format(unit)
809809

810810
df = DataFrame(np.array([[1, 2, 3]], dtype=dtype))
811-
with pytest.raises(TypeError):
811+
msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to"
812+
r" \[timedelta64\[{}\]\]").format(unit)
813+
with pytest.raises(TypeError, match=msg):
812814
df.astype(other)
813815

816+
msg = (r"cannot astype a timedelta from \[timedelta64\[ns\]\] to"
817+
r" \[datetime64\[{}\]\]").format(unit)
814818
df = DataFrame(np.array([[1, 2, 3]], dtype=other))
815-
with pytest.raises(TypeError):
819+
with pytest.raises(TypeError, match=msg):
816820
df.astype(dtype)
817821

818822
def test_timedeltas(self):

pandas/tests/series/test_constructors.py

+38-11
Original file line numberDiff line numberDiff line change
@@ -683,17 +683,44 @@ def test_constructor_dtype_datetime64(self):
683683
assert s.dtype == 'M8[ns]'
684684

685685
# GH3414 related
686-
# msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to"
687-
# r" \[int32\]")
688-
# with pytest.raises(TypeError, match=msg):
689-
# Series(Series(dates).astype('int') / 1000000, dtype='M8[ms]')
690-
pytest.raises(TypeError, lambda x: Series(
691-
Series(dates).astype('int') / 1000000, dtype='M8[ms]'))
692-
693-
msg = (r"The 'datetime64' dtype has no unit\. Please pass in"
694-
r" 'datetime64\[ns\]' instead\.")
695-
with pytest.raises(ValueError, match=msg):
696-
Series(dates, dtype='datetime64')
686+
expected = Series([
687+
datetime(2013, 1, 1),
688+
datetime(2013, 1, 2),
689+
datetime(2013, 1, 3),
690+
], dtype='datetime64[ns]')
691+
692+
result = Series(
693+
Series(dates).astype(np.int64) / 1000000, dtype='M8[ms]')
694+
tm.assert_series_equal(result, expected)
695+
696+
result = Series(dates, dtype='datetime64[ns]')
697+
tm.assert_series_equal(result, expected)
698+
699+
expected = Series([
700+
pd.NaT,
701+
datetime(2013, 1, 2),
702+
datetime(2013, 1, 3),
703+
], dtype='datetime64[ns]')
704+
result = Series([np.nan] + dates[1:], dtype='datetime64[ns]')
705+
tm.assert_series_equal(result, expected)
706+
707+
dts = Series(dates, dtype='datetime64[ns]')
708+
709+
# valid astype
710+
dts.astype('int64')
711+
712+
# invalid casting
713+
msg = (r"cannot astype a datetimelike from \[datetime64\[ns\]\] to"
714+
r" \[int32\]")
715+
with pytest.raises(TypeError, match=msg):
716+
dts.astype('int32')
717+
718+
# ints are ok
719+
# we test with np.int64 to get similar results on
720+
# windows / 32-bit platforms
721+
result = Series(dts, dtype=np.int64)
722+
expected = Series(dts.astype(np.int64))
723+
tm.assert_series_equal(result, expected)
697724

698725
# invalid dates can be help as object
699726
result = Series([datetime(2, 1, 1)])

pandas/tests/series/test_dtypes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,9 @@ def test_astype_generic_timestamp_no_frequency(self, dtype):
415415
data = [1]
416416
s = Series(data)
417417

418-
msg = "dtype has no unit. Please pass in"
418+
msg = ((r"The '{dtype}' dtype has no unit\. "
419+
r"Please pass in '{dtype}\[ns\]' instead.")
420+
.format(dtype=dtype.__name__))
419421
with pytest.raises(ValueError, match=msg):
420422
s.astype(dtype)
421423

0 commit comments

Comments
 (0)