Issue while using magics in Atom
I try to use the magics %browse to look at the data viewer the got the error message from Atom:
[IPKernelApp] ERROR | Exception in message handler: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tornado/gen.py", line 735, in run value = future.result() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 543, in execute_request self.do_execute( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/stata_kernel/kernel.py", line 110, in do_execute code = self.magics.magic(code, self) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/stata_kernel/stata_magics.py", line 228, in magic code = getattr(self, "magic_" + name)(code, kernel) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/stata_kernel/stata_magics.py", line 272, in magic_browse res = self.show_data_head(code, kernel, N=200) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/stata_kernel/stata_magics.py", line 310, in show_data_head df = pd.read_csv(using, dtype=str) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 688, in read_csv return _read(filepath_or_buffer, kwds) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 460, in _read data = parser.read(nrows) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 1198, in read ret = self._engine.read(nrows) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/parsers.py", line 2157, in read data = self._reader.read(nrows) File "pandas/_libs/parsers.pyx", line 847, in pandas._libs.parsers.TextReader.read File "pandas/_libs/parsers.pyx", line 862, in pandas._libs.parsers.TextReader._read_low_memory File "pandas/_libs/parsers.pyx", line 941, in pandas._libs.parsers.TextReader._read_rows File "pandas/_libs/parsers.pyx", line 1073, in pandas._libs.parsers.TextReader._convert_column_data File "pandas/_libs/parsers.pyx", line 1104, in pandas._libs.parsers.TextReader._convert_tokens File "pandas/_libs/parsers.pyx", line 1241, in pandas._libs.parsers.TextReader._convert_with_dtype File "pandas/_libs/parsers.pyx", line 1259, in pandas._libs.parsers.TextReader._string_convert File "pandas/_libs/parsers.pyx", line 1450, in pandas._libs.parsers._string_box_utf8 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xab in position 1: invalid start byte
I tried to upgrade to the latest version but the issue stays.
Code Sample
All the other Stata codes function well except the magics (%browse, %help, etc.)
Expected Output
Someway to fix the issue?
Other information
If you didn't attach the debugging log, please provide:
- macOS Catalina
- Stata version: 16.1 MP
- Package version: 1.12.1
You can inspect the data file in the cache dir (~/.stata_kernel_cache I think). Basically %browse tried to export a CSV from Stata and load it into Python, but it was an invalid CSV.
By the way this error should be tied specifically to %browse/%head/%tail which all use the same mechanism. %help shouldn't be affected by this.
Quite interesting that I found the data_head.cvs in the ~/.stata_kernel_cache folder and it's seems to be ok. It may be affected by the Unicode problem.
If you look closely in the Python traceback, you can see df = pd.read_csv(using, dtype=str) and that's causing the error. So it's possible that Stata wrote the CSV in a non-Unicode encoding. I don't have access to Stata anymore.