It appears that the applymap
is the culprit here :-)
When you have an empty result set of the read_sql
query, you wil get an empty dataframe. Eg:
In [2]: df = pd.DataFrame(columns=list('ABC'))
In [3]: df
Out[3]:
Empty DataFrame
Columns: [A, B, C]
Index: []
Using this empty dataframe, when you then call the applymap on this, it is apparantly converted to a Series, and then the count just gives a number:
In [10]: df2 = df.applymap(lambda x: np.nan if x == "" else x)
In [11]: df2
Out[11]:
A NaN
B NaN
C NaN
dtype: float64
In [12]: df2.count()
Out[12]: 0
while doing the count directly on the empty dataframe gives the desired output:
In [13]: df.count()
Out[13]:
A 0
B 0
C 0
dtype: int64
I don't know exactly why the applymap does this (or if it is a bug), but a simple solution for now would be to just do a quick if before the applymap:
if not len(df):
df = df.applymap(lambda x: np.nan if x == "" else x)
The reason that the above is a problem, is that the DataFrame
constructor does not accept a scalar as input data.