Skip to content

TST: add test for .unique() dtype preserving #29515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
from pandas.compat.numpy import np_array_datetime64_compat
import pandas.util._test_decorators as td

from pandas.core.dtypes.common import (
is_bool_dtype,
is_complex_dtype,
is_float_dtype,
is_integer_dtype,
is_object_dtype,
)
from pandas.core.dtypes.dtypes import CategoricalDtype as CDT

import pandas as pd
Expand All @@ -23,6 +30,7 @@
Timestamp,
compat,
)
from pandas.conftest import BYTES_DTYPES, STRING_DTYPES
import pandas.core.algorithms as algos
from pandas.core.arrays import DatetimeArray
import pandas.core.common as com
Expand Down Expand Up @@ -353,6 +361,35 @@ def test_on_index_object(self):

tm.assert_almost_equal(result, expected)

def test_dtype_preservation(self, any_numpy_dtype):
# GH 15442
if any_numpy_dtype in (BYTES_DTYPES + STRING_DTYPES):
pytest.skip("skip string dtype")
elif is_integer_dtype(any_numpy_dtype):
data = [1, 2, 2]
uniques = [1, 2]
elif is_float_dtype(any_numpy_dtype):
data = [1, 2, 2]
uniques = [1.0, 2.0]
elif is_complex_dtype(any_numpy_dtype):
data = [complex(1, 0), complex(2, 0), complex(2, 0)]
uniques = [complex(1, 0), complex(2, 0)]
elif is_bool_dtype(any_numpy_dtype):
data = [True, True, False]
uniques = [True, False]
elif is_object_dtype(any_numpy_dtype):
data = ["A", "B", "B"]
uniques = ["A", "B"]
else:
# datetime64[ns]/M8[ns]/timedelta64[ns]/m8[ns] tested elsewhere
data = [1, 2, 2]
uniques = [1, 2]

result = Series(data, dtype=any_numpy_dtype).unique()
expected = np.array(uniques, dtype=any_numpy_dtype)

tm.assert_numpy_array_equal(result, expected)

def test_datetime64_dtype_array_returned(self):
# GH 9431
expected = np_array_datetime64_compat(
Expand Down