pandas: Unable to import pandas when using the gtk3 backend in ipython

In [1]: %matplotlib gtk3

In [2]: import pandas
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-2-38d4b0363d82> in <module>()
----> 1 import pandas

~/.local/lib/python3.6/site-packages/pandas/__init__.py in <module>()
     56 
     57 from pandas.util._print_versions import show_versions
---> 58 from pandas.io.api import *
     59 from pandas.util._tester import test
     60 import pandas.testing

~/.local/lib/python3.6/site-packages/pandas/io/api.py in <module>()
     17 from pandas.io.stata import read_stata
     18 from pandas.io.pickle import read_pickle, to_pickle
---> 19 from pandas.io.packers import read_msgpack, to_msgpack
     20 from pandas.io.gbq import read_gbq
     21 

~/.local/lib/python3.6/site-packages/pandas/io/packers.py in <module>()
     66 
     67 from pandas.io.msgpack import Unpacker as _Unpacker, Packer as _Packer, ExtType
---> 68 from pandas.util._move import (
     69     BadMove as _BadMove,
     70     move_into_mutable_buffer as _move_into_mutable_buffer,

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 3: invalid start byte

If I use tk or qt instead, this doesn’t happen.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 33 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I was having the same problem of you, but without use matplotlib directly.

I was using graph_tool, which has a draw module that imports gtk internally), and pandas. I just have changed the order that I imported the libs.

My code before the workaround:

import graph_tool.all as gt
import pandas as pd

My code after the workaround:

import pandas as pd
import graph_tool.all as gt

So, I think that you should try to import pandas before and, set the matplotlib backend after.

Importing Glib instead is ok:

>>> from gi.repository import GLib
>>> import pandas
>>> 

Also Gdk works. The problem triggers as soon as class Widget(Gtk.Widget): is left in gi/overrides/Gtk.py. Here Gtk = get_introspection_module('Gtk') where

def get_introspection_module(namespace):
    """
    :Returns:
        An object directly wrapping the gi module without overrides.

    Might raise gi._gi.RepositoryError
    """
    if namespace in _introspection_modules:
        return _introspection_modules[namespace]

    version = gi.get_required_version(namespace)
    module = IntrospectionModule(namespace, version)
    _introspection_modules[namespace] = module
    return module

I can leave only

from ..module import get_introspection_module
Gtk = get_introspection_module('Gtk')
Gtk.Widget    # or for example Gtk.Container

in the entire Gtk.py module and the problem still happens. I’ve instrumented every method of IntrospectionModule and none of them is called just before the error occurs.

Clearly the problem is very localized, we’ve now come to a point where no element can be removed without losing the ability to reproduce the error. Just three lines of the entire Gtk.py module get us into trouble.

OTOH the utf8 aspect could be unrelated to the real problem. Maybe something binary or ill-codified is being written to the terminal thus obfuscating the real nature of the problem.

Finally, at the point of the error the exception shows this trace:

  <frozen importlib._bootstrap>(983)_find_and_load()
  <frozen importlib._bootstrap>(967)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(677)_load_unlocked()
  <frozen importlib._bootstrap_external>(728)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
> /home/carlos/.local/lib/python3.7/site-packages/pandas/io/packers.py(76)<module>()