|
10 | 10 | from pandas.compat.numpy import np_array_datetime64_compat
|
11 | 11 | import pandas.util._test_decorators as td
|
12 | 12 |
|
| 13 | +from pandas.core.dtypes.common import ( |
| 14 | + is_bool_dtype, |
| 15 | + is_complex_dtype, |
| 16 | + is_float_dtype, |
| 17 | + is_integer_dtype, |
| 18 | + is_object_dtype, |
| 19 | +) |
13 | 20 | from pandas.core.dtypes.dtypes import CategoricalDtype as CDT
|
14 | 21 |
|
15 | 22 | import pandas as pd
|
|
23 | 30 | Timestamp,
|
24 | 31 | compat,
|
25 | 32 | )
|
| 33 | +from pandas.conftest import BYTES_DTYPES, STRING_DTYPES |
26 | 34 | import pandas.core.algorithms as algos
|
27 | 35 | from pandas.core.arrays import DatetimeArray
|
28 | 36 | import pandas.core.common as com
|
@@ -352,6 +360,35 @@ def test_on_index_object(self):
|
352 | 360 |
|
353 | 361 | tm.assert_almost_equal(result, expected)
|
354 | 362 |
|
| 363 | + def test_dtype_preservation(self, any_numpy_dtype): |
| 364 | + # GH 15442 |
| 365 | + if any_numpy_dtype in (BYTES_DTYPES + STRING_DTYPES): |
| 366 | + pytest.skip("skip string dtype") |
| 367 | + elif is_integer_dtype(any_numpy_dtype): |
| 368 | + data = [1, 2, 2] |
| 369 | + uniques = [1, 2] |
| 370 | + elif is_float_dtype(any_numpy_dtype): |
| 371 | + data = [1, 2, 2] |
| 372 | + uniques = [1.0, 2.0] |
| 373 | + elif is_complex_dtype(any_numpy_dtype): |
| 374 | + data = [complex(1, 0), complex(2, 0), complex(2, 0)] |
| 375 | + uniques = [complex(1, 0), complex(2, 0)] |
| 376 | + elif is_bool_dtype(any_numpy_dtype): |
| 377 | + data = [True, True, False] |
| 378 | + uniques = [True, False] |
| 379 | + elif is_object_dtype(any_numpy_dtype): |
| 380 | + data = ["A", "B", "B"] |
| 381 | + uniques = ["A", "B"] |
| 382 | + else: |
| 383 | + # datetime64[ns]/M8[ns]/timedelta64[ns]/m8[ns] tested elsewhere |
| 384 | + data = [1, 2, 2] |
| 385 | + uniques = [1, 2] |
| 386 | + |
| 387 | + result = Series(data, dtype=any_numpy_dtype).unique() |
| 388 | + expected = np.array(uniques, dtype=any_numpy_dtype) |
| 389 | + |
| 390 | + tm.assert_numpy_array_equal(result, expected) |
| 391 | + |
355 | 392 | def test_datetime64_dtype_array_returned(self):
|
356 | 393 | # GH 9431
|
357 | 394 | expected = np_array_datetime64_compat(
|
|
0 commit comments