Skip to content
12 changes: 8 additions & 4 deletions pandas/_libs/include/pandas/vendored/klib/khash_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#include <Python.h>


// use numpy's definitions for complex
#include <numpy/arrayobject.h>
typedef npy_complex64 khcomplex64_t;
typedef npy_complex128 khcomplex128_t;
typedef struct {
float real;
float imag;
} khcomplex64_t;
typedef struct {
double real;
double imag;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this change anything on platforms where float/double aren't 32/64 bits? (which i guess we have zero testing for so not a huge deal)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. It appears in numpy/numpy#24085 in numpy/core/include/numpy/npy_math.h numpy also defines real/imag components as doubles

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the misnomer is to call these complex64 and complex128. The C99 standard has float complex and double complex types, but nothing that is a fixed width

See also https://stackoverflow.com/questions/2524737/fixed-size-floating-point-types#2524933

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess technically float32/float64 are sized aliases under the assumption they refer to float/double, but in practice the assumption seems so strong that I doubt it matters.

I am very puzzled about the change though, this is exactly what it was after all, and I don't think C cares about type aliases (even if there was interaction anywhere). I am wondering if the include <numpy/arrayobject.h> somehow is involved, but I don't see why it should be either?

I assume the value stored is correct here and just the printing somehow thinks it is printing a NaN?

} khcomplex128_t;



Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ 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)]
expected = f"{'val': >{len(reprs[0])}}\n" + "\n".join(reprs)
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"\n{str(obj)}\n\n{expected}"