Repository: pandas-dev/pandas
Pull request: TST: Add regression test for Series dropping uint datatype (#18311)
- [x] closes #18311
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`


Problem statement:
Assignment to uint series drops datatype
Assigning a uint value to a slice of a uint series results in a dataframe of type int.

```python
s = pd.Series([1,2,3], dtype='uint64') # Also happens with other uint dtypes
s.loc[0] = 4
s
```
With Pandas 0.19.2, this returns:
```
0    4
1    2
2    3
dtype: uint64
```

With version 0.21.0, the dtype gets lost and replaced with int64:
```
0    4
1    2
2    3
dtype: int64
```

Hints from issue discussion:
Yeah, `uint64` should get better treatment than that!  Feel free to do a `git bisect` to determine where the bug lies in this case.
`git bisect` gives me 4efe6560e07f28de6a1834fa90e31cef31b0fb18 as the first bad commit, and unfortunately, it's a big one.
Looks to work on master now. Could use a test:

```
In [70]: s
Out[70]:
0    4
1    2
2    3
dtype: uint64

In [71]: pd.__version__
Out[71]: '0.26.0.dev0+576.gde67bb72e'
```

Commit message: TST: Add regression test for Series dropping uint datatype (#18311)

Patch to review:
diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py
index 6ee120f3bec64..a6309d20d180d 100644
--- a/pandas/tests/series/test_dtypes.py
+++ b/pandas/tests/series/test_dtypes.py
@@ -518,3 +518,9 @@ def test_values_compatibility(self, data):
         result = pd.Series(data).values
         expected = np.array(data.astype(object))
         tm.assert_numpy_array_equal(result, expected)
+
+    def test_uint_drop(self):
+        for type in ["uint8", "uint16", "uint32", "uint64"]:
+            series = pd.Series([1, 2, 3], dtype=type)
+            series.loc[0] = 4
+            assert series.dtype == type


Code review comments:
diff_hunk: @@ -518,3 +518,9 @@ def test_values_compatibility(self, data):
         result = pd.Series(data).values
         expected = np.array(data.astype(object))
         tm.assert_numpy_array_equal(result, expected)
+
+    def test_uint_drop(self):
+        for type in ["uint8", "uint16", "uint32", "uint64"]:
+            series = pd.Series([1, 2, 3], dtype=type)
+            series.loc[0] = 4
+            assert series.dtype == type
path: pandas/tests/series/test_dtypes.py
text: @user1:
create the expected series and user tm.assert_series_equal(series, expected)

diff_hunk: @@ -518,3 +518,9 @@ def test_values_compatibility(self, data):
         result = pd.Series(data).values
         expected = np.array(data.astype(object))
         tm.assert_numpy_array_equal(result, expected)
+
+    def test_uint_drop(self):
+        for type in ["uint8", "uint16", "uint32", "uint64"]:
path: pandas/tests/series/test_dtypes.py
text: @user1:
Could you:

  1. Parameterize this instead of doing a loop within the function body
  2. Change `type` to `type_` so as not to shadow the builtin
  3. Add the issue number as a comment?

@user2:
you can pop in ``uint_dtype`` to the argument and it will just work

diff_hunk: @@ -518,3 +518,9 @@ def test_values_compatibility(self, data):
         result = pd.Series(data).values
         expected = np.array(data.astype(object))
         tm.assert_numpy_array_equal(result, expected)
+
+    def test_uint_drop(self):
path: pandas/tests/series/test_dtypes.py
text: @user1:
move to pandas/series/test_indexing.py