I have a Pandas DataFrame constructed from a dict with a nan (e.g.: float("nan")
). When I use .to_dict
on it, I get a different dict - the nan values is something "else".
Is it possible to know what this new nan value is?
Here is a toy example I created, and a bunch of checks I did:
import numpy as np
import pandas as pd
a_dict = {
"a": (1, 2),
"b": (3, float("nan")),
}
df = pd.DataFrame(a_dict)
print(df.to_dict())
# {'a': {0: 1, 1: 2}, 'b': {0: 3.0, 1: nan}}
# to_dict() gives a different dict:
print(a_dict == a_dict) # True
print(df.to_dict == a_dict) # False
print(df.to_dict()["b"][1]) # nan
print(type(df.to_dict()["b"][1])) # <class 'float'>
print(df.to_dict()["b"][1] == float("nan")) # False
print(df.to_dict()["b"][1] == np.nan) # False
print(df.to_dict()["b"][1] == pd.NA) # False
print(df.to_dict()["b"][1] is None) # False
print(np.isnan(df.to_dict()["b"][1])) # True
print(pd.isna(df.to_dict()["b"][1])) # True
In terms of motivation, this is biting me when I try to create tests using unittest.TestCase.assertEqual
Thanks upfront.
Related but didn't help: