pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.65k stars 17.92k forks source link

API: Poor error message on colspec validation. #5288

Closed jtratner closed 10 years ago

jtratner commented 11 years ago

AssertionError bubbling up to the user - ruroh:

from io import StringIO
data = StringIO(u"""
121301234
121300123
121300012
""")
pd.read_fwf(data, colspecs=[4, 5])

Ends up with an AssertionError:

/opt/anaconda/envs/np17py27-1.5/lib/python2.7/site-packages/pandas/io/parsers.pyc in __init__(self, f, colspecs, filler, thousands)
   1771         assert isinstance(colspecs, (tuple, list))
   1772         for colspec in colspecs:
-> 1773             assert isinstance(colspec, (tuple, list))
   1774             assert len(colspec) == 2
   1775             assert isinstance(colspec[0], int)

AssertionError: 

Low priority but we should fix at some point. Really could be as simple as this in read_fwf. if not all(isinstance(colspec, (tuple, list) and len(colspec) == 2 and isinstance(colspec[0], int) for colspec in colspecs): raise ValueError("colspecs must be 2-tuples of integers")

jreback commented 10 years ago

cc @alefnula

want to submit a PR for this?

alefnula commented 10 years ago

@jreback No problem, just tell me which exception is the most reasonable one?

assert isinstance(colspec, (tuple, list))  # TypeError 
assert len(colspec) == 2                   # ValueError
assert isinstance(colspec[0], int)         # TypeError

?

jreback commented 10 years ago

yep those look good

alefnula commented 10 years ago

@jreback This is fixed on master.

jreback commented 10 years ago

gr8 I should have looked!