{"cells":[{"block_group":"8758a76016b64ef3bbdf612eb8f71a15","cell_type":"markdown","execution_count":null,"metadata":{"deepnote_cell_height":271.125,"tags":[],"cell_id":"8758a76016b64ef3bbdf612eb8f71a15","deepnote_block_group":"8758a76016b64ef3bbdf612eb8f71a15","deepnote_cell_type":"markdown","deepnote_sorting_key":"0","deepnote_source":""},"source":""},{"block_group":"bc634738f7654bc0a95ed31d13aad0e4","cell_type":"markdown","execution_count":null,"metadata":{"deepnote_cell_height":215.59375,"tags":[],"cell_id":"bc634738f7654bc0a95ed31d13aad0e4","deepnote_block_group":"bc634738f7654bc0a95ed31d13aad0e4","deepnote_cell_type":"markdown","deepnote_sorting_key":"1","deepnote_source":"# Query InfluxDB from Deepnote\n\nThis template shows you how to fetch and visualize data from InfluxDB v2 in Deepnote. As an example, we're using [the simulated airSensors dataset built into InfluxDB](https://docs.influxdata.com/influxdb/v2.2/reference/sample-data/#air-sensor-sample-data). Click \"Run notebook\" at the top of this window to see it in action.\n\nThis template assumes you are familiar with the Flux language used to query InfluxDB.\n\n_Want to query your own data?_ Follow the next steps at the end of the notebook 👇"},"source":"# Query InfluxDB from Deepnote\n\nThis template shows you how to fetch and visualize data from InfluxDB v2 in Deepnote. As an example, we're using [the simulated airSensors dataset built into InfluxDB](https://docs.influxdata.com/influxdb/v2.2/reference/sample-data/#air-sensor-sample-data). Click \"Run notebook\" at the top of this window to see it in action.\n\nThis template assumes you are familiar with the Flux language used to query InfluxDB.\n\n_Want to query your own data?_ Follow the next steps at the end of the notebook 👇"},{"block_group":"f135d8ba5dc04e0885b2934c22f66524","cell_type":"code","execution_count":null,"metadata":{"execution_start":1656011532022,"execution_millis":97,"source_hash":"ba30f4a5","tags":[],"cell_id":"f135d8ba5dc04e0885b2934c22f66524","deepnote_to_be_reexecuted":false,"deepnote_block_group":"f135d8ba5dc04e0885b2934c22f66524","deepnote_cell_type":"code","deepnote_sorting_key":"2","deepnote_content_hash":"ba30f4a5","deepnote_execution_started_at":"2022-06-23T19:12:12.022Z","deepnote_execution_finished_at":"2022-06-23T19:12:12.119Z","deepnote_source":"import os\nfrom influxdb_client import InfluxDBClient"},"outputs":[],"source":"import os\nfrom influxdb_client import InfluxDBClient"},{"block_group":"9f068fba029c47ca8895d2dab54c5014","cell_type":"markdown","execution_count":null,"metadata":{"deepnote_cell_height":108.390625,"tags":[],"cell_id":"9f068fba029c47ca8895d2dab54c5014","deepnote_block_group":"9f068fba029c47ca8895d2dab54c5014","deepnote_cell_type":"markdown","deepnote_sorting_key":"3","deepnote_source":"## Query data from InfluxDB\n\nWe use our API key to compose a query in the Flux query language and run it against our InfluxDB instance."},"source":"## Query data from InfluxDB\n\nWe use our API key to compose a query in the Flux query language and run it against our InfluxDB instance."},{"block_group":"93f6c0dc5b654f778793ec28965f97db","cell_type":"code","execution_count":null,"metadata":{"execution_start":1656011532163,"execution_millis":1,"source_hash":"71c6278b","tags":[],"cell_id":"93f6c0dc5b654f778793ec28965f97db","deepnote_to_be_reexecuted":false,"deepnote_block_group":"93f6c0dc5b654f778793ec28965f97db","deepnote_cell_type":"code","deepnote_sorting_key":"4","deepnote_content_hash":"71c6278b","deepnote_execution_started_at":"2022-06-23T19:12:12.163Z","deepnote_execution_finished_at":"2022-06-23T19:12:12.164Z","deepnote_source":"# Change this to your token\n# You can use environment variables, too: https://docs.deepnote.com/environment/environment-variables\nINFLUXDB_TOKEN = \"wxKB63AOv5lUxOq_L5Yew2vEQS96a4f5bWGu7mQbRMMbYAXcyMxU5Q_SABKkhc6VI0Vm4P2y_xd5XgdMclHu_g==\"\n\n# Change this to the URL of your InfluxDB instance\nINFLUXDB_URL = \"https://us-central1-1.gcp.cloud2.influxdata.com\"\n\n# Change this to the name of your organization\nINFLUXDB_ORG = \"deepnote-examples\"\n\nclient = InfluxDBClient(\n url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG\n)\n"},"outputs":[],"source":"# Change this to your token\n# You can use environment variables, too: https://docs.deepnote.com/environment/environment-variables\nINFLUXDB_TOKEN = \"wxKB63AOv5lUxOq_L5Yew2vEQS96a4f5bWGu7mQbRMMbYAXcyMxU5Q_SABKkhc6VI0Vm4P2y_xd5XgdMclHu_g==\"\n\n# Change this to the URL of your InfluxDB instance\nINFLUXDB_URL = \"https://us-central1-1.gcp.cloud2.influxdata.com\"\n\n# Change this to the name of your organization\nINFLUXDB_ORG = \"deepnote-examples\"\n\nclient = InfluxDBClient(\n url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG\n)\n"},{"block_group":"d133c45f641d43c6ab71c9873077fbf6","cell_type":"markdown","execution_count":null,"metadata":{"deepnote_cell_height":74.796875,"tags":[],"cell_id":"d133c45f641d43c6ab71c9873077fbf6","deepnote_block_group":"d133c45f641d43c6ab71c9873077fbf6","deepnote_cell_type":"markdown","deepnote_sorting_key":"5","deepnote_source":"The Python client returns the result as a Pandas dataframe, which means we can leverage Deenote's built-in dataframe analysis. You can filter and sort each column to drill into the data."},"source":"The Python client returns the result as a Pandas dataframe, which means we can leverage Deenote's built-in dataframe analysis. You can filter and sort each column to drill into the data."},{"block_group":"172de5071ad64136a53c2547e2cecfe3","cell_type":"code","execution_count":null,"metadata":{"execution_start":1656011532164,"execution_millis":1476,"source_hash":"f1c8972e","deepnote_table_state":{"sortBy":[],"filters":[],"pageSize":10,"pageIndex":4},"tags":[],"cell_id":"172de5071ad64136a53c2547e2cecfe3","deepnote_table_loading":false,"deepnote_to_be_reexecuted":false,"deepnote_block_group":"172de5071ad64136a53c2547e2cecfe3","deepnote_cell_type":"code","deepnote_sorting_key":"6","deepnote_content_hash":"f1c8972e","deepnote_execution_started_at":"2022-06-23T19:12:12.164Z","deepnote_execution_finished_at":"2022-06-23T19:12:13.640Z","deepnote_source":"query_api = client.query_api()\n\nquery = \"\"\"import \"influxdata/influxdb/sample\"\n\nsample.data(set: \"airSensor\")\n |> filter(fn: (r) => r._field == \"temperature\")\n |> range(start: -45m)\n |> aggregateWindow(every: 1m, fn: mean)\"\"\"\n\n# influxdb_client gives us Pandas dataframes out-of-the-box\ndf = query_api.query_data_frame(query)\n\ndf\n"},"outputs":[{"output_type":"execute_result","execution_count":3,"data":{"application/vnd.deepnote.dataframe.v3+json":{"column_count":9,"row_count":368,"columns":[{"name":"result","dtype":"object","stats":{"unique_count":1,"nan_count":0,"categories":[{"name":"_result","count":368}]}},{"name":"table","dtype":"int64","stats":{"unique_count":8,"nan_count":0,"min":"0","max":"7","histogram":[{"bin_start":0,"bin_end":0.7,"count":46},{"bin_start":0.7,"bin_end":1.4,"count":46},{"bin_start":1.4,"bin_end":2.0999999999999996,"count":46},{"bin_start":2.0999999999999996,"bin_end":2.8,"count":0},{"bin_start":2.8,"bin_end":3.5,"count":46},{"bin_start":3.5,"bin_end":4.199999999999999,"count":46},{"bin_start":4.199999999999999,"bin_end":4.8999999999999995,"count":0},{"bin_start":4.8999999999999995,"bin_end":5.6,"count":46},{"bin_start":5.6,"bin_end":6.3,"count":46},{"bin_start":6.3,"bin_end":7,"count":46}]}},{"name":"_time","dtype":"datetime64[ns, tzlocal()]","stats":{"unique_count":46,"nan_count":0,"min":"2022-06-23 18:28:00+00:00","max":"2022-06-23 19:12:12.245438+00:00","histogram":[{"bin_start":1656008880000000000,"bin_end":1656009145224543700,"count":40},{"bin_start":1656009145224543700,"bin_end":1656009410449087500,"count":32},{"bin_start":1656009410449087500,"bin_end":1656009675673631500,"count":40},{"bin_start":1656009675673631500,"bin_end":1656009940898175200,"count":32},{"bin_start":1656009940898175200,"bin_end":1656010206122719000,"count":40},{"bin_start":1656010206122719000,"bin_end":1656010471347262700,"count":32},{"bin_start":1656010471347262700,"bin_end":1656010736571806500,"count":32},{"bin_start":1656010736571806500,"bin_end":1656011001796350500,"count":40},{"bin_start":1656011001796350500,"bin_end":1656011267020894200,"count":32},{"bin_start":1656011267020894200,"bin_end":1656011532245438000,"count":48}]}},{"name":"_start","dtype":"datetime64[ns, tzlocal()]","stats":{"unique_count":1,"nan_count":0,"min":"2022-06-23 18:27:12.245438+00:00","max":"2022-06-23 18:27:12.245438+00:00","histogram":null}},{"name":"_stop","dtype":"datetime64[ns, tzlocal()]","stats":{"unique_count":1,"nan_count":0,"min":"2022-06-23 19:12:12.245438+00:00","max":"2022-06-23 19:12:12.245438+00:00","histogram":null}},{"name":"_field","dtype":"object","stats":{"unique_count":1,"nan_count":0,"categories":[{"name":"temperature","count":368}]}},{"name":"_measurement","dtype":"object","stats":{"unique_count":1,"nan_count":0,"categories":[{"name":"airSensors","count":368}]}},{"name":"sensor_id","dtype":"object","stats":{"unique_count":8,"nan_count":0,"categories":[{"name":"TLM0100","count":46},{"name":"TLM0101","count":46},{"name":"6 others","count":276}]}},{"name":"_value","dtype":"float64","stats":{"unique_count":312,"nan_count":56,"min":"70.66973156692363","max":"75.62100941370004","histogram":[{"bin_start":70.66973156692363,"bin_end":71.16485935160127,"count":59},{"bin_start":71.16485935160127,"bin_end":71.65998713627891,"count":38},{"bin_start":71.65998713627891,"bin_end":72.15511492095655,"count":30},{"bin_start":72.15511492095655,"bin_end":72.6502427056342,"count":25},{"bin_start":72.6502427056342,"bin_end":73.14537049031183,"count":21},{"bin_start":73.14537049031183,"bin_end":73.64049827498947,"count":22},{"bin_start":73.64049827498947,"bin_end":74.13562605966712,"count":5},{"bin_start":74.13562605966712,"bin_end":74.63075384434475,"count":29},{"bin_start":74.63075384434475,"bin_end":75.1258816290224,"count":36},{"bin_start":75.1258816290224,"bin_end":75.62100941370004,"count":47}]}},{"name":"_deepnote_index_column","dtype":"int64"}],"rows":[{"result":"_result","table":0,"_time":"2022-06-23T19:08:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":40},{"result":"_result","table":0,"_time":"2022-06-23T19:09:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":41},{"result":"_result","table":0,"_time":"2022-06-23T19:10:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":42},{"result":"_result","table":0,"_time":"2022-06-23T19:11:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":43},{"result":"_result","table":0,"_time":"2022-06-23T19:12:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":44},{"result":"_result","table":0,"_time":"2022-06-23T19:12:12.245438","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0100","_value":"nan","_deepnote_index_column":45},{"result":"_result","table":1,"_time":"2022-06-23T18:28:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0101","_value":71.56077485961086,"_deepnote_index_column":46},{"result":"_result","table":1,"_time":"2022-06-23T18:29:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0101","_value":71.6057852910876,"_deepnote_index_column":47},{"result":"_result","table":1,"_time":"2022-06-23T18:30:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0101","_value":71.60346207425799,"_deepnote_index_column":48},{"result":"_result","table":1,"_time":"2022-06-23T18:31:00.000000","_start":"2022-06-23T18:27:12.245438","_stop":"2022-06-23T19:12:12.245438","_field":"temperature","_measurement":"airSensors","sensor_id":"TLM0101","_value":71.62870434562065,"_deepnote_index_column":49}]},"text/plain":" result table _time \\\n0 _result 0 2022-06-23 18:28:00+00:00 \n1 _result 0 2022-06-23 18:29:00+00:00 \n2 _result 0 2022-06-23 18:30:00+00:00 \n3 _result 0 2022-06-23 18:31:00+00:00 \n4 _result 0 2022-06-23 18:32:00+00:00 \n.. ... ... ... \n363 _result 7 2022-06-23 19:09:00+00:00 \n364 _result 7 2022-06-23 19:10:00+00:00 \n365 _result 7 2022-06-23 19:11:00+00:00 \n366 _result 7 2022-06-23 19:12:00+00:00 \n367 _result 7 2022-06-23 19:12:12.245438+00:00 \n\n _start _stop \\\n0 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n1 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n2 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n3 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n4 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n.. ... ... \n363 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n364 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n365 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n366 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n367 2022-06-23 18:27:12.245438+00:00 2022-06-23 19:12:12.245438+00:00 \n\n _field _measurement sensor_id _value \n0 temperature airSensors TLM0100 71.101404 \n1 temperature airSensors TLM0100 71.151548 \n2 temperature airSensors TLM0100 71.194573 \n3 temperature airSensors TLM0100 71.242369 \n4 temperature airSensors TLM0100 71.178880 \n.. ... ... ... ... \n363 temperature airSensors TLM0203 NaN \n364 temperature airSensors TLM0203 NaN \n365 temperature airSensors TLM0203 NaN \n366 temperature airSensors TLM0203 NaN \n367 temperature airSensors TLM0203 NaN \n\n[368 rows x 9 columns]","text/html":"
| \n | result | \ntable | \n_time | \n_start | \n_stop | \n_field | \n_measurement | \nsensor_id | \n_value | \n
|---|---|---|---|---|---|---|---|---|---|
| 0 | \n_result | \n0 | \n2022-06-23 18:28:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0100 | \n71.101404 | \n
| 1 | \n_result | \n0 | \n2022-06-23 18:29:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0100 | \n71.151548 | \n
| 2 | \n_result | \n0 | \n2022-06-23 18:30:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0100 | \n71.194573 | \n
| 3 | \n_result | \n0 | \n2022-06-23 18:31:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0100 | \n71.242369 | \n
| 4 | \n_result | \n0 | \n2022-06-23 18:32:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0100 | \n71.178880 | \n
| ... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n
| 363 | \n_result | \n7 | \n2022-06-23 19:09:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0203 | \nNaN | \n
| 364 | \n_result | \n7 | \n2022-06-23 19:10:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0203 | \nNaN | \n
| 365 | \n_result | \n7 | \n2022-06-23 19:11:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0203 | \nNaN | \n
| 366 | \n_result | \n7 | \n2022-06-23 19:12:00+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0203 | \nNaN | \n
| 367 | \n_result | \n7 | \n2022-06-23 19:12:12.245438+00:00 | \n2022-06-23 18:27:12.245438+00:00 | \n2022-06-23 19:12:12.245438+00:00 | \ntemperature | \nairSensors | \nTLM0203 | \nNaN | \n
368 rows × 9 columns
\n