Almenon / AREPL-vscode

Program python in real-time
MIT License
262 stars 31 forks source link

pandas not working when printing dataframes #404

Open Almenon opened 3 years ago

Almenon commented 3 years ago

Describe the bug Pandas does not work when printing multiple columns of dataframes

To Reproduce Steps to reproduce the behavior:

import pandas as pd
cafes = pd.DataFrame({
'zip': [30324,],
'poc': ['jared', ]
})

# this works
print(cafes['zip'])
print('------')
print(cafes['poc'])

# this doesn't
print(cafes[['zip', 'poc']])

Expected behavior Pandas prints without error as it does when running in the terminal

Other Information (please complete the following information):

Additional context It's weird because usually errors happen the second or third run, but this error happens immediately. So it's not related to the module or jsonpickle logic.

Error:

Traceback (most recent call last):
  line 14, in <module>
  File "C:\Users\almenon\AppData\Roaming\Python\Python36\site-packages\pandas\core\frame.py", line 2806, in __getitem__
    indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
  File "C:\Users\almenon\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexing.py", line 1552, in _get_listlike_indexer
    keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
  File "C:\Users\almenon\AppData\Roaming\Python\Python36\site-packages\pandas\core\indexing.py", line 1634, in _validate_read_indexer
    missing = (indexer < 0).sum()
  File "C:\Users\almenon\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 47, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial, where)
TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'
Almenon commented 3 years ago

Just doing a normal exec works 🤔 why does my exec fail?

code = """
import pandas as pd
cafes = pd.DataFrame({
'zip': [30324,],
'poc': ['jared', ]
})

# this works
print(cafes['zip'])
print('------')
print(cafes['poc'])

# this doesn't
print(cafes[['zip', 'poc']])
"""

exec(code)
Almenon commented 3 years ago

Using runcontext also works:

locals = {}
import contextvars
run_context = contextvars.Context()
run_context.run(exec, code, locals)

The pandas code works in AREPL if python version 3.7 or above. This is still an issue with 3.6 but at least it has a workaround.