Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoneType object is not iterable error in pandas

I am trying to pull some data from a stored proc on a sql server using python.

Here is my code:

import datetime as dt
import pyodbc
import pandas as pd

conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server Native client 11.0}',server = '*****, database = '**')

pd.read_sql("EXEC ******** '20140528'",conn)

I get the error: TypeError: 'NoneType' object is not iterable

I suspect this is because I have a cell in the sql table with value NULL but not sure if that's the true reason why I am getting the error. I have run many sql statements using the same code without any errors.

Here's the traceback:

In[39]: pd.read_sql("EXEC [dbo].[] '20140528'",conn)
Traceback (most recent call last):
  File "C:*", line 3032, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-39-68fb1c956dd7>", line 1, in <module>
    pd.read_sql("EXEC [dbo].[] '20140528'",conn)
  File "C:*", line 467, in read_sql
    chunksize=chunksize
  File "c:***", line 1404, in read_query
    columns = [col_desc[0] for col_desc in cursor.description]
TypeError: 'NoneType' object is not iterable
like image 504
Himanshu Gupta Avatar asked May 29 '15 16:05

Himanshu Gupta


People also ask

How do I fix NoneType object is not iterable in Python?

One way to avoid this error is to check before iterating on an object if that object is None or not. In addition, another way to handle this error: Python nonetype object is not iterable is to write the for loop in try-except block. Thirdly, it is to explicitly assign an empty list to the variable if it is None .

How do you fix a NoneType object in Python?

The TypeError: 'NoneType' object is not iterable error is raised when you try to iterate over an object whose value is equal to None. To solve this error, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list.

How do you fix an object not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

How do you fix TypeError argument of type NoneType is not iterable?

The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None .


2 Answers

Your sproc needs

SET NOCOUNT ON;

Without this sql will return the rowcount for the call, which will come back without a column name, causing the NoneType error.

like image 138
Corvus Avatar answered Sep 23 '22 03:09

Corvus


pd.read_sql() expects to have output to return, and tries to iterate through the output; that's where the TypeError is coming from. Instead, execute with a cursor object:

import datetime as dt
import pyodbc
import pandas as pd

conn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server Native client 11.0}',server = '*****', database = '**')
cur = conn.cursor()
cur.execute("EXEC ******** '20140528'")

You won't receive any output, but since none is expected, your code should run without error.

like image 25
allydh Avatar answered Sep 23 '22 03:09

allydh