python – DateOffset Panda减法

python – DateOffset Panda减法,第1张

概述我有一个dataFrame [in] MyDates[out] 2017-04-04 -5.02017-04-03 -5.02017-03-31 -4.02017-03-30 -6.02017-03-29 -5.02017-03-28 -5.0 每个数字对应我应该在相应日期添加或删除的天数.我想创建 我有一个dataFrame

[in] MyDates[out]  2017-04-04        -5.02017-04-03        -5.02017-03-31        -4.02017-03-30        -6.02017-03-29        -5.02017-03-28        -5.0

每个数字对应我应该在相应日期添加或删除的天数.我想创建一个新列,索引日期减去第一列中的天数.我知道我可以用DateOffset做到但我无法弄清楚如何…

谢谢!

解决方法 您可以将列转换为 TimedeltaIndexto_timedelta并添加()或减去( – )值:

df['new'] = df.index - pd.timedeltaIndex(df['col'],unit='d')print (df)            col        new2017-04-04 -5.0 2017-04-092017-04-03 -5.0 2017-04-082017-03-31 -4.0 2017-04-042017-03-30 -6.0 2017-04-052017-03-29 -5.0 2017-04-032017-03-28 -5.0 2017-04-02

要么:

df['new'] = df.index + pd.to_timedelta(df['col'],unit='d')print (df)            col        new2017-04-04 -5.0 2017-03-302017-04-03 -5.0 2017-03-292017-03-31 -4.0 2017-03-272017-03-30 -6.0 2017-03-242017-03-29 -5.0 2017-03-242017-03-28 -5.0 2017-03-23

如果SerIEs as input添加to_frame

df = s.to_frame('date')df['new'] = df.index - pd.timedeltaIndex(df['date'],unit='d')print (df)            date        new2017-04-04  -5.0 2017-04-092017-04-03  -5.0 2017-04-082017-03-31  -4.0 2017-04-042017-03-30  -6.0 2017-04-052017-03-29  -5.0 2017-04-032017-03-28  -5.0 2017-04-02
总结

以上是内存溢出为你收集整理的python – DateOffset Panda减法全部内容,希望文章能够帮你解决python – DateOffset Panda减法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1193915.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存