Skip to content
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

BUG: bytes of special nan value are lost after pd.concat #51675

Open
3 tasks done
boxblox opened this issue Feb 27, 2023 · 2 comments
Open
3 tasks done

BUG: bytes of special nan value are lost after pd.concat #51675

boxblox opened this issue Feb 27, 2023 · 2 comments
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode Strings String extension data type and string data

Comments

@boxblox
Copy link

boxblox commented Feb 27, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd
import struct

def test_nans(arr):
    special_nan = "fffffffffffffffe"
    boo = []
    for i in arr:
        boo.append(bytes(struct.pack(">d", i)).hex() == special_nan)
    return boo


diff_nans = [struct.unpack(">d", bytes.fromhex("fffffffffffffffe"))[0], float("nan")]
print(f"nan list: {diff_nans}")
print(f"test for special nan: {test_nans(diff_nans)}")

a = np.array(diff_nans, dtype=np.float64)
e = np.array([], dtype=np.float64)
print(f"test for special nan after np.concat: {test_nans(np.concatenate((a, e)))}")

df = pd.DataFrame(a)
dfe = pd.DataFrame([], columns=[0], dtype=np.float64)

print(f"test for special nan in original df: {test_nans(df[0])}")
print(f"test for special nan after pd.concat: {test_nans(pd.concat([df, dfe])[0])}")

Issue Description

we use special nan values to hold information. it appears that pd.concat destroys the original byte signature of a special nan value. Perhaps this is expected behavior, but could not find any mention of this in pandas docs or with google. Could use some sage advice. This appears in 1.5.2 and 2.0.0rc0.

Expected Behavior

I would have expected the pd.concat function to maintain the right bytes (as numpy.concatenate seems to do).

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.10.9.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.10.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : 1.4.46
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : 0.19.0
tzdata : None

@boxblox boxblox added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 27, 2023
@jbrockmendel
Copy link
Member

Nothing in pandas distinguishes between different variants of float NaN. The concat code has a couple of paths that check for all-nan arrays and length-zero arrays and sometimes constructs new arrays rather than explicitly call np.concatenate. Without looking more closely I don't have a guess which path this goes down.

Doing the concatenation in numpy before constructing a DataFrame seems to retain your sentinel and off the top of my head I can't think of other places it would get discarded.

@boxblox
Copy link
Author

boxblox commented Feb 28, 2023

yes, I see. Doing more experiments, seems like this behavior has more around it than just nan-only arrays (which is where we were first looking)

special nan is lost here:

special_nan = struct.unpack(">d", bytes.fromhex("fffffffffffffffe"))[0]
arr = [special_nan, float("nan")]
arr2 = [1.0, 1.0]
df1 = pd.DataFrame(arr)
df2 = pd.DataFrame(arr2)
print(f"test for special nan after pd.concat: {test_nans(pd.concat([df1, df2])[0])}")

special nan is NOT lost here (just change arr2 to be ints instead of floats)

special_nan = struct.unpack(">d", bytes.fromhex("fffffffffffffffe"))[0]
arr = [special_nan, float("nan")]
arr2 = [1, 1]
df1 = pd.DataFrame(arr)
df2 = pd.DataFrame(arr2)
print(f"test for special nan after pd.concat: {test_nans(pd.concat([df1, df2])[0])}")

special nan is NOT lost here (note arr is no longer nan only):

special_nan = struct.unpack(">d", bytes.fromhex("fffffffffffffffe"))[0]
arr = [special_nan, float("nan"), 1]
arr2 = [1, 1]
df1 = pd.DataFrame(arr)
df2 = pd.DataFrame(arr2)
print(f"test for special nan after pd.concat: {test_nans(pd.concat([df1, df2])[0])}")

@mroeschke mroeschke added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Strings String extension data type and string data and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

3 participants