0

In Python 3, I have a list of datetime objects that I would like, using numpy, to wrap (python modulus % operator) by dividing by a fixed time period represented as a python timedelta. The goal is to derive a numpy array containing the remainders.

Consider:

import numpy as np
from datetime import datetime,timedelta
t=[]
for i in range(10):
    t.append(datetime.strptime('1066-10-1%i'%i, '%Y-%m-%d'))

delta_t = timedelta(days=2)

# Apply np.fmod(), but run into type conversion difficulties

How can I use numpy to achieve the equivalent of the following operation?

t_mod = t % delta_t

I have tried converting to numpy datetime64 and timedelta64 objects, so far in vain. I am happy to flesh out error messages if that would be useful, or provide any other information people may require.

Broader question: If this works for e.g. np.fmod(), would it work for other useful numpy functions? Is anything off-limits?

Or should I just convert everything to floats and have done with it? Or should I go via strings?

I don't want to use pandas or Python 2.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
jtlz2
  • 7,700
  • 9
  • 64
  • 114
  • There isn't much math defined for dates. The difference between dates results in a timedelta (not another date). A date plus/minus a timedelta results in a date. Division or multiplication is not defined. – hpaulj Nov 05 '19 at 08:01
  • @hpaulj OK, so actually something like this is a start: https://stackoverflow.com/questions/865618/how-can-i-perform-divison-on-a-datetime-timedelta-in-python – jtlz2 Nov 05 '19 at 09:43
  • What’s the context for this, what are you trying to do? – AMC Nov 05 '19 at 15:30

0 Answers0