简体   繁体   English

使用 rpy2 将 python dataframe 字符列转换为 r

[英]Conversion python dataframe character column to r with rpy2

I am trying to convert a python dataframe to r with rpy2 and I cannot get a date in python dataframe to be converted to a date type in r dataframes. I am trying to convert a python dataframe to r with rpy2 and I cannot get a date in python dataframe to be converted to a date type in r dataframes.

When converting a pd.to_datetime() to r dataframe I am not getting a correct conversion.pd.to_datetime()转换为 r dataframe 时,我没有得到正确的转换。

df date columns in question: df 日期列有问题:

     event_time
0    2019-10-11
1    2020-01-01
2    2019-11-15
3    2020-03-05

Conversion code:转换代码:

with localconverter(ro.default_converter + pandas2ri.converter):

    df['event_time'] = pd.to_datetime(df['event_time']).dt.strftime("%Y-%m-%d")
    df["event_time"] = pd.to_datetime(df["event_time"]).dt.date
    r_df = ro.conversion.py2rpy(df)

Produces:产生:

event_time: <class 'numpy.ndarray'>
  array([737343., 737425., 737378., 737489.])

And the same thing for discharge_time.放电时间也是如此。

Conversion code with string and then attempt to convert:用字符串转换代码,然后尝试转换:

with localconverter(ro.default_converter + pandas2ri.converter):

    df['event_time'] = pd.to_datetime(df['event_time']).dt.strftime("%Y-%m-%d")
    #### df["event_time"] = pd.to_datetime(df["event_time"]).dt.date
    r_df = ro.conversion.py2rpy(df)

    r_df = base.cbind(r_df, event_time = base.as_Date(r_df[r_df.names.index('event_time')], '%Y-%m-%d'))

Produces a dataframe with:产生一个 dataframe :

event_time: <class 'numpy.ndarray'>
  array(['2019-10-11', '2020-01-01', '2019-11-15', '2020-03-05'], dtype='<U10')

But this line of code r_df = base.cbind(r_df, event_time = base.as_Date(r_df[r_df.names.index('event_time')], '%Y-%m-%d')) errors with:但是这行代码r_df = base.cbind(r_df, event_time = base.as_Date(r_df[r_df.names.index('event_time')], '%Y-%m-%d'))错误:

AttributeError: 'numpy.ndarray' object has no attribute 'index' AttributeError: 'numpy.ndarray' object 没有属性 'index'

Using this code produces:使用此代码会产生:

with localconverter(ro.default_converter + pandas2ri.converter):

    df['event_time'] = pd.to_datetime(df['event_time']).dt.strftime("%Y-%m-%d")
    #### df["event_time"] = pd.to_datetime(df["event_time"]).dt.date
    r_df = ro.conversion.py2rpy(df)

    r_df = base.cbind(r_df, event_time = base.as_Date(r_df[r_df.rx2('event_time')], '%Y-%m-%d'))

Error:错误:

Conversion 'py2rpy' not defined for objects of type '<class 'numpy.ndarray'>'未为“<class 'numpy.ndarray'>”类型的对象定义转换“py2rpy”

So how do I get a date from a python dataframe into a date in r with rpy2?那么如何使用 rpy2 从 python dataframe 中获取日期到 r 中的日期? I need it in a date format because I will be doing date calculations later on and strings will not work.我需要日期格式,因为稍后我将进行日期计算,而字符串将不起作用。

Versions:版本:

pandas==1.0.1熊猫==1.0.1

rpy2~=3.3.5 rpy2~=3.3.5

Your problem has nothing to do with rpy2, you are just parsing dates incorrectly in pandas.您的问题与 rpy2 无关,您只是在 pandas 中错误地解析日期。 See:看:

from pandas import DataFrame, to_datetime

df = DataFrame(dict(event_time=['2019-10-11', '2020-01-01']))

df.event_time = to_datetime(df.event_time)

print(list(df.event_time))
# [Timestamp('2019-10-11 00:00:00'), Timestamp('2020-01-01 00:00:00')]

# you using dt.strftime you was just converting them back to strings, see:
print(list(df.event_time.dt.strftime("%Y-%m-%d")))
# ['2019-10-11', '2020-01-01', '2019-11-15']

# now you could extract date object (but don't! timestamps are fine for rpy2)
print(list(df.event_time.dt.date))
# [datetime.date(2019, 10, 11), datetime.date(2020, 1, 1)]

Now in rpy2 you simply do:现在在 rpy2 中,您只需执行以下操作:

from rpy2.robjects import conversion, default_converter, pandas2ri
from rpy2.robjects.conversion import localconverter


with localconverter(default_converter + pandas2ri.converter):
    df_r = conversion.py2rpy(df)

print(repr(df_r.rx2('event_time')))
# R object with classes: ('POSIXct', 'POSIXt') mapped to:
# [2019-10-11, 2020-01-01]

Now you can have fun with handling the dates on the R side, see dates .现在,您可以在 R 方面享受处理日期的乐趣,请参阅dates Also, if you happen to use Jupyter notebooks, conversion is much more handy using cell magics .此外,如果您碰巧使用 Jupyter 笔记本,使用cell magics进行转换会更方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用rpy2将带有时间戳列的python数据帧(pandas)转换为R数据帧 - Converting python dataframe(pandas) with a timestamp column to R dataframe using rpy2 rpy2 转换-&gt; 将 python dict 传递给 ... R 中的参数 - rpy2 conversion-> passing a python dict to a … argument in R 将列添加到rpy2中的DataFrame - Add column to DataFrame in rpy2 将python pandas DataFrame转换为R dataframe以与rpy2一起使用 - issue converting python pandas DataFrame to R dataframe for use with rpy2 rpy2 在 python 中加载 R-dataframe 会产生错误的时间戳 - rpy2 loading R-dataframe in python produces wrong timestamps 如何从python中的rpy2中的R数据框中选择列? - how to select columns from R dataframe in rpy2 in python? 有没有办法在python / rpy2中访问R数据框列名? - Is there a way to access R data frame column names in python/rpy2? 获得Rpy2数据帧的R类 - get R classes of rpy2 dataframe rpy2在ubuntu上测试获取“ NotImplementedError:从rpy2 DataFrame转换为熊猫的DataFrame” - rpy2 tests getting “NotImplementedError: Conversion from rpy2 DataFrame to pandas' DataFrame” on ubuntu Python 脚本中的 RPY2 错误! (未为类型为“的对象定义转换‘py2rpy’<class 'pandas.core.frame.dataframe'> ')</class> - RPY2 error in Python script! (Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.frame.DataFrame'>')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM