Skip to content

add support for numpy 1.8+ data types for conversion to r dataframe #8400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ API changes
- ``DataFrame.info()`` now ends its output with a newline character (:issue:`8114`)
- add ``copy=True`` argument to ``pd.concat`` to enable pass thrue of complete blocks (:issue:`8252`)


- Added support for numpy 1.8+ data types (bool_, int_, float_, string_) for conversion to R dataframe (:issue:`8400`)

.. _whatsnew_0150.dt:

Expand Down
18 changes: 15 additions & 3 deletions pandas/rpy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from __future__ import print_function

from distutils.version import LooseVersion
from pandas.compat import zip, range
import numpy as np

Expand Down Expand Up @@ -72,7 +73,7 @@ def _list(item):
return list(item)
except TypeError:
return []

# For iris3, HairEyeColor, UCBAdmissions, Titanic
dim = list(obj.dim)
values = np.array(list(obj))
Expand Down Expand Up @@ -101,9 +102,9 @@ def _convert_vector(obj):
except AttributeError:
return list(obj)
if 'names' in attributes:
return pd.Series(list(obj), index=r['names'](obj))
return pd.Series(list(obj), index=r['names'](obj))
elif 'tsp' in attributes:
return pd.Series(list(obj), index=r['time'](obj))
return pd.Series(list(obj), index=r['time'](obj))
elif 'labels' in attributes:
return pd.Series(list(obj), index=r['labels'](obj))
if _rclass(obj) == 'dist':
Expand Down Expand Up @@ -268,6 +269,7 @@ def convert_to_r_posixct(obj):
np.str: robj.StrVector,
np.bool: robj.BoolVector}


NA_TYPES = {np.float64: robj.NA_Real,
np.float32: robj.NA_Real,
np.float: robj.NA_Real,
Expand All @@ -279,6 +281,16 @@ def convert_to_r_posixct(obj):
np.bool: robj.NA_Logical}


if LooseVersion(np.__version__) >= LooseVersion('1.8'):
for dict_ in (VECTOR_TYPES, NA_TYPES):
dict_.update({
np.bool_: dict_[np.bool],
np.int_: dict_[np.int],
np.float_: dict_[np.float],
np.string_: dict_[np.str]
})


def convert_to_r_dataframe(df, strings_as_factors=False):
"""
Convert a pandas DataFrame to a R data.frame.
Expand Down