Repository: biolab/orange3

Issue:
CSV File Import does not work with categorical variables
Latest master (3.27):
When loading data with CSV File Import and setting some variables to categorical the data can not be loaded. In the terminal from which I run the Orange I get the following error:
``` Error in sys.excepthook:
Traceback (most recent call last):
  File "/home/karin/Documents/git/orange-widget-base/orangewidget/workflow/errorreporting.py", line 275, in handle_exception
    return ErrorReporting.handle_exception(exc)
  File "/usr/lib/python3.6/unittest/mock.py", line 1183, in patched
    return func(*args, **keywargs)
  File "/home/karin/Documents/git/orange-widget-base/orangewidget/workflow/errorreporting.py", line 234, in handle_exception
    if (err_module, widget_module) in cls._cache or not internet_on():
  File "/home/karin/Documents/git/orange-widget-base/orangewidget/workflow/errorreporting.py", line 50, in internet_on
    urlopen(REPORT_POST_URL, timeout=1)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 564, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 564, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 1368, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.6/urllib/request.py", line 1328, in do_open
    r = h.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1356, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.6/ssl.py", line 1012, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.6/ssl.py", line 874, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/karin/Documents/git/orange3venv/lib/python3.6/site-packages/orangecanvas/application/outputview.py", line 364, in __call__
    self.handledException.emit((exc_type, exc_value, tb))
SystemError: <built-in method emit of PyQt5.QtCore.pyqtBoundSignal object at 0x7faa98061918> returned a result with an error set

Original exception was:
Traceback (most recent call last):
  File "/home/karin/Documents/git/orange3/Orange/widgets/data/owcsvimport.py", line 963, in __handle_result
    table = pandas_to_table(df)
  File "/home/karin/Documents/git/orange3/Orange/widgets/data/owcsvimport.py", line 1550, in pandas_to_table
    coldata.astype(coldata), categories=var.values
  File "/home/karin/Documents/git/orange3venv/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 523, in astype
    dtype = self.dtype.update_dtype(dtype)
  File "/home/karin/Documents/git/orange3venv/lib/python3.6/site-packages/pandas/core/dtypes/dtypes.py", line 562, in update_dtype
    new_ordered = dtype._ordered
AttributeError: 'Categorical' object has no attribute '_ordered'




Discussion:

Resolving patch:
diff --git a/Orange/widgets/data/owcsvimport.py b/Orange/widgets/data/owcsvimport.py
--- a/Orange/widgets/data/owcsvimport.py
+++ b/Orange/widgets/data/owcsvimport.py
@@ -1547,7 +1547,7 @@ def pandas_to_table(df):
             )
             # Remap the coldata into the var.values order/set
             coldata = pd.Categorical(
-                coldata.astype(coldata), categories=var.values
+                coldata.astype("str"), categories=var.values
             )
             codes = coldata.codes
             assert np.issubdtype(codes.dtype, np.integer)


Test patch: