Pandas Pivot - “TypeError:'NoneType'对象不可迭代”

时间:2015-08-12 03:42:26

标签: python sql pandas pivot nonetype

Pandas read_SQL无效,我正在尝试访问SQL pivot功能(例1)。我尝试过读取所有数据,但Pandas也没有处理数据。

示例1

sql_status_20 = """
SET NOCOUNT ON
USE [HilltopSamplerFix]

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

select @ColumnName = STUFF((SELECT distinct ',' +
QUOTENAME(Name)
FROM [Hilltopsamplerfix].[dbo].[tempHilltopWaterQualityExtractAll]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '');


SET @DynamicPivotQuery =
N'SELECT
SiteID,SiteName,RunDate,SampleTime,SampleID,' + @ColumnName + '
FROM [Hilltopsamplerfix].[dbo].[tempHilltopWaterQualityExtractAll]
PIVOT(max(Value)
FOR Name IN (' + @ColumnName + ')) AS PVTTable'

EXEC sp_executesql @DynamicPivotQuery
"""
connection_string = '...works...'
cnxn = pyodbc.connect(connection_string)
test = pd.read_sql(sql_status_20, cnxn)
cnxn.close()

错误

columns = [col.desc中col_desc的col_desc [0]]

TypeError:'NoneType'对象不可迭代

2 个答案:

答案 0 :(得分:0)

老问题,但我遇到了与上述相同的问题。问题在于USE [HilltopSamplerFix]。连接字符串中已经提到了数据库名称,因此可以删除此部分。

答案 1 :(得分:0)

我遇到了类似的问题,删除USE [DatabaseName]子句解决了该问题。我还在连接字符串中指定了数据库。