python提取字符串中的日期_从pandas列中的日期字符串列中提取年份

我有一个包含日期列的数据集:df.faultDate.head()

0 2014-07-13

1 2014-07-13

2 2014-07-12

3 2014-07-12

4 2014-07-13

我只想从中得到几年。在

^{pr2}$

但是:df.faultDate.apply(dt.datetime.strptime, args=('%Y-%m-%d'))

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in ()

----> 1 df.faultDate.apply(dt.datetime.strptime, args=('%Y-%m-%d'))

/home/shiny/anaconda3/lib/python3.4/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)

2058 values = lib.map_infer(values, lib.Timestamp)

2059

-> 2060 mapped = lib.map_infer(values, f, convert=convert_dtype)

2061 if len(mapped) and isinstance(mapped[0], Series):

2062 from pandas.core.frame import DataFrame

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:58435)()

/home/shiny/anaconda3/lib/python3.4/site-packages/pandas/core/series.py in (x)

2047

2048 if kwds or args and not isinstance(func, np.ufunc):

-> 2049 f = lambda x: func(x, *args, **kwds)

2050 else:

2051 f = func

TypeError: strptime() takes exactly 2 arguments (9 given)

我也不知道在成功地将字符串转换为datetime之后,如何传递.year参数。我可以编写一个包装器函数,将每一行作为输入,然后提取年份,但我认为了解如何应用pandas语法以供将来参考是很有用的。谢谢!在

你可能感兴趣的:(python提取字符串中的日期)