Repository: pandas-dev/pandas
Pull request: Add simple test for GH 28448
- [x] closes #28448
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Problem statement:
Can't convert `Categorical` to 'datetime64[ns, MET]', but can do it with Series
#### Code Sample, a copy-pastable example if possible

```python
from pandas import Categorical

Categorical(['2015-01-01']).astype('datetime64[ns]')  # works
Categorical(['2015-01-01']).astype('datetime64[ns, MET]')  # fails
```

This is because the `astype` method returns
```
       np.array(self, dtype=dtype, copy=copy)
```
, and `numpy` arrays don't handle this dtype.

Is this expected behaviour?

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.3.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.15.0-60-generic
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_GB.UTF-8
LOCALE           : en_GB.UTF-8

pandas           : 0.25.1
numpy            : 1.16.4
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.1.1
setuptools       : 41.0.1
Cython           : None
pytest           : 5.0.1
hypothesis       : None
sphinx           : 2.2.0
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.1
IPython          : 7.8.0
pandas_datareader: None
bs4              : 4.8.0
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.1
numexpr          : 2.7.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 0.13.0
pytables         : None
s3fs             : None
scipy            : 1.2.1
sqlalchemy       : None
tables           : 3.5.2
xarray           : None
xlrd             : 1.2.0
xlwt             : None
xlsxwriter       : None

</details>

Hints from issue discussion:
I think this should be consistent so probably OK to support. If you have a PR you'd like to submit would certainly welcome one
Fixed by #28762
hmm do we have a test for this?

Commit message: Add simple test for GH 28448

Patch to review:
diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py
index e70e4f2fe501b..6143652b3ee47 100644
--- a/pandas/tests/extension/test_categorical.py
+++ b/pandas/tests/extension/test_categorical.py
@@ -223,6 +223,28 @@ def test_cast_category_to_extension_dtype(self, expected):
 
         tm.assert_series_equal(result, expected)
 
+    @pytest.mark.parametrize(
+        "data, dtype, expected",
+        [
+            (
+                "2015-01-01",
+                "datetime64[ns]",
+                np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"),
+            ),
+            (
+                "2015-01-01",
+                "datetime64[ns, MET]",
+                np.array(
+                    [Timestamp("2015-01-01 00:00:00+0100", tz="MET")], dtype=object
+                ),
+            ),
+        ],
+    )
+    def test_consistent_casting(self, data, dtype, expected):
+        # GH 28448
+        result = np.asarray(pd.Categorical(data).astype(dtype))
+        assert result == expected
+
 
 class TestArithmeticOps(base.BaseArithmeticOpsTests):
     def test_arith_series_with_scalar(self, data, all_arithmetic_operators):


Code review comments:
diff_hunk: @@ -223,6 +223,28 @@ def test_cast_category_to_extension_dtype(self, expected):
 
         tm.assert_series_equal(result, expected)
 
+    @pytest.mark.parametrize(
+        "data, dtype, expected",
+        [
+            (
+                "2015-01-01",
path: pandas/tests/extension/test_categorical.py
text: @user1:
Side note - since `data` is always the same no need to parametrize