Skip to content Skip to sidebar Skip to footer

Pandas Convert Series From Datetimestamp To Datetime.datetime Object

I have a pandas' dataframe with a column that is of datatype: datetime64[ns]. How do I convert the whole series to a Python's Datetime.datetime objects? On the individual object

Solution 1:

If performance is a concern I would advise to use the following function to convert those columns to 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

Post a Comment for "Pandas Convert Series From Datetimestamp To Datetime.datetime Object"