· clickhouse python til

ClickHouse - AttributeError: 'NoneType' object has no attribute 'array'

I was querying a ClickHouse server from a Python script a couple of days ago and ran into an error message when trying to create a Pandas DataFrame. In this blog, we’ll see the error message and how to fix it.

I’m gonna assume that we have a ClickHouse Server running and we’re going to connect to it like this:

./clickhouse client
ClickHouse client version (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 23.11.1 revision 54466.

Next, let’s create a table with one column and populate it with some values:

CREATE TABLE x(value Int32)
ENGINE MergeTree()
ORDER BY tuple();

INSERT INTO x VALUES(1) (4) (7) (12) (24);

Now we’re going to query this table from a Python script. We’ll first install clickhouse-connect:

pip install clickhouse-connect

And then we’ll try to get back a Pandas DataFrame containing our result:

import clickhouse_connect

client = clickhouse_connect.get_client(host='localhost')

client.query_df("FROM x SELECT *")
File ~/Library/Caches/pypoetry/virtualenvs/fastembed-clickhouse-4QwM5Uh0-py3.11/lib/python3.11/site-packages/clickhouse_connect/datatypes/string.py:40, in String._finalize_column(self, column, ctx)
     38 def _finalize_column(self, column: Sequence, ctx: QueryContext) -> Sequence:
     39     if ctx.use_extended_dtypes and self.read_format(ctx) == 'native':
---> 40         return pd.array(column, dtype=pd.StringDtype())
     41     if ctx.use_numpy and ctx.max_str_len:
     42         return np.array(column, dtype=f'<U{ctx.max_str_len}')

AttributeError: 'NoneType' object has no attribute 'array'

It looks like we need to install Pandas, which I guess kinda makes sense.

pip install pandas

And then if we re-run our script, we’ll see the following output:

0      1
1      4
2      7
3     12
4     24

Job done.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket