Retrieving a dataframe previously stored in a HDFStore with a row label "nan" (string) is incorrectly retrieved as NaN.

My blog talks more about this issue: http://makeyourowntextminingtoolkit.blogspot.co.uk/2016/09/pandas-dataframe-hdfstore-bug.html

Note: to_pickle() fails for lathe dataframes, hence HDF5

create dataframe and store it

import pandas
df1 = pandas.DataFrame()
df1.ix['apple', '001'] = 0.1
df1.ix['banana', '001'] = 0.2
df1.ix['apple', '002'] = 0.3
df1.ix['banana', '002'] = 0.7
df1.ix['nan', '001'] = 0.5

df1
        001  002
apple   0.1  0.3
banana  0.2  0.7
nan     0.5  NaN


s = pandas.HDFStore('test')
s['index'] = df1
s.close()

now retrieve it

import pandas
s = pandas.HDFStore('test')
s
<class 'pandas.io.pytables.HDFStore'>
File path: test
/index            frame        (shape->[3,2])

df2 = s['index']
s.close()
del s
df2
        001  002
apple   0.1  0.3
banana  0.2  0.7
NaN     0.5  NaN

That NaN should be a string "nan" just like "banana" and "apple".

workaround

df2.index = df2.index.astype(str)
df2
        001  002
apple   0.1  0.3
banana  0.2  0.7
nan     0.5  NaN

output of pd.show_versions()

This is the latest anaconda python 3.5 64bit for MacOSX.

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Darwin
OS-release: 15.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 26.1.1
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: 3.2.3.1
numexpr: 2.6.1
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.4
bs4: 4.5.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.42.0
pandas_datareader: None

Comment From: jreback

this is not a bug, and documented here.

Comment From: makeyourowntextminingtoolkit

Hi - you suggestion is fine but it doesn't work for the index .. it works for values inside a dataframe.

Here is another report of the same issue by @toobaz https://github.com/pydata/pandas/issues/9604

and also @dimazest at https://gist.github.com/dimazest/02c726f228c988cc431f#file-bug-ipynb

there you suggest a "step through" .. does the following help?

create and store data using nan_rep

import pandas
df1 = pandas.DataFrame()
df1.ix['apple', '001'] = 0.1
df1.ix['banana', '001'] = 0.2
df1.ix['apple', '002'] = 0.3
df1.ix['banana', '002'] = 0.7
df1.ix['nan', '001'] = 0.5
df1
        001  002
apple   0.1  0.3
banana  0.2  0.7
nan     0.5  NaN

s = pandas.HDFStore('test')
s.append(df1, nan_rep='_nan_')
s.close()

attempt to retrieve data

import pandas
s = pandas.HDFStore('test')
s
<class 'pandas.io.pytables.HDFStore'>
File path: test
/index            frame_table  (typ->appendable,nrows->3,ncols->2,indexers->[index])

df = s.select('index')
df
        001  002
apple   0.1  0.3
banana  0.2  0.7
NaN     0.5  NaN

df2 = s.select('index', nan_rep='_nan_')
df2
        001  002
apple   0.1  0.3
banana  0.2  0.7
NaN     0.5  NaN

So - the problem is confirmed to still be there, despite the use of nan_rep.

Comment From: jreback

step thru means use the debugger

Comment From: makeyourowntextminingtoolkit

sorry jreback that's beyond my capability - I'm not an expert - but I hope the snippets I have provided help someone who is an expert to do so.

it is clear however the bug is real

ps - good to see at the pydata conf a while back .. :)

Comment From: jorisvandenbossche

Duplicate of https://github.com/pydata/pandas/issues/9604

Comment From: makeyourowntextminingtoolkit

thanks @jorisvandenbossche - its a duplicate of a bug reported by @topa that's still open from 2015.

I've tweeted continuum analytics to raise awareness - I know there have been several attempts to close this issue down as "expected behaviour".

I just wish I had the skills to fix this myself.

Comment From: jreback

@makeyourowntextminingtoolkit we have 1729 open issues. This will need to have someone submit a PR to fix. CA has nothing to do with pandas.