0

I've been playing around with datetime in Python, and found out something rather confusing.

For both datetime.datetime and pandas.datetime, I use datetime.timedelta for time calculation:

testing = pd.date_range("2022-02-02","2022-03-02")
testing[0] + timedelta(days = 1)

-> works fine.

However, it seems that such method does not work on numpy.datetime64.
(the code is a bit idiotic. It's solely for understanding the concept)

testing = pd.date_range("2022-02-02","2022-03-02")
pd.Series(testing).unique()[0] + timedelta(days = 1)

-> throws an error.

Questions are:

  1. Why does pd.Series(testing).unique()[0] returns numpy.datetime64, instead of ordinary Timestamp?
  2. Why exactly is the code throwing me an error?
  3. Seems datetime.datetime and pandas.datetime are compatible, but numpy.datetime isn't. Why?
martineau
  • 112,593
  • 23
  • 157
  • 280
jay
  • 25
  • 3

0 Answers0