0

I have a dataframe where I move line by line through it to jump the image. I have empty values ​​marked as np.nan

DataFrame example:

enter image description here

I need to fulfill a condition under which, if the value of the passed variable is not equal to np.nan, then the image jump will be performed.

def save_all_images(df):
    print('Row count is dataframe:', df.shape[0])
    print('W_type image: ', df.w_type.count())
    print('Z_type image: ', df.z_type.count())
    print('Y_type image: ', df.y_type.count())
    print('R_type image: ', df.r_type.count())
    print('Q_type image: ', df.q_type.count())
    print('X_type image: ', df.x_type.count())
    
    print('\nStart save images on server')

    number = 0
    y_number = df.y_type.count()
    for i in df.iterrows():
        number = number + 1
        date = i[1][2]
        id_image = i[1][0]
        type_w = i[1][5]
        type_x = i[1][10]
        type_y = i[1][7]
        type_q = i[1][9]

        if type_w != np.nan:
            print(type(type_w))
            time.sleep(1)
            img_type_w = Image.open(requests.get(type_w, stream=True).raw)
            img_type_w.save(directory + 'w_' + str(id_image) + '_' + str(date) + '.jpg')
            img_type_w.close()

I know it doesn't look very good. I am getting the following error:

MissingSchema                             Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9108/1715813655.py in <module>
----> 1 save_all_images(df)

~\AppData\Local\Temp/ipykernel_9108/3837361018.py in save_all_images(df)
     24             print(type(type_w))
     25             time.sleep(1)
---> 26             img_type_w = Image.open(requests.get(type_w, stream=True).raw)
     27             img_type_w.save(directory + 'w_' + str(id_image) + '_' + str(date) + '.jpg')
     28             img_type_w.close()

~\AppData\Roaming\Python\Python39\site-packages\requests\api.py in get(url, params, **kwargs)
     73     """
     74 
---> 75     return request('get', url, params=params, **kwargs)
     76 
     77 

~\AppData\Roaming\Python\Python39\site-packages\requests\api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62 
     63 

~\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    513             hooks=hooks,
    514         )
--> 515         prep = self.prepare_request(req)
    516 
    517         proxies = proxies or {}

~\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py in prepare_request(self, request)
    441 
    442         p = PreparedRequest()
--> 443         p.prepare(
    444             method=request.method.upper(),
    445             url=request.url,

~\AppData\Roaming\Python\Python39\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
    316 
    317         self.prepare_method(method)
--> 318         self.prepare_url(url, params)
    319         self.prepare_headers(headers)
    320         self.prepare_cookies(cookies)

~\AppData\Roaming\Python\Python39\site-packages\requests\models.py in prepare_url(self, url, params)
    390             error = error.format(to_native_string(url, 'utf8'))
    391 
--> 392             raise MissingSchema(error)
    393 
    394         if not host:

MissingSchema: Invalid URL 'nan': No scheme supplied. Perhaps you meant http://nan?
1
  • 1
    I would propose to use df.loc in combination with df.notna. Since you did not deliver reproducible code, I unfortunately can not provide a more exact answer, but you should be able to find a solution with these functions and a bit of experimentation.
    – André
    Mar 22, 2022 at 15:12

1 Answer 1

1

You should knok np.nan == np.nan return False so use instead np.isnan:

if not np.isnan(type_w):
    print(type(type_w))
    ....

Or if it is string type:

if type_w != 'nan':
#if type_w.replace(' ', '').lower() != 'nan': # if blanks or upper case
    print(type(type_w))
    ....
2
  • add error: ``` ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''``` Mar 22, 2022 at 15:07
  • @kostyaivanov you should check with type_w != 'nan'
    – ansev
    Mar 22, 2022 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.