[AstroPy] Summing an array

Eric Jensen ejensen1 at swarthmore.edu
Mon Jul 11 10:03:40 EDT 2022


Hi Rudolf, 

Can you try restarting your Python interpreter, and stripping your code down to a minimal example that shows the problem in the shortest number of lines?   The first line of your code as written below references an array that doesn’t exist yet (‘rpflux’) on the right-hand side of the expression, so that would give an error right away.  So I suspect that you may have run several expressions before that and already have some defined variables, making it harder to tell what is happening here.  Starting with the third line (genfromtxt) would make sense (since you’re overwriting the array anyway there), as long as you define ‘path’ before that.  Giving a set of lines that, by themselves, show the problem when run straight through will make it easiest to troubleshoot. 

Strip it down to a minimal example, and we can go from there.

Also, note that if you have any NaNs in your array, then np.sum will return NaN.  If you want those to be treated as zeroes, use np.nansum instead. 

Eric


> On Jul 11, 2022, at 9:03 AM, Rudolf Baer <rbaer25 at gmail.com> wrote:
> 
> I have not been able to sum an array; the array is created as follows
> 
> rpflux = np.array(rpflux, dtype=float)
> rpflux=np.zeros(len(rptime),dtype='float64')
> rpflux=np.genfromtxt(path,delimiter=',',dtype="float64",autostrip=True,skip_header=137,skip_footer=0, usecols=(19))
> print(rpflux.astype('float64'))
> [3140.           nan 2770.     3080.     3170.     3050.     3060.
>  3100.     2990.     3120.     3060.           nan 3100.     3070.
>  3020.           nan 3200.     3150.     3350.     2990.     2910.
>  2920.     3030.     3000.     2800.     2980.     2860.     2840.
>  2610.     2650.     2610.     2650.     2490.     2490.     2540.
>  2510.     2470.           nan       nan   35.           nan 3099.
>        nan ] 
> 
> print (np.sum(rpflux))
> and 
> print (np.sum([rpflux]))
> result in
> 'numpy.ndarray' object is not callable'
> 
> I have tried several alternatives, none of them work. Any suggestions  would be appreciated.
> Rudolf Baer
> 
> Mac Os 10.15.6, python 3.7
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20220711/fb5f40e4/attachment.html>


More information about the AstroPy mailing list