Repository: pandas-dev/pandas
Pull request: Add tests for NaT when performing dt.to_period

@mroeschke Here is a test that tests the functionality discussed in the issue. Please let me know if it needs to be done differently and if more tests should be added.

- [x] closes #21205 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`


Problem statement:
BUG: Series.dt.to_period returns wrong dtype on all-NaTs 
#### Code Sample, a copy-pastable example if possible

Input:

```python
print(pd.to_datetime(['2001']).to_series().dt.to_period('D').dtype)
print(pd.to_datetime(['NaT']).to_series().dt.to_period('D').dtype)
```

Output:

```python
object
datetime64[ns]
```

#### Problem description

Calling `.dt.to_period` on an all-NaT series will return datetimes, not periods. This can easily cause programs to crash on unexpected inputs.

#### Expected Output

```python
object
object
```

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.14-200.fc26.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: C
LOCALE: None.None

pandas: 0.23.0
pytest: 3.5.1
pip: 10.0.1
setuptools: 39.1.0
Cython: 0.28.2
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 4.2.1
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.2
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: 0.1.5
pandas_gbq: None
pandas_datareader: None
</details>

Hints from issue discussion:
This looks fixed on master (as Series now supports period types). Could use a test.

```
In [40]: print(pd.to_datetime(['2001']).to_series().dt.to_period('D').dtype)
    ...: print(pd.to_datetime(['NaT']).to_series().dt.to_period('D').dtype)
period[D]
period[D]

In [41]: pd.__version__
Out[41]: '0.24.0rc1+1.g33f91d8f9'
```

Commit message: Add tests for NaT when performing dt.to_period

Patch to review:
diff --git a/pandas/tests/series/test_period.py b/pandas/tests/series/test_period.py
index 0a86bb0b67797..58a55a7142e69 100644
--- a/pandas/tests/series/test_period.py
+++ b/pandas/tests/series/test_period.py
@@ -164,3 +164,10 @@ def test_end_time_timevalues(self, input_vals):
         result = s.dt.end_time
         expected = s.apply(lambda x: x.end_time)
         tm.assert_series_equal(result, expected)
+
+    def test_to_period(self):
+        series = pd.Series(['2001'], dtype='datetime64[ns]')
+        assert series.dt.to_period('D').dtype == 'Period[D]'
+
+        series = pd.Series(['NaT'], dtype='datetime64[ns]')
+        assert series.dt.to_period('D').dtype == 'Period[D]'


Code review comments:
diff_hunk: @@ -164,3 +164,10 @@ def test_end_time_timevalues(self, input_vals):
         result = s.dt.end_time
         expected = s.apply(lambda x: x.end_time)
         tm.assert_series_equal(result, expected)
+
+    def test_to_period(self):
+        series = pd.Series(['2001'], dtype='datetime64[ns]')
path: pandas/tests/series/test_period.py
text: @user1:
You can parameterize over `'2001', 'NaT'`

diff_hunk: @@ -164,3 +164,10 @@ def test_end_time_timevalues(self, input_vals):
         result = s.dt.end_time
         expected = s.apply(lambda x: x.end_time)
         tm.assert_series_equal(result, expected)
+
+    def test_to_period(self):
+        series = pd.Series(['2001'], dtype='datetime64[ns]')
+        assert series.dt.to_period('D').dtype == 'Period[D]'
path: pandas/tests/series/test_period.py
text: @user1:
Construct the expected series and compare using `tm.assert_series.equals(result, expected)` so we can test that more than the dtype is correct.