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

HDFStore.select_column #17912

Closed
lafrech opened this Issue Oct 18, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@lafrech

lafrech commented Oct 18, 2017

Code Sample, a copy-pastable example if possible

Let's select column from a non-existing dataframe in a HDFStore:

import pandas as pd

store = pd.HDFStore('test.hdf5', mode='w')
store.select_column('dummy', 'index')

Problem description

We get an AttributeError because get_storer returns None:

Traceback (most recent call last):
  File "pandas_hdf5.py", line 4, in <module>
    store.select_column('dummy', 'index')
  File "[...]/site-packages/pandas/io/pytables.py", line 778, in select_column
    return self.get_storer(key).read_column(column=column, **kwargs)
AttributeError: 'NoneType' object has no attribute 'read_column'

Is this intended?

Expected Output

The docstring says:

            """
            Exceptions
            ----------
            raises KeyError if the column is not found (or key is not a valid
                store)
            raises ValueError if the column can not be extracted individually (it
                is part of a data block)
            """

Shouldn't I expect a KeyError, then?

It could be just this simple patch:

    - return self.get_storer(key).read_column(column=column, **kwargs)
    + storer = self.get_storer(key)
    + if storer is None:
    +    raise KeyError('{} not in {}'.format(key, self))
    + return storer.read_column(column=column, **kwargs)

or should get_storer raise en exception in the first place?

I'm new to Pandas/PyTables so I don't have the big picture.

From a caller perspective, I could to check first that the dataframe is in the store:

store = pd.HDFStore('test.hdf5', mode='w')
if 'dummy' in store:
    store.select_column('dummy', 'index')

but I'd rather "ask forgiveness not permission",

store = pd.HDFStore('test.hdf5', mode='w')
try:
    store.select_column('dummy', 'index')
except AttributeError:
    [...]

so I should catch AttributeError but I'm not sure this exception being thrown is a design choice.

I hope I'm being constructive and I don't sound like I'm nitpicking.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-4-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8
LOCALE: fr_FR.UTF-8

pandas: 0.20.3
pytest: 3.2.3
pip: 9.0.1
setuptools: 36.6.0
Cython: None
numpy: 1.13.3
scipy: 0.19.1
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@chris-b1

This comment has been minimized.

Contributor

chris-b1 commented Oct 18, 2017

Agree this looks buggy, I would probably push the exception down to get_storer and see if any tests break, docstring says it raises. Want to do a PR?

""" return the storer object for a key, raise if not in the file """

@chris-b1 chris-b1 added this to the Next Major Release milestone Oct 18, 2017

@lafrech

This comment has been minimized.

lafrech commented Oct 18, 2017

I might try to give it a shot.

The error in get_storer would be a KeyError, right? Would this be fine?

    def get_storer(self, key):
        """ return the storer object for a key, raise if not in the file """
        group = self.get_node(key)
        if group is None:
-           return None
+           raise KeyError('No {} node in {}'.format(key, self))
        s = self._create_storer(group)
        s.infer_axes()
        return s
@CianciuStyles

This comment has been minimized.

Contributor

CianciuStyles commented Apr 15, 2018

Hello, I've just opened a PR for this bug (#20705). Hope it helps.

@jreback jreback modified the milestones: Next Major Release, 0.23.0 Apr 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment