[AstroPy] Summing an array

Rudolf Baer rbaer25 at gmail.com
Mon Jul 11 11:43:38 EDT 2022


print(np.version.version) 1.18.5

class 'numpy.ndarray

   1 print(np.version.version)      2 print(np.sum.__class__)----> 3
print(np.sum.__name__)      4 print(np.sum.__module__)
AttributeError: 'numpy.ndarray' object has no attribute '__name__'


2 print(np.sum.__class__)      3 #print(np.sum.__name__)----> 4
print(np.sum.__module__)
AttributeError: 'numpy.ndarray' object has no attribute '__module__'



On Mon, Jul 11, 2022 at 5:39 PM Rudolf Baer <rbaer25 at gmail.com> wrote:

> Hi Eric
> thanks for the quick reply. The following works:
>
> rpflux=[3140.,   2770. ,    3080.  ,   3170. ,    3050.,     3060.,]
> print(sum(rpflux))
> 18270.0
>
> The rpflux which I read with
> rpflux=np.genfromtxt(path,delimiter=',',dtype="float64",autostrip=True,skip_header=137,skip_footer=0,
> usecols=(19))
> comes from a csv-file which reads in part
> 3140
>
> 2770
> 3080
> 3170
> 3050
> 3060
> 3100 the whole file is printed out above as
>
> [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 ]
>
> it does contain nan s, but np.nansum still produces 'numpy.ndarray' object is not callable. What do I do wrong?
>
> With kind regards
>
> Rudolf
>
>
> PS: I can provide you with an extract of csv- file
>
>
>
>
>
> On Mon, Jul 11, 2022 at 4:04 PM Eric Jensen <ejensen1 at swarthmore.edu>
> wrote:
>
>> 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
>>
>>
>> _______________________________________________
>> 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/b9d7e9dd/attachment-0001.html>


More information about the AstroPy mailing list