Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: resolve issues with test_constructor_dtype_datetime64 #24868

Merged
merged 10 commits into from
Mar 13, 2019
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm is this not intentional to have around still?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is explicitly tested by test_astype_generic_timestamp_no_frequency. however i've now updated this test so that the error message check is more explicit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd

i'm guessing that the intended error message for this test would have been originally that raised by

raise TypeError("cannot astype a datetimelike from [{from_dtype}] "
"to [{to_dtype}]".format(from_dtype=arr.dtype,
to_dtype=dtype))

and before the conversion to pytest.raises context manager the test was
pytest.raises(TypeError,
lambda x: Series(dates, dtype='datetime64'))

which would have been raising TypeError: missing 1 required positional argument: 'x'

since there was no message check the tested function raising TypeError would have incorrectly been considered a test pass

so this test was never intended to test for The 'datetime64' dtype has no unit message.

hope that makes it clear!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I DO want this test to remain, can you simply change it so it is testing with dtype='datetime64', IOW it raises.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might actually be a duplicate test (if so, then can remove)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I DO want this test to remain, can you simply change it so it is testing with dtype='datetime64', IOW it raises.

This might actually be a duplicate test (if so, then can remove)

testing with dtype='datetime64' is explicitly tested by another test: test_astype_generic_timestamp_no_frequency.

however within this PR i've also changed test_astype_generic_timestamp_no_frequency so that the error message check is more explicit so that it could be removed here.

Copy link
Contributor

@jreback jreback Jan 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, you are going to remove this test? rather, this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not removing... since it now passes, additionally constructed the expected result and compared.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, you are going to remove this test? rather, this case?

so have now added some cases to test_constructor_dtype_datetime64 similar to those in test_constructor_dtype_timedelta64 and also added the exception message check to tests/frame/test_dtypes.py

so could probably remove some of the # GH3414 related stuff

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