|
3 | 3 | import pandas as pd |
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from woodwork.accessor_utils import _is_dask_series, _is_koalas_series |
7 | | -from woodwork.exceptions import TypeConversionError |
| 6 | +from woodwork.accessor_utils import _is_koalas_series |
| 7 | +from woodwork.exceptions import TypeConversionWarning |
8 | 8 | from woodwork.logical_types import ( |
9 | 9 | Boolean, |
10 | 10 | Categorical, |
@@ -141,15 +141,32 @@ def test_datetime_transform(datetimes): |
141 | 141 | assert datetime.datetime_format is not None |
142 | 142 |
|
143 | 143 |
|
144 | | -def test_datetime_conversion_error(sample_series): |
145 | | - if _is_dask_series(sample_series): |
146 | | - pytest.xfail('Dask does not show error until compute is made.') |
147 | | - |
148 | | - dtype = str(sample_series.dtype) |
149 | | - match = f'Error converting datatype for sample_series from type {dtype} to type datetime64[ns]. ' |
150 | | - match += 'Please confirm the underlying data is consistent with logical type Datetime.' |
151 | | - with pytest.raises(TypeConversionError, match=re.escape(match)): |
152 | | - Datetime().transform(sample_series) |
| 144 | +def test_datetime_inference_ambiguous_format(): |
| 145 | + datetime = Datetime() |
| 146 | + dates = pd.Series(["01/01/2017"] * 2 + ["13/12/2017"], name="dates") |
| 147 | + warning = "Some rows in series 'dates' are incompatible with datetime format " \ |
| 148 | + "'%m/%d/%Y' and have been replaced with null values. You may be able " \ |
| 149 | + "to fix this by using an instantiated Datetime logical type with a different " \ |
| 150 | + "format string specified for this column during Woodwork initialization." |
| 151 | + with pytest.warns(TypeConversionWarning, match=warning): |
| 152 | + transformed = datetime.transform(dates) |
| 153 | + assert str(transformed.dtype) == "datetime64[ns]" |
| 154 | + assert transformed[2] is pd.NaT |
| 155 | + assert datetime.datetime_format == "%m/%d/%Y" |
| 156 | + |
| 157 | + |
| 158 | +def test_datetime_coerce_user_format(): |
| 159 | + datetime = Datetime(datetime_format="%m/%d/%Y") |
| 160 | + dates = pd.Series(["01/01/2017"] * 2 + ["13/12/2017"], name="dates") |
| 161 | + warning = "Some rows in series 'dates' are incompatible with datetime format " \ |
| 162 | + "'%m/%d/%Y' and have been replaced with null values. You may be able " \ |
| 163 | + "to fix this by using an instantiated Datetime logical type with a different " \ |
| 164 | + "format string specified for this column during Woodwork initialization." |
| 165 | + with pytest.warns(TypeConversionWarning, match=warning): |
| 166 | + transformed = datetime.transform(dates) |
| 167 | + assert str(transformed.dtype) == "datetime64[ns]" |
| 168 | + assert transformed[2] is pd.NaT |
| 169 | + assert datetime.datetime_format == "%m/%d/%Y" |
153 | 170 |
|
154 | 171 |
|
155 | 172 | def test_ordinal_transform(sample_series): |
|
0 commit comments