0

I have a column of dates. I want to calculate the number of days between each datetime in the array and a specified date.

import datetime    
print(mydates[0])

2013-04-30T20:00:00.000000000-0400

print(mydates[0] - datetime.date(2013,9,20))

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

I need to create an array with the number of days between those dates but, more generally, I will also need to know how to convert an array of datetimes into dates.

I found another post that converted datetime.datetime.now() into a date but I don't know how the syntax would work to use that on a an existing set of date times.

Here is the link

Community
  • 1
  • 1
Chris
  • 9,530
  • 8
  • 34
  • 58
  • "I found another post that converted datetime.datetime.now() into a date but I don't know how the syntax would work to use that on a an existing set of date times." Well then, link to that other post, and show us the syntax it used and where you're stuck trying to apply the same thing, don't just tell us it exists. – abarnert Sep 02 '14 at 19:20
  • http://stackoverflow.com/questions/3743222/how-do-i-convert-datetime-to-date-in-python – Chris Sep 02 '14 at 19:22
  • If I plug in my variable in place of .now() in the answer in the link above it doesn't work – Chris Sep 02 '14 at 19:23
  • OK, that post is about converting between `datetime.datetime` and `datetime.date`, not about converting between `numpy.datetime64` and `datetime.date`—or whatever type you're using. (Your question really needs more information to be answerable clearly—give us an [MCVE](http://stackoverflow.com/help/mcve), e.g., a simple example that creates a `mydates` array of two values of whatever type you're using, instead of just showing us the output of printing one out.) – abarnert Sep 02 '14 at 19:25

1 Answers1

1

Whatever numpy type you're using (presumably np.datetime64) and the types in the datetime module aren't implicitly convertible.

But they are explicitly convertible, which means all you need to do is explicitly convert:

>>> mydates[0] - np.datetime64(datetime.date(2013,9,20))
numpy.timedelta64(-1,'D')
>>> mydates - np.datetime64(datetime.date(2013,9,20))
array([-1, 0, -152], dtype='timedelta64['D'])

(Note that the 'D' there is because I'm using datetime values with day units; from your output, it looks like you're using nanosecond units. But the same applies generally.)


If you actually want to convert the array into an array of datetime.date objects, you can… but that's probably a bad idea. When you're using native numpy types, your arrays are compact and fast; when you're storing general Python types, arrays are no better than normal Python lists, and it's more misleading than helpful to pretend otherwise.

If you explained what you were actually trying to do, someone could probably explain a better way to do it than converting to an array of date objects.

abarnert
  • 313,628
  • 35
  • 508
  • 596
  • Thanks, this is really helpful and informative. I have a pandas DataFrame with 8 columns of stock prices and the column of datetimes is used as the index. Each column has a different 'event date'. I want to calculate the days between each columns event date and then merge the stock price data by 'days since event' so that I can compare price changes in the days before and after each companies event. – Chris Sep 02 '14 at 19:38
  • @Chris: I'm not sure what you're asking. (Please try posting an [MCVE](http://stackoverflow.com/help/mcve), a few lines of self-contained code that creates the values you want and shows what output you're hoping for and where you're stuck getting it.) If you just want to subtract a date from a datetime index, just subtract. If you want to convert a datetime index into a date-only index (who you calculate whole days, instead of partial days), you probably want `astype` or some other conversion function, or maybe to just create a new DataFrame from the existing one. – abarnert Sep 02 '14 at 20:15