Home > Net >  How does pandas to_dict deal with key np.nan and None?
How does pandas to_dict deal with key np.nan and None?

Time:12-06

import pandas as pd
import numpy as np

a = {np.nan: -1, None: 1}

take a look at a:

{nan: -1, None: 1}

if we convert it from Series back to dict:

pd.Series(a).to_dict()

we see:

{nan: -1, nan: 1}

how could Pandas create the dict with two same keys? Well, I guess they are not really the same since np.nan == np.nan evaluates to False. But I can't create it manually with:

b = {np.nan: -1, np.nan: 1}

if we take a look at b it gives:

{nan: 1}

I'm trying to wrap my head around this, maybe I missed something obvious?

For the full code snippet, see image

enter image description here

this also shows that I can keep adding the key np.nan to the dict generated from pandas series to_dcit, but not my manually created ones

CodePudding user response:

import pandas as pd

r={'A':{1:2,2:3,3:4},'B':{1:np.nan,2:44,3:np.nan}} data = pd.(r) A B 1 2 nan 2 3 44 3 4 nan

CodePudding user response:

It has something to do with float("nan") internals, take a look:

In [24]: s = pd.Series({np.nan: -1, None: 1})
    ...: d = s.to_dict()

In [25]: d
Out[25]: {nan: -1, nan: 1}

In [26]: [type(k) for k in d.keys()]
Out[26]: [<class 'float'>, <class 'float'>]

In [27]: d[float("nan")] = 3

In [28]: d
Out[28]: {nan: -1, nan: 1, nan: 3}

In [29]: d[np.nan] = 100

In [30]: d
Out[30]: {nan: -1, nan: 1, nan: 3, nan: 100}

In [31]: d[np.nan] = 1000

In [32]: d
Out[32]: {nan: -1, nan: 1, nan: 3, nan: 1000}
  • Related