0

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 there is a function called to_datetime(), but I can't call this function on the series.

Thank you

3
  • It is really problematic, because it automatically cast to pandas datetimes, but is possible convert to numpy array of dates by df.Date.dt.to_pydatetime() Commented Feb 6, 2017 at 12:42
  • So this df.Date = df.Date.dt.to_pydatetime() still return Date datetime64[ns] Commented Feb 6, 2017 at 12:44
  • better it is explain here Commented Feb 6, 2017 at 13:08

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

@erip great solution! Works very well.
@codebase5000 It's not my solution! It's SerialDev's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.