From 940a4505c1351257e80898f64f740a28326e3212 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 2 Aug 2023 10:05:57 -0700 Subject: [PATCH 1/7] COMPAT: Change khcomplex64/128_t typedefs --- .../include/pandas/vendored/klib/khash_python.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/include/pandas/vendored/klib/khash_python.h b/pandas/_libs/include/pandas/vendored/klib/khash_python.h index 56afea049c1ec..12b61e5270358 100644 --- a/pandas/_libs/include/pandas/vendored/klib/khash_python.h +++ b/pandas/_libs/include/pandas/vendored/klib/khash_python.h @@ -2,10 +2,14 @@ #include -// use numpy's definitions for complex -#include -typedef npy_complex64 khcomplex64_t; -typedef npy_complex128 khcomplex128_t; +typedef struct { + float real; + float imag; + } khcomplex64_t; +typedef struct { + double real; + double imag; + } khcomplex128_t; From c4571d93cd63cd45faab6ff1a91204c1450543c4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:44:57 -0700 Subject: [PATCH 2/7] Spacing, use double --- pandas/_libs/include/pandas/vendored/klib/khash_python.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/include/pandas/vendored/klib/khash_python.h b/pandas/_libs/include/pandas/vendored/klib/khash_python.h index 12b61e5270358..454e3c70f0ad4 100644 --- a/pandas/_libs/include/pandas/vendored/klib/khash_python.h +++ b/pandas/_libs/include/pandas/vendored/klib/khash_python.h @@ -3,13 +3,13 @@ typedef struct { - float real; - float imag; - } khcomplex64_t; + double real; + double imag; +} khcomplex64_t; typedef struct { double real; double imag; - } khcomplex128_t; +} khcomplex128_t; From 1efe3bebc5d9b8168b8b612c5e7ee0c4164a90da Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:07:46 -0700 Subject: [PATCH 3/7] Use double more places --- pandas/_libs/include/pandas/vendored/klib/khash_python.h | 8 ++++---- pandas/_libs/khash.pxd | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/include/pandas/vendored/klib/khash_python.h b/pandas/_libs/include/pandas/vendored/klib/khash_python.h index 454e3c70f0ad4..0311f168dcc4a 100644 --- a/pandas/_libs/include/pandas/vendored/klib/khash_python.h +++ b/pandas/_libs/include/pandas/vendored/klib/khash_python.h @@ -93,9 +93,9 @@ khuint64_t PANDAS_INLINE asuint64(double key) { return val; } -khuint32_t PANDAS_INLINE asuint32(float key) { +khuint32_t PANDAS_INLINE asuint32(double key) { khuint32_t val; - memcpy(&val, &key, sizeof(float)); + memcpy(&val, &key, sizeof(double)); return val; } @@ -115,9 +115,9 @@ khuint32_t PANDAS_INLINE kh_float64_hash_func(double val){ return murmur2_64to32(as_int); } -khuint32_t PANDAS_INLINE kh_float32_hash_func(float val){ +khuint32_t PANDAS_INLINE kh_float32_hash_func(double val){ // 0.0 and -0.0 should have the same hash: - if (val == 0.0f){ + if (val == 0.0){ return ZERO_HASH; } // all nans should have the same hash: diff --git a/pandas/_libs/khash.pxd b/pandas/_libs/khash.pxd index c439e1cca772b..06595f44a591a 100644 --- a/pandas/_libs/khash.pxd +++ b/pandas/_libs/khash.pxd @@ -29,8 +29,8 @@ cdef extern from "pandas/vendored/klib/khash_python.h": "kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil ctypedef struct khcomplex64_t: - float real - float imag + double real + double imag bint are_equivalent_khcomplex64_t \ "kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil From 89091d7d5e6963f4df90b1ea2d9876da1a4edc7b Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:53:53 -0700 Subject: [PATCH 4/7] Go back to old fix --- .../include/pandas/vendored/klib/khash_python.h | 12 ++++++------ pandas/_libs/khash.pxd | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/include/pandas/vendored/klib/khash_python.h b/pandas/_libs/include/pandas/vendored/klib/khash_python.h index 0311f168dcc4a..76b212a4b4660 100644 --- a/pandas/_libs/include/pandas/vendored/klib/khash_python.h +++ b/pandas/_libs/include/pandas/vendored/klib/khash_python.h @@ -3,8 +3,8 @@ typedef struct { - double real; - double imag; + float real; + float imag; } khcomplex64_t; typedef struct { double real; @@ -93,9 +93,9 @@ khuint64_t PANDAS_INLINE asuint64(double key) { return val; } -khuint32_t PANDAS_INLINE asuint32(double key) { +khuint32_t PANDAS_INLINE asuint32(float key) { khuint32_t val; - memcpy(&val, &key, sizeof(double)); + memcpy(&val, &key, sizeof(float)); return val; } @@ -115,9 +115,9 @@ khuint32_t PANDAS_INLINE kh_float64_hash_func(double val){ return murmur2_64to32(as_int); } -khuint32_t PANDAS_INLINE kh_float32_hash_func(double val){ +khuint32_t PANDAS_INLINE kh_float32_hash_func(float val){ // 0.0 and -0.0 should have the same hash: - if (val == 0.0){ + if (val == 0.0f){ return ZERO_HASH; } // all nans should have the same hash: diff --git a/pandas/_libs/khash.pxd b/pandas/_libs/khash.pxd index 06595f44a591a..c439e1cca772b 100644 --- a/pandas/_libs/khash.pxd +++ b/pandas/_libs/khash.pxd @@ -29,8 +29,8 @@ cdef extern from "pandas/vendored/klib/khash_python.h": "kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil ctypedef struct khcomplex64_t: - double real - double imag + float real + float imag bint are_equivalent_khcomplex64_t \ "kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil From dd12989979b71ec1d794f48a0f57cba4e276ffe9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:06:12 -0700 Subject: [PATCH 5/7] Check both, cant repo locally --- pandas/tests/io/formats/test_printing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index d21da4ec93318..5f4a51f012273 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -245,4 +245,4 @@ def test_ser_df_with_complex_nans(data, output, as_frame): else: reprs = [f"{i} {val}" for i, val in enumerate(output)] expected = "\n".join(reprs) + "\ndtype: complex128" - assert str(obj) == expected + assert str(obj) == expected, f"{str(obj)} \n\n {expected}" From a9c6a116379cdcf17615acd3506a26b9b026cc36 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 3 Aug 2023 08:42:53 -0700 Subject: [PATCH 6/7] Rule out pandas inference --- pandas/tests/io/formats/test_printing.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index 5f4a51f012273..af2e96d5d9e5d 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -221,15 +221,15 @@ def test_multiindex_long_element(): ([-1.23j, complex("nan"), -1], ["-0.00-1.23j", " NaN+0.00j", "-1.00+0.00j"]), ([1.23j, complex("nan"), 1.23], [" 0.00+1.23j", " NaN+0.00j", " 1.23+0.00j"]), ( - [-1.23j, complex(np.nan, np.nan), 1], + [-1.23j, complex(float("nan"), float("nan")), 1], ["-0.00-1.23j", " NaN+ NaNj", " 1.00+0.00j"], ), ( - [-1.23j, complex(1.2, np.nan), 1], + [-1.23j, complex(1.2, float("nan")), 1], ["-0.00-1.23j", " 1.20+ NaNj", " 1.00+0.00j"], ), ( - [-1.23j, complex(np.nan, -1.2), 1], + [-1.23j, complex(float("nan"), -1.2), 1], ["-0.00-1.23j", " NaN-1.20j", " 1.00+0.00j"], ), ], @@ -237,7 +237,7 @@ def test_multiindex_long_element(): @pytest.mark.parametrize("as_frame", [True, False]) def test_ser_df_with_complex_nans(data, output, as_frame): # GH#53762, GH#53841 - obj = pd.Series(data) + obj = pd.Series(np.array(data)) if as_frame: obj = obj.to_frame(name="val") reprs = [f"{i} {val}" for i, val in enumerate(output)] From d32fbb5a5f8ab51b0089f65bfd486f74f4abacbd Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 3 Aug 2023 10:37:13 -0700 Subject: [PATCH 7/7] Use np.nan insted of float(nan) --- pandas/tests/io/formats/test_printing.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index af2e96d5d9e5d..2d0dc0d937709 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -221,15 +221,15 @@ def test_multiindex_long_element(): ([-1.23j, complex("nan"), -1], ["-0.00-1.23j", " NaN+0.00j", "-1.00+0.00j"]), ([1.23j, complex("nan"), 1.23], [" 0.00+1.23j", " NaN+0.00j", " 1.23+0.00j"]), ( - [-1.23j, complex(float("nan"), float("nan")), 1], + [-1.23j, complex(np.nan, np.nan), 1], ["-0.00-1.23j", " NaN+ NaNj", " 1.00+0.00j"], ), ( - [-1.23j, complex(1.2, float("nan")), 1], + [-1.23j, complex(1.2, np.nan), 1], ["-0.00-1.23j", " 1.20+ NaNj", " 1.00+0.00j"], ), ( - [-1.23j, complex(float("nan"), -1.2), 1], + [-1.23j, complex(np.nan, -1.2), 1], ["-0.00-1.23j", " NaN-1.20j", " 1.00+0.00j"], ), ], @@ -245,4 +245,4 @@ def test_ser_df_with_complex_nans(data, output, as_frame): else: reprs = [f"{i} {val}" for i, val in enumerate(output)] expected = "\n".join(reprs) + "\ndtype: complex128" - assert str(obj) == expected, f"{str(obj)} \n\n {expected}" + assert str(obj) == expected, f"\n{str(obj)}\n\n{expected}"