考虑
In [99]: d = pd.to_datetime({'year':[2016], 'month':[06], 'day':[01]})
In [100]: d1 = pd.to_datetime({'year':[2016], 'month':[01], 'day':[01]})
In [101]:d - d1
Out[101]:
0 152 days
dtype: timedelta64[ns]
Run Code Online (Sandbox Code Playgroud)
但当我尝试对整个专栏执行此操作时,这给我带来了麻烦。考虑:
df['Age'] = map(lambda x:x - pd.to_datetime({'year':[2016], 'month':[06], 'day':[01]}), df['Manager_DoB'])
Run Code Online (Sandbox Code Playgroud)
df['Manager_Dob']是一列日期时间对象。它标记以下错误:
TypeError: can only operate on a datetime with a rhs of a timedelta/DateOffset for addition and subtraction, but the operator [__rsub__] was passed
您不需要使用map*,您可以从日期时间列/系列中减去时间戳:
In [11]: d = pd.to_datetime({'year':[2016], 'month':[6], 'day':[1]})
In [12]: d
Out[12]:
0 2016-06-01
dtype: datetime64[ns]
In [13]: d[0] # This is the Timestamp you are actually interested in subtracting
Out[13]: Timestamp('2016-06-01 00:00:00')
In [14]: dates = pd.date_range(start="2016-01-01", periods=4)
In [15]: dates - d[0]
Out[15]: TimedeltaIndex(['-152 days', '-151 days', '-150 days', '-149 days'], dtype='timedelta64[ns]', freq=None)
Run Code Online (Sandbox Code Playgroud)
您可以使用构造函数更直接地获取时间戳:
In [21]: pd.Timestamp("2016-06-01")
Out[21]: Timestamp('2016-06-01 00:00:00')
Run Code Online (Sandbox Code Playgroud)
*你不应该将 python 的地图与 pandas 一起使用,更喜欢.apply.