Repository: pandas-dev/pandas
Pull request: TST: Add test for setitem preserving period dtype 
- [x] closes #26861
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#code-standards) for how to run them



Problem statement:
Period dtype not recognized for added column 
```python
data = [pd.Period('2002-03','D')]
# 1
pd.DataFrame({'a': data}).a.dtype
# 2
df = pd.DataFrame([])
df['a'] = data
df.a.dtype
```
```python
period[D]

dtype('O')
```
Expected output is the same - `period[D]`. As it is the same for example for `data = [pd.Timestamp('2000-01-01')]` : both `dtype('<M8[ns]')`.

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Linux
OS-release: 5.0.0-16-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 4.5.0
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.7
numpy: 1.16.4
scipy: 1.2.1
pyarrow: None
xarray: 0.12.1
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

</details>

Hints from issue discussion:
Probably a bug, thought I would recommend being explicit about your dtypes where possible,

```python
df['a'] = pd.array(data)  # or `pd.array(data, dtype=pd.PeriodDtype("d"))`
```
This now works, could use a test

Commit message: TST: GH: 26861

Patch to review:
diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py
index 25682330fe19a..08c3b14f49fe4 100644
--- a/pandas/tests/frame/indexing/test_setitem.py
+++ b/pandas/tests/frame/indexing/test_setitem.py
@@ -179,6 +179,19 @@ def test_setitem_with_unaligned_sparse_value(self):
         expected = Series(SparseArray([1, 0, 0]), name="new_column")
         tm.assert_series_equal(df["new_column"], expected)
 
+    def test_setitem_period_preserves_dtype(self):
+        # GH: 26861
+        data = [Period("2003-12", "D")]
+        result = DataFrame([])
+        result["a"] = data
+        result_dtype = result["a"].dtype
+
+        expected = DataFrame({"a": data})
+        expected_dtype = PeriodDtype(freq="D")
+
+        assert result_dtype == expected_dtype
+        tm.assert_frame_equal(result, expected)
+
     def test_setitem_dict_preserves_dtypes(self):
         # https://github.com/pandas-dev/pandas/issues/34573
         expected = DataFrame(


Code review comments:
diff_hunk: @@ -179,6 +179,19 @@ def test_setitem_with_unaligned_sparse_value(self):
         expected = Series(SparseArray([1, 0, 0]), name="new_column")
         tm.assert_series_equal(df["new_column"], expected)
 
+    def test_setitem_period_preserves_dtype(self):
+        # GH: 26861
+        data = [Period("2003-12", "D")]
+        result = DataFrame([])
+        result["a"] = data
+        result_dtype = result["a"].dtype
+
+        expected = DataFrame({"a": data})
+        expected_dtype = PeriodDtype(freq="D")
+
+        assert result_dtype == expected_dtype
path: pandas/tests/frame/indexing/test_setitem.py
text: @user1:
This shouldn't be needed. `tm.assert_frame_equal` will also check the dtypes of `result` and `expected`

@author:
Thanks, I've removed the dtype check