Home > Software design >  EDIT: numpy operations on astropy time series objects
EDIT: numpy operations on astropy time series objects

Time:11-15

EDIT: the original title of this question was 'Unable to multiply two python arrays together',and the corresponding question is below. The error arose from the fact that list2 contained data that had implicit units of 'astropy.Time' and each element in the list was a 'time object'. The answer provided is a standard quick fix to enable regular numpy operations to be performed on such data (e.g., in the below case, where the time series output was from a lightkurve process)

I have two lists of numpy arrays in Python, one of which has 36 elements and the other one has 5, i.e.

list1 = [array1, array2, array3, array4, array5], 
list2 = [arrayA, arrayB, arrayC, arrayD, ...]

I am trying to multiply every element in list2 by, for instance, element 0 in list 1 (so array1 * list2). However, no matter how I try to implement this (for loop, while loop), Python returns the error 'Fatal Python error: Segmentation fault'. The same thing happens even if I try the test case: list1[0]*list2[0], or alternatively, np.multiply(list1[0], list2[0]) I have checked the length and dimensions of all the pertaining elements and they all are the same as each other (they're both 1D numpy arrays, and for e.g. len(list1[0]) = 2000 and len(list2[0]) = 2000 ), so I'm really confused on why I can't perform this basic multiplication? I am using the Spyder IDE, if that makes any difference, and would be super grateful for any advice, thanks!

CodePudding user response:

You're looking for np.outer. See here:

import numpy as np 
list1 = np.ones((5,))
list2 = np.linspace(-2,2,5)
rl = np.outer(list1, list2)
rl

Also, welcome to SO but please note that it would be better if you were to provide some sample code for people to work with. See how to write a minimal reproducible example.

CodePudding user response:

In the above case where one of the lists contains elements from a lightkurve time-series, the dtype of the elements is astropy.time

To convert, the 'to_value()' method needs to be used, where you want to convert your astropy time series to a series of floats and you know the unit of your data is MJD (Modified Julian Date):

converted_times = []

for i in range(len(list2)):
    newlist = [x.to_value('jd') for x in list2[i]]
    converted_times.append(newlist)

You can then use your astropy time series and do standard numpy manipulations with them

  • Related