Skip to content

Commit ae7674b

Browse files
committed
complex 64 and 128 testing
1 parent 737ea96 commit ae7674b

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Other enhancements
9292
- :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`)
9393
- :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`42273`)
9494
- :meth:`.GroupBy.cummin` and :meth:`.GroupBy.cummax` now support the argument ``skipna`` (:issue:`34047`)
95-
-
95+
- Methods that relied on hashmap based algos such as :meth:`DataFrameGroupBy.value_counts`, :meth:`DataFrameGroupBy.count` and :func:`factorize` ignored imaginary component for complex numbers (:issue:`17927`)
9696

9797
.. ---------------------------------------------------------------------------
9898

pandas/tests/groupby/test_groupby.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,36 @@ def test_groupby_complex():
10091009
tm.assert_series_equal(result, expected)
10101010

10111011

1012+
@pytest.mark.parametrize(
1013+
"frame,expected",
1014+
[
1015+
(
1016+
DataFrame(
1017+
[
1018+
{"a": 1, "b": 1 + 1j},
1019+
{"a": 1, "b": 1 + 2j},
1020+
{"a": 4, "b": 1},
1021+
]
1022+
),
1023+
DataFrame(
1024+
np.array([1, 1, 1], dtype=np.int64),
1025+
index=Index([(1 + 1j), (1 + 2j), (1 + 0j)], dtype="object", name="b"),
1026+
columns=Index(["a"], dtype="object"),
1027+
),
1028+
)
1029+
],
1030+
)
1031+
def test_groupby_complex_numbers(frame, expected):
1032+
result = frame.groupby("b", sort=False).count()
1033+
tm.assert_frame_equal(result, expected)
1034+
1035+
# Sorted by the magnitude of the complex numbers
1036+
# Complex Index dtype is cast to object
1037+
expected.index = Index([(1 + 0j), (1 + 1j), (1 + 2j)], dtype="object", name="b")
1038+
result = frame.groupby("b", sort=True).count()
1039+
tm.assert_frame_equal(result, expected)
1040+
1041+
10121042
def test_groupby_series_indexed_differently():
10131043
s1 = Series(
10141044
[5.0, -9.0, 4.0, 100.0, -5.0, 55.0, 6.7],

pandas/tests/series/methods/test_value_counts.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,25 +209,15 @@ def test_value_counts_bool_with_nan(self, ser, dropna, exp):
209209
tm.assert_series_equal(out, exp)
210210

211211
@pytest.mark.parametrize(
212-
"input_array,expected,dtype",
212+
"input_array,expected",
213213
[
214214
(
215-
[1 + 1j, 0, 1 + 0j, 1j, 1 + 2j],
216-
Series([1, 1, 1, 1, 1], index=[0, 1 + 0j, 1j, 1 + 1j, 1 + 2j]),
217-
np.complex128
218-
),
219-
(
220-
[1 + 2j, 0, 1j, 1, 1j, 1 + 1j, 1 + 1j],
221-
Series([2, 1, 1, 2, 1], index=pd.Index([1j, 0, 1, 1 + 1j, 1 + 2j], dtype=np.complex128)),
222-
np.complex128
223-
),
224-
(
225-
[1 + 2j, 0, 1j, 1, 1j, 1 + 1j, 1 + 1j],
226-
Series([2, 1, 1, 2, 1], index=pd.Index([1j, 0, 1, 1 + 1j, 1 + 2j], dtype=np.complex64)),
227-
np.complex64
228-
),
215+
[1 + 1j, 1 + 1j, 1, 3j, 3j, 3j],
216+
Series([3, 2, 1], index=pd.Index([3j, 1 + 1j, 1])),
217+
)
229218
],
230219
)
231-
def test_value_counts_complex_numbers(self, input_array, expected, dtype):
232-
result = pd.Series(input_array, dtype=dtype).value_counts()
233-
tm.assert_series_equal(result, expected)
220+
def test_value_counts_complex_numbers(self, input_array, expected):
221+
# Complex Index dtype is cast to object
222+
result = Series(input_array).value_counts()
223+
tm.assert_series_equal(result, expected, check_index_type=False)

0 commit comments

Comments
 (0)