Pandas将系列从DateTimeStamp转换为datetime.datetime对象

时间:2017-02-06 12:37:54

标签: python pandas datetime

我有一只大熊猫'具有数据类型为datetime64[ns]的列的数据框。

如何将整个系列转换为Python的Datetime.datetime对象?在单个对象上有一个名为to_datetime()的函数,但我无法在该系列上调用此函数。

谢谢

1 个答案:

答案 0 :(得分:2)

如果考虑到性能,我建议使用以下函数将这些列转换为date_time

def lookup(s):
    """
    This is an extremely fast approach to datetime parsing.
    For large data, the same dates are often repeated. Rather than
    re-parse these, we store all unique dates, parse them, and
    use a lookup to convert all dates.
    """
    dates = {date:pd.to_datetime(date) for date in s.unique()}
    return s.apply(lambda v: dates[v])
to_datetime: 5799 ms
dateutil:    5162 ms
strptime:    1651 ms
manual:       242 ms
lookup:        32 ms