pythondatetimepandastimestamp

Why does pandas return timestamps instead of datetime objects when calling pd.to_datetime()?


According to the manual, pd.to_datetime() should create a datetime object.

Instead, when I call pd.to_datetime("2012-05-14"), I get a timestamp object! Calling to_datetime() on that object finally gives me a datetime object.

In [1]: pd.to_datetime("2012-05-14")
Out[1]: Timestamp('2012-05-14 00:00:00', tz=None)

In [2]: t = pd.to_datetime("2012-05-14")
In [3]: t.to_datetime()
Out[2]: datetime.datetime(2012, 5, 14, 0, 0)

Is there an explanation for this unexpected behaviour?


Solution

  • A Timestamp object is the way pandas works with datetimes, so it is a datetime object in pandas. But you expected a datetime.datetime object.
    Normally you should not care about this (it is just a matter of a different repr). As long as you are working with pandas, the Timestamp is OK. And even if you really want a datetime.datetime, most things will work (eg all methods), and otherwise you can use to_pydatetime to retrieve the datetime.datetime object.

    The longer story: