将pandas.Timedelta添加到DataFrame中的datetime64系列或列

时间:2017-09-25 06:53:56

标签: pandas datetime timezone timezone-offset timedelta

这是一个带有datetime64 [ns]列的pandas数据帧。

In [69]: dataframe_with_datetime
   some_other_column                  ts
0                  0 2017-09-22 23:29:11

In [70]: dataframe_with_datetime.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 2 columns):
some_other_column    1 non-null int64
ts                   1 non-null datetime64[ns]
dtypes: datetime64[ns](1), int64(1)
memory usage: 96.0 bytes
In [72]: import datetime
In [73]: offset = datetime.timedelta(hours=5, minutes=30)
Out[74]: datetime.timedelta(0, 19800)

In [75]: delta = pd.Timedelta(offset)
Out[76]: Timedelta('0 days 05:30:00')

In [77]: dataframe_with_datetime['ts']
0   2017-09-22 23:29:11
Name: ts, dtype: datetime64[ns]

考虑到这样的pandas数据帧,我需要在这个pandas列中添加/减去时间增量,但是我得到以下异常:

In [78]: dataframe_with_datetime['ts'] - delta
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-78-6d6985a3c867> in <module>()
----> 1 dts['ts'] - td

/usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in wrapper(left, right, name, na_op)
    694         left, right = _align_method_SERIES(left, right)
    695
--> 696         converted = _Op.get_op(left, right, name, na_op)
    697
    698         left, right = converted.left, converted.right

/usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in get_op(cls, left, right, name, na_op)
    328             return _Op(left, right, name, na_op)
    329         else:
--> 330             return _TimeOp(left, right, name, na_op)
    331
    332

/usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in __init__(self, left, right, name, na_op)
    341         super(_TimeOp, self).__init__(left, right, name, na_op)
    342
--> 343         lvalues = self._convert_to_array(left, name=name)
    344         rvalues = self._convert_to_array(right, name=name, other=lvalues)
    345

/usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in _convert_to_array(self, values, name, other)
    452             supplied_dtype = values.dtype
    453         inferred_type = supplied_dtype or lib.infer_dtype(values)
--> 454         if (inferred_type in ('datetime64', 'datetime', 'date', 'time') or
    455                 is_datetimetz(inferred_type)):
    456             # if we have a other of timedelta, but use pd.NaT here we

TypeError: data type "datetime" not understood

2 个答案:

答案 0 :(得分:1)

您可以逐个对每个datetime64对象执行timedelta减法。

In [85]: dataframe_with_datetime['ts'][0]
Out[85]: Timestamp('2017-09-22 23:29:11')

In [86]: dataframe_with_datetime['ts'][0] + delta
Out[86]: Timestamp('2017-09-23 04:59:11')

因此,要为Series执行此操作,请将系列转换为numpy数组,然后添加delta

dataframe_with_datetime['delta_ts'] = dataframe_with_datetime['ts'].values + delta

   some_other_column                  ts            delta_ts
0                  0 2017-09-22 23:29:11 2017-09-23 04:59:11

答案 1 :(得分:1)

对我来说,它工作得很好,pandas.Timedelta具有与datetime.timedelta相同的参数,因此解决方案很简单:

d = {'some_other_column': {0: 0}, 'ts': {0: pd.Timestamp('2017-09-22 23:29:11')}}
df = pd.DataFrame(d)
print (df)
   some_other_column                  ts
0                  0 2017-09-22 23:29:11

delta = pd.Timedelta(hours=5, minutes=30)
df['ts1'] = df['ts'] - delta
df['ts2'] = df['ts'] + delta
print (df)
   some_other_column                  ts                 ts1  \
0                  0 2017-09-22 23:29:11 2017-09-22 17:59:11   

                  ts2  
0 2017-09-23 04:59:11  

我的版本:

print (pd.show_versions())

python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7

pandas: 0.20.3
numpy: 1.12.1