python - 将系列而不是整数传递给熊猫偏移量
问题描述
我有一个带有日期和数字的数据框(df)。我想将数字添加到日期。如何使用 pd.offsets() 将 df['additional_days'] 系列添加到 df['start_date'] 系列?有一个更好的方法吗?
开始日期附加天数
2018-03-29 360
2018-07-31 0
2018-11-01 360
2016-11-03 720
2018-12-04 480
我尝试时遇到错误
df['start_date'] + pd.offsets.Day(df['additional_days'])
这是错误
TypeError Traceback (most recent call last)
pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets._BaseOffset._validate_n()
/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in wrapper(self)
117 raise TypeError("cannot convert the series to "
--> 118 "{0}".format(str(converter)))
119
TypeError: cannot convert the series to <class 'int'>
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-76-03920804db29> in <module>
----> 1 df_test['start_date'] + pd.offsets.Day(df_test['additional_days'])
/opt/conda/lib/python3.6/site-packages/pandas/tseries/offsets.py in __init__(self, n, normalize)
2219 def __init__(self, n=1, normalize=False):
2220 # TODO: do Tick classes with normalize=True make sense?
-> 2221 self.n = self._validate_n(n)
2222 self.normalize = normalize
2223
pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets._BaseOffset._validate_n()
TypeError: `n` argument must be an integer, got <class 'pandas.core.series.Series'>
解决方案
采用pd.to_timedelta
import pandas as pd
#df['start_date'] = pd.to_datetime(df.start_date)
df['start_date'] + pd.to_timedelta(df.additional_days, unit='d')
#0 2019-03-24
#1 2018-07-31
#2 2019-10-27
#3 2018-10-24
#4 2020-03-28
#dtype: datetime64[ns]
推荐阅读
- python - os.environ.setdefault() 与 os.environ.putenv() 有什么区别
- javascript - 禁用捕获阶段传播时,Javascript 点击事件填充“目标”元素
- java - Java:在 JFrame 上的 WINDOW_CLOSING 的 dispatchEvent() 之后总是按下转义键
- android - 如何在 Retrofit android 中使用这个查询
- c# - 在 C# 的课堂上无法识别日期时间?
- python - 这个 lambda 函数是否有理由保留“self”的“旧实例”?
- r - 如何在有条件地定义向量的情况下提高循环的速度?
- python - 模型必须导入两次
- r - 是否有一个 R 函数允许我根据相同的字符将列表分组在一起,但如果有子目录,它将把它们分开
- amazon-s3 - 将文件复制并合并到另一个 S3 存储桶