pandasjupyter-notebookrpy2

AttributeError: module 'pandas' has no attribute 'Float64Dtype'


I am using Azure ML Notebook to run both python and R code in notebook cells. I have installed python packages pandas 1.1.5 and rpy2 3.5.4 but when I run the following lines of code:

%load_ext rpy2.ipython
print("ankit")

I get the following error message:

 AttributeError: module 'pandas' has no attribute 'Float64Dtype'

Can anyone please help to resolve this error ? Any help would be appreciated.

Complete error is as following:

AttributeError                            Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('load_ext', 'rpy2.ipython')
      2 print("ankit")

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2305, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2303     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2304 with self.builtin_trap:
-> 2305     result = fn(*args, **kwargs)
   2306 return result

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/magics/extension.py:33, in ExtensionMagics.load_ext(self, module_str)
     31 if not module_str:
     32     raise UsageError('Missing module name.')
---> 33 res = self.shell.extension_manager.load_extension(module_str)
     35 if res == 'already loaded':
     36     print("The %s extension is already loaded. To reload it, use:" % module_str)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/extensions.py:76, in ExtensionManager.load_extension(self, module_str)
     69 """Load an IPython extension by its module name.
     70 
     71 Returns the string "already loaded" if the extension is already loaded,
     72 "no load function" if the module doesn't have a load_ipython_extension
     73 function, or None if it succeeded.
     74 """
     75 try:
---> 76     return self._load_extension(module_str)
     77 except ModuleNotFoundError:
     78     if module_str in BUILTINS_EXTS:

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/extensions.py:92, in ExtensionManager._load_extension(self, module_str)
     90 if module_str not in sys.modules:
     91     with prepended_to_syspath(self.ipython_extension_dir):
---> 92         mod = import_module(module_str)
     93         if mod.__file__.startswith(self.ipython_extension_dir):
     94             print(("Loading extensions from {dir} is deprecated. "
     95                    "We recommend managing extensions like any "
     96                    "other Python packages, in site-packages.").format(
     97                   dir=compress_user(self.ipython_extension_dir)))

File /anaconda/envs/azureml_py38/lib/python3.8/importlib/__init__.py:127, in import_module(name, package)
    125             break
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1014, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:991, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:975, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:671, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:783, in exec_module(self, module)

File <frozen importlib._bootstrap>:219, in _call_with_frames_removed(f, *args, **kwds)

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/__init__.py:1, in <module>
----> 1 from . import rmagic
      3 load_ipython_extension = rmagic.load_ipython_extension

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/rmagic.py:121, in <module>
    116 def _get_converter(template_converter=template_converter):
    117     return Converter('ipython conversion',
    118                      template=template_converter)
--> 121 ipy_template_converter = _get_ipython_template_converter(template_converter,
    122                                                          numpy=numpy,
    123                                                          pandas=pandas)
    124 converter = _get_converter(template_converter=ipy_template_converter)
    127 def CELL_DISPLAY_DEFAULT(res, args):

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/rmagic.py:111, in _get_ipython_template_converter(template_converter, numpy, pandas)
    109     template_converter += numpy2ri.converter
    110     if pandas:
--> 111         from rpy2.robjects import pandas2ri
    112         template_converter += pandas2ri.converter
    113 return template_converter

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/robjects/pandas2ri.py:144, in <module>
    125             v = math.nan
    126         set_elt(r_vector, i, cast_value(v))
    129 _PANDASTYPE2RPY2 = {
    130     datetime.date: DateVector,
    131     int: functools.partial(
    132         IntVector.from_iterable,
    133         populate_func=_int_populate_r_vector
    134     ),
    135     pandas.BooleanDtype: functools.partial(
    136         BoolVector.from_iterable,
    137         populate_func=_bool_populate_r_vector
    138     ),
    139     None: BoolVector,
    140     str: functools.partial(
    141         StrVector.from_iterable,
    142         populate_func=_str_populate_r_vector
    143     ),
--> 144     pandas.Float64Dtype: functools.partial(
    145         FloatVector.from_iterable,
    146         populate_func=_float_populate_r_vector
    147     ),
    148     bytes: (numpy2ri.converter.py2rpy.registry[
    149         numpy.ndarray
    150     ])
    151 }
    154 @py2rpy.register(pandas.core.series.Series)
    155 def py2rpy_pandasseries(obj):
    156     if obj.dtype.name == 'O':

File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/pandas/__init__.py:258, in __getattr__(name)
    254     from pandas.core.arrays.sparse import SparseArray as _SparseArray
    256     return _SparseArray
--> 258 raise AttributeError(f"module 'pandas' has no attribute '{name}'")

AttributeError: module 'pandas' has no attribute 'Float64Dtype'

Solution

  • I replicate your situation on my system and it works.

    You can use this set of library versions:

    R  version: 4.2.1
    rpy2  version: 3.4.5
    Pandas  version: 1.3.5