Repository: influxdata/influxdb-client-python

Issue:
Field type is set to `string` when writing `float32` type from Pandas DataFrame
<!--

Thank you for reporting a bug. 

* Please add a :+1: or comment on a similar existing bug report instead of opening a new one.
    * https://github.com/influxdata/influxdb-client-python/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+is%3Aclosed+sort%3Aupdated-desc+label%3Abug+
* Please check whether the bug can be reproduced with the latest release.
* The fastest way to fix a bug is to open a Pull Request.
    * https://github.com/influxdata/influxdb-client-python/pulls

-->

__Steps to reproduce:__
List the minimal actions needed to reproduce the behavior.

1. Create pandas DataFrame with field columns as type `float32`, e.g., `df.astype("float32")`
2. Write DataFrame using default write method (batching etc.), e.g., `write_api.write("bucket_name", record=df, data_frame_measurement_name="measurement", data_frame_tag_columns=["tag1", "tag2"])`

__Expected behavior:__
Expect python client to write data as `float` in the InfluxDB database. Note that converting the field columns with `df.astype(float)` does result in the expected behaviour with floats in the database.

__Actual behavior:__
Data is written as `string`. 

__Specifications:__
 - Client Version: 1.17.0
 - InfluxDB Version: 2.0.5
 - Platform: Linux (x86_64)


Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) Report
> Merging [#254](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) (105bd36) into [master](https://codecov.io/gh/influxdata/influxdb-client-python/commit/e2b92af09f8893d7ae732a8f0280da88e1a5844b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) (e2b92af) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254/graphs/tree.svg?width=650&height=150&src=pr&token=PcyupQSPFt&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata)](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata)

```diff
@@           Coverage Diff           @@
##           master     #254   +/-   ##
=======================================
  Coverage   91.93%   91.93%           
=======================================
  Files          25       25           
  Lines        2033     2033           
=======================================
  Hits         1869     1869           
  Misses        164      164           
```


| [Impacted Files](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata) | Coverage Δ | |
|---|---|---|
| [...fluxdb\_client/client/write/dataframe\_serializer.py](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata#diff-aW5mbHV4ZGJfY2xpZW50L2NsaWVudC93cml0ZS9kYXRhZnJhbWVfc2VyaWFsaXplci5weQ==) | `98.66% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata). Last update [e2b92af...105bd36](https://codecov.io/gh/influxdata/influxdb-client-python/pull/254?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=influxdata).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bf7506b..7695d7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@
 1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field`
 1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo`
 
+### Bug Fixes
+1. [#254](https://github.com/influxdata/influxdb-client-python/pull/254): Serialize `numpy` floats into LineProtocol
+
 ### Documentation
 1. [#255](https://github.com/influxdata/influxdb-client-python/pull/255): Fix invalid description for env var `INFLUXDB_V2_CONNECTION_POOL_MAXSIZE`
 
diff --git a/influxdb_client/client/write/dataframe_serializer.py b/influxdb_client/client/write/dataframe_serializer.py
index ee1d9ba..db5bd3b 100644
--- a/influxdb_client/client/write/dataframe_serializer.py
+++ b/influxdb_client/client/write/dataframe_serializer.py
@@ -158,7 +158,7 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
             field_value = f"{sep}{key_format}={{{val_format}}}i"
         elif issubclass(value.type, np.bool_):
             field_value = f'{sep}{key_format}={{{val_format}}}'
-        elif issubclass(value.type, np.float):
+        elif issubclass(value.type, np.floating):
             if null_columns[index]:
                 field_value = f"""{{"" if math.isnan({val_format}) else f"{sep}{key_format}={{{val_format}}}"}}"""
             else:


Test patch:
diff --git a/tests/test_WriteApiDataFrame.py b/tests/test_WriteApiDataFrame.py
index 7c9b15a..4b4317f 100644
--- a/tests/test_WriteApiDataFrame.py
+++ b/tests/test_WriteApiDataFrame.py
@@ -1,5 +1,3 @@
-import csv
-import os
 import time
 import unittest
 from datetime import timedelta
@@ -160,7 +158,7 @@ class DataSerializerTest(unittest.TestCase):
                          points[1])
 
     def test_write_field_bool(self):
-        from influxdb_client.extras import pd, np
+        from influxdb_client.extras import pd
 
         now = pd.Timestamp('2020-04-05 00:00+00:00')
 
@@ -252,7 +250,7 @@ class DataSerializerTest(unittest.TestCase):
         self.assertEqual("h2o,a=a,b=b,c=c level=2i 1586048400000000000", points[0])
 
     def test_escape_text_value(self):
-        from influxdb_client.extras import pd, np
+        from influxdb_client.extras import pd
 
         now = pd.Timestamp('2020-04-05 00:00+00:00')
         an_hour_ago = now - timedelta(hours=1)
@@ -273,7 +271,7 @@ class DataSerializerTest(unittest.TestCase):
         self.assertEqual("test,d=bar\\ foo b=\"goodbye cruel world\",c=2i 1586044800000000000", points[1])
 
     def test_with_default_tags(self):
-        from influxdb_client.extras import pd, np
+        from influxdb_client.extras import pd
         now = pd.Timestamp('2020-04-05 00:00+00:00')
         data_frame = pd.DataFrame(data={
                                       'value': [1, 2],
@@ -309,8 +307,7 @@ class DataSerializerTest(unittest.TestCase):
         self.assertEqual(True, (data_frame == original_data).all(axis = None), f'data changed; old:\n{original_data}\nnew:\n{data_frame}')
 
     def test_with_period_index(self):
-        from influxdb_client.extras import pd, np
-        now = pd.Timestamp('2020-04-05 00:00+00:00')
+        from influxdb_client.extras import pd
         data_frame = pd.DataFrame(data={
                                       'value': [1, 2],
                                 },
@@ -323,3 +320,16 @@ class DataSerializerTest(unittest.TestCase):
         self.assertEqual(2, len(points))
         self.assertEqual("h2o value=1i 1586048400000000000", points[0])
         self.assertEqual("h2o value=2i 1586052000000000000", points[1])
+
+    def test_write_num_py_floats(self):
+        from influxdb_client.extras import pd, np
+        now = pd.Timestamp('2020-04-05 00:00+00:00')
+
+        for np_float_type in [np.float, np.float16, np.float32, np.float64, np.float128]:
+            data_frame = pd.DataFrame([15.5], index=[now], columns=['level']).astype(np_float_type)
+            points = data_frame_to_list_of_points(data_frame=data_frame,
+                                                  data_frame_measurement_name='h2o',
+                                                  point_settings=PointSettings())
+            self.assertEqual(1, len(points))
+            self.assertEqual("h2o level=15.5 1586044800000000000", points[0], msg=f'Current type: {np_float_type}')
+