Exception: Data must be 1-dimensional when converting a data.frame

Issue #391 invalid
Dmytro Lituiev
created an issue

Example:

%%R -o out_spearman

out_spearman = as.data.frame(rbind(
    cor.test(iris$Sepal.Length, iris$Sepal.Width, method="spearman"), 
    cor.test(iris$Sepal.Length, iris$Petal.Length, method="spearman"))[,c("data.name", "p.value", "estimate")]
                             )

Exception:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-108-225dffa3a8ee> in <module>()
----> 1 get_ipython().run_cell_magic('R', '-o out_spearman', '\nout_spearman = as.data.frame(rbind(\n    cor.test(iris$Sepal.Length, iris$Sepal.Width, method="spearman"), \n    cor.test(iris$Sepal.Length, iris$Petal.Length, method="spearman"))[,c("data.name", "p.value", "estimate")]\n                             )')

/netapp/home/dlituiev/.usr/py3/lib/ipython-5.0.0.dev0-py2.7.egg/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2114             magic_arg_s = self.var_expand(line, stack_depth)
   2115             with self.builtin_trap:
-> 2116                 result = fn(magic_arg_s, cell)
   2117             return result
   2118 

<decorator-gen-129> in R(self, line, cell, local_ns)

/netapp/home/dlituiev/.usr/py3/lib/ipython-5.0.0.dev0-py2.7.egg/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/netapp/home/dlituiev/.usr/py3/lib/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns)
    712             for output in ','.join(args.output).split(','):
    713                 if args.converter is None:
--> 714                     output_ipy = converter.ri2py(ri.globalenv.get(output))
    715                 else:
    716                     output_ipy = localconverter.ri2py(ri.globalenv.get(output))

/netapp/home/dlituiev/.usr/py3/lib/singledispatch-3.4.0.3-py3.3.egg/singledispatch.py in wrapper(*args, **kw)
    208 
    209     def wrapper(*args, **kw):
--> 210         return dispatch(args[0].__class__)(*args, **kw)
    211 
    212     registry[object] = func

/netapp/home/dlituiev/.usr/py3/lib/rpy2/robjects/pandas2ri.py in ri2py_listvector(obj)
    140 def ri2py_listvector(obj):
    141     if 'data.frame' in obj.rclass:
--> 142         res = ri2py.registry[DataFrame](obj)
    143     else:
    144         res = numpy2ri.ri2py(obj)

/netapp/home/dlituiev/.usr/py3/lib/rpy2/robjects/pandas2ri.py in ri2py_dataframe(obj)
    154         idx = None
    155     res = PandasDataFrame.from_records(recarray,
--> 156                                        index=idx)
    157     return res
    158 

/netapp/home/dlituiev/.usr/py3/lib/pandas-0.18.0-py3.3-linux-x86_64.egg/pandas/core/frame.py in from_records(cls, data, index, exclude, columns, coerce_float, nrows)
   1020             columns = columns.drop(exclude)
   1021 
-> 1022         mgr = _arrays_to_mgr(arrays, arr_columns, result_index, columns)
   1023 
   1024         return cls(mgr)

/netapp/home/dlituiev/.usr/py3/lib/pandas-0.18.0-py3.3-linux-x86_64.egg/pandas/core/frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   5243 
   5244     # don't force copy because getting jammed in an ndarray anyway
-> 5245     arrays = _homogenize(arrays, index, dtype)
   5246 
   5247     # from BlockManager perspective

/netapp/home/dlituiev/.usr/py3/lib/pandas-0.18.0-py3.3-linux-x86_64.egg/pandas/core/frame.py in _homogenize(data, index, dtype)
   5553                 v = lib.fast_multiget(v, oindex.values, default=NA)
   5554             v = _sanitize_array(v, index, dtype=dtype, copy=False,
-> 5555                                 raise_cast_failure=False)
   5556 
   5557         homogenized.append(v)

/netapp/home/dlituiev/.usr/py3/lib/pandas-0.18.0-py3.3-linux-x86_64.egg/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
   2969     elif subarr.ndim > 1:
   2970         if isinstance(data, np.ndarray):
-> 2971             raise Exception('Data must be 1-dimensional')
   2972         else:
   2973             subarr = _asarray_tuplesafe(data, dtype=dtype)

Exception: Data must be 1-dimensional

Comments (5)

  1. Dmytro Lituiev reporter
    'data.frame':   2 obs. of  3 variables:
     $ data.name:List of 2
      ..$ : chr "iris$Sepal.Length and iris$Sepal.Width"
      ..$ : chr "iris$Sepal.Length and iris$Petal.Length"
     $ p.value  :List of 2
      ..$ : num 0.0414
      ..$ : num 3.44e-50
     $ estimate :List of 2
      ..$ : Named num -0.167
      .. ..- attr(*, "names")= chr "rho"
      ..$ : Named num 0.882
      .. ..- attr(*, "names")= chr "rho"
    NULL
    
  2. Laurent Gautier

    Yes. I am not sure that this is really a data frame. R can be tricked very easily into making a object of class data.frame that does not conform to the the definition. Each "column" in a data.frame should be "atomic" (therefore not of type list).

    I think that you want:

    %%R -o out_spearman
    
    out_spearman = rbind(
        as.data.frame(cor.test(iris$Sepal.Length, iris$Sepal.Width, method="spearman")), 
        as.data.frame(cor.test(iris$Sepal.Length, iris$Petal.Length, method="spearman"))))[c("data.name", "p.value", "estimate")]
    
  3. Log in to comment