Skip to content

Commit 21f7003

Browse files
committed
BUG: unique() should preserve the dtype of the input
1 parent 9fd432b commit 21f7003

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

doc/source/whatsnew/v0.25.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ ExtensionArray
156156
Other
157157
^^^^^
158158
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when replacing timezone-aware timestamps using a dict-like replacer (:issue:`27720`)
159-
-
159+
- The returned dtype of ::func:`pd.unique` now matches the input dtype. (:issue`#27869`)
160160
-
161161
-
162162

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def unique(values):
404404

405405
table = htable(len(values))
406406
uniques = table.unique(values)
407-
uniques = _reconstruct_data(uniques, dtype, original)
407+
uniques = _reconstruct_data(uniques, original.dtype, original)
408408
return uniques
409409

410410

pandas/tests/test_base.py

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def test_value_counts_unique_nunique(self):
396396
)
397397
else:
398398
tm.assert_numpy_array_equal(result, orig.values)
399+
assert result.dtype == orig.dtype
399400

400401
assert o.nunique() == len(np.unique(o.values))
401402

0 commit comments

Comments
 (0)