Skip to content
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

IndexError: list index out of range #1

Open
a-edalat opened this issue May 30, 2020 · 2 comments
Open

IndexError: list index out of range #1

a-edalat opened this issue May 30, 2020 · 2 comments

Comments

@a-edalat
Copy link

Hi Shail,

I tried to follow your review on Medium, when I run your code on my machine I got the above error:

here is the full error, and the error is not due to reading from the web address, as that function reads the csv:

I am not sure what more I should provide as this is the first time ever I am raising an issue.

`Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/aedal/OneDrive/Desktop/del.py', wdir='C:/Users/aedal/OneDrive/Desktop')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/aedal/OneDrive/Desktop/del.py", line 53, in
top_host_1 = df.query('[email protected][0]')

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3184, in query
res = self.eval(expr, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3300, in eval
return _eval(expr, inplace=inplace, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\eval.py", line 322, in eval
parsed_expr = Expr(expr, engine=engine, parser=parser, env=env, truediv=truediv)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 827, in init
self.terms = self.parse()

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 844, in parse
return self._visitor.visit(self.expr)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 441, in visit
return visitor(node, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 447, in visit_Module
return self.visit(expr, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 441, in visit
return visitor(node, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 450, in visit_Expr
return self.visit(node.value, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 441, in visit
return visitor(node, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 744, in visit_Compare
return self.visit(binop)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 441, in visit
return visitor(node, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 563, in visit_BinOp
op, op_class, left, right = self._maybe_transform_eq_ne(node)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 484, in _maybe_transform_eq_ne
right = self.visit(node.right, side="right")

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 441, in visit
return visitor(node, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expr.py", line 603, in visit_Subscript
slobj, local_dict=self.env, engine=self.engine, parser=self.parser

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\eval.py", line 319, in eval
target=target,

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\scope.py", line 31, in _ensure_scope
target=target,

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\scope.py", line 124, in init
self.update(local_dict.level)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\scope.py", line 261, in update
stack = inspect.stack()

File "C:\ProgramData\Anaconda3\lib\inspect.py", line 1513, in stack
return getouterframes(sys._getframe(1), context)

File "C:\ProgramData\Anaconda3\lib\inspect.py", line 1490, in getouterframes
frameinfo = (frame,) + getframeinfo(frame, context)

File "C:\ProgramData\Anaconda3\lib\inspect.py", line 1464, in getframeinfo
lines, lnum = findsource(frame)

File "C:\ProgramData\Anaconda3\lib\inspect.py", line 828, in findsource
if pat.match(lines[lnum]): break

IndexError: list index out of range`

Thanks,
Amin

@a-edalat
Copy link
Author

here is the issue when the same code is ran in Jupyter Notebook:

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
280 tname = type(obj).qualname.encode()
--> 281 b = b"%s:%s" % (tname, self._to_bytes(obj, context))
282 # _LOGGER.debug("Done hashing: %s", obj)

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _to_bytes(self, obj, context)
518
--> 519 if self._file_should_be_hashed(obj.code.co_filename):
520 context = _get_context(obj)

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _file_should_be_hashed(self, filename)
316 return file_util.file_is_in_folder_glob(
--> 317 filepath, self._get_main_script_directory()
318 ) or file_util.file_in_pythonpath(filepath)

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _get_main_script_directory()
601 # script path in ScriptRunner.
--> 602 main_path = main.file
603 return os.path.dirname(main_path)

AttributeError: module 'main' has no attribute 'file'

During handling of the above exception, another exception occurred:

InternalHashError Traceback (most recent call last)
in
3 import plotly.express as px
4
----> 5 @st.cache
6 def get_data():
7 return pd.read_csv("http://data.insideairbnb.com/united-states/ny/new-york-city/2019-09-12/visualisations/listings.csv")

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\caching.py in cache(func, persist, allow_output_mutation, show_spinner, suppress_st_warning, hash_funcs, max_entries, ttl)
490 hash_funcs=hash_funcs,
491 hash_reason=HashReason.CACHING_FUNC_BODY,
--> 492 hash_source=func,
493 )
494

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in update_hash(val, hasher, hash_reason, hash_source, context, hash_funcs)
74
75 ch = _CodeHasher(hash_funcs)
---> 76 ch.update(hasher, val, context)
77
78

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in update(self, hasher, obj, context)
305 def update(self, hasher, obj, context=None):
306 """Update the provided hasher with the hash of an object."""
--> 307 b = self.to_bytes(obj, context)
308 hasher.update(b)
309

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
279 # _LOGGER.debug("About to hash: %s", obj)
280 tname = type(obj).qualname.encode()
--> 281 b = b"%s:%s" % (tname, self._to_bytes(obj, context))
282 # _LOGGER.debug("Done hashing: %s", obj)
283

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _to_bytes(self, obj, context)
355 h = hashlib.new("md5")
356 for item in obj:
--> 357 self.update(h, item, context)
358 return h.digest()
359

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in update(self, hasher, obj, context)
305 def update(self, hasher, obj, context=None):
306 """Update the provided hasher with the hash of an object."""
--> 307 b = self.to_bytes(obj, context)
308 hasher.update(b)
309

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
294
295 except BaseException as e:
--> 296 raise InternalHashError(e, obj)
297
298 finally:

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in to_bytes(self, obj, context)
279 # _LOGGER.debug("About to hash: %s", obj)
280 tname = type(obj).qualname.encode()
--> 281 b = b"%s:%s" % (tname, self._to_bytes(obj, context))
282 # _LOGGER.debug("Done hashing: %s", obj)
283

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _to_bytes(self, obj, context)
517 h = hashlib.new("md5")
518
--> 519 if self._file_should_be_hashed(obj.code.co_filename):
520 context = _get_context(obj)
521 if obj.defaults:

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _file_should_be_hashed(self, filename)
315 return False
316 return file_util.file_is_in_folder_glob(
--> 317 filepath, self._get_main_script_directory()
318 ) or file_util.file_in_pythonpath(filepath)
319

C:\ProgramData\Anaconda3\lib\site-packages\streamlit\hashing.py in _get_main_script_directory()
600 # This works because we set main.file to the report
601 # script path in ScriptRunner.
--> 602 main_path = main.file
603 return os.path.dirname(main_path)
604

InternalHashError: module 'main' has no attribute 'file'

While caching the body of get_data(), Streamlit encountered an
object of type builtins.function, which it does not know how to hash.

In this specific case, it's very likely you found a Streamlit bug so please
[file a bug report here.]
(https://github.com/streamlit/streamlit/issues/new/choose)

In the meantime, you can try bypassing this error by registering a custom
hash function via the hash_funcs keyword in @st.cache(). For example:

@st.cache(hash_funcs={builtins.function: my_hash_func})
def my_func(...):
    ...

If you don't know where the object of type builtins.function is coming
from, try looking at the hash chain below for an object that you do recognize,
then pass that to hash_funcs instead:

Object of type builtins.function: <function get_data at 0x000001C71433A288>
Object of type builtins.tuple: ('__main__', 'get_data', <function get_data at 0x000001C71433A288>)

Please see the hash_funcs [documentation]
(https://streamlit.io/docs/advanced_caching.html)
for more details.

`

@ashu198518
Copy link

Above mentioned errors are due to data being old and not available on the hyperlink provided in the code.
Someone can get the dataset from updated URL http://insideairbnb.com/get-the-data.html
Hope it will be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants