8

I have following dataframe:

                      time         t            sp
598258 2017-01-02 00:00:00 -2.634766  89892.492188
598259 2017-01-02 01:00:00 -2.753906  89921.398438
598260 2017-01-02 02:00:00 -2.730469  89896.914062
598261 2017-01-02 03:00:00 -2.765625  89874.468750
598262 2017-01-02 04:00:00 -2.855469  89864.609375
598263 2017-01-02 05:00:00 -3.005859  89846.929688
598264 2017-01-02 06:00:00 -3.011719  89877.875000
598265 2017-01-02 07:00:00 -2.900391  89873.109375
598266 2017-01-02 08:00:00 -2.416016  89891.812500
598267 2017-01-02 09:00:00 -2.126953  89882.289062

and following code:

import pandas as pd
import numpy as np
from numba import jit
@jit(nopython=True)
def en(t,p):
    dd = np.empty((t.size),np.float16)
    for i in range(len(t)):
        dd[i]=p[i]/287.05/(t[i]+273.15)
    return dd

ex=en(dfx['t'].values,dfx['sp'].values)

I get error:

TypingError: non-precise type pyobject
[1] During: typing of argument at dd = np.empty((t.size),np.float16)

I know I must define precise type for numba to accept it, i think I did just that with np.float16 but error is present. Any help to resolve this is appreciated

1
  • @Ethan Tried that, still same error. Commented Dec 22, 2019 at 19:36

1 Answer 1

2

You have to change to float 64 or 32 depending on the precision you want:

from numba import jit
@jit(nopython=True)
def en(t,p):
    dd = np.empty((t.size),np.float64)
    for i in range(len(t)):
        dd[i]=p[i]/287.05/(t[i]+273.15)
    return dd

ex=en(dfx['t'].values,dfx['sp'].values)

output:

array([1.15764165, 1.15852414, 1.15810831, 1.1579697 , 1.15822752,
       1.15864432, 1.15906853, 1.15852962, 1.15669754, 1.15534144])

That will fix it!

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, that doesn't work on my machine. I get same error. I tried passing directly with dd = np.empty((dfx['t'].values.size),np.float64), i get error TypingError: cannot determine Numba type of <class 'pandas.core.frame.DataFrame'>
@user2727167, interesting. float16 definitely didnt work on my machine, it gave the same error you got, but the other two worked. What versions of the packages are your running, maybe you have an older version that just needs to be pip upgraded. just trying to think out of the box here, since the higher floats worked on mine. I have numpy '1.16.2', pandas 0.25.3, and numba '0.46.0'
I found out what the problem was. List of values in dfx['t'] was defined as float16 in original dataframe and even had some NaNs. When filtering this everything worked fine. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.