4

I am trying to convert a pandas.tslib.Timestamp object to datetime.df['orig_iss_dt'] is the pandas.tslib.Timestamp object. I used the answer here to try and do this, but find that print(type(origination)) still returns the same type.

df['orig_iss_dt'] = df['orig_iss_dt'].apply(lambda x: datetime.date(x.year,x.month,x.day)) 

for (i, row) in df:
    origination = row.orig_iss_dt
    print(type(origination))
1
  • What version of pandas are you using? Commented Aug 20, 2015 at 8:33

1 Answer 1

12

The easiest way to convert a pandas Timestamp to datetime.datetime is to use the to_pydatetime method. This can be called on Timestamp directly or on the series:

In [15]: s = pd.Series(pd.date_range('2012-01-01', periods=3))

In [16]: s
Out[16]:
0   2012-01-01
1   2012-01-02
2   2012-01-03
dtype: datetime64[ns]

In [17]: s[0]
Out[17]: Timestamp('2012-01-01 00:00:00')

In [18]: s.dt.to_pydatetime()
Out[18]:
array([datetime.datetime(2012, 1, 1, 0, 0),
       datetime.datetime(2012, 1, 2, 0, 0),
       datetime.datetime(2012, 1, 3, 0, 0)], dtype=object)

But note that this is often times not needed, as Timestamp is a subclass of datetime.datetime.

Sign up to request clarification or add additional context in comments.

Comments

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.