Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialization of dataframes with NaN values and columns starting with digits #486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes
1. [#483](https://github.com/influxdata/influxdb-client-python/pull/483): Querying data if the `debug` is enabled
1. [#477](https://github.com/influxdata/influxdb-client-python/pull/477): Parsing date fails due to thread race
1. [#486](https://github.com/influxdata/influxdb-client-python/pull/486): Fix bug when serializing DataFrames that might occur if you're inserting NaN values and have columns starting with digits.

### Dependencies
1. [#472](https://github.com/influxdata/influxdb-client-python/pull/472): Update `RxPY` to `4.0.4`
Expand Down
2 changes: 1 addition & 1 deletion influxdb_client/client/write/dataframe_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def serialize(self, chunk_idx: int = None):
if self.first_field_maybe_null:
# When the first field is null (None/NaN), we'll have
# a spurious leading comma which needs to be removed.
lp = (re.sub('^(( |[^ ])* ),([a-zA-Z])(.*)', '\\1\\3\\4', self.f(p))
lp = (re.sub('^(( |[^ ])* ),([a-zA-Z0-9])(.*)', '\\1\\3\\4', self.f(p))
for p in filter(lambda x: _any_not_nan(x, self.field_indexes), _itertuples(chunk)))
return list(lp)
else:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_WriteApiDataFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,39 @@ def test_specify_timezone_period_time_index(self):
self.assertEqual('test value1=10i,value2=30i 1590307200000000000', points[0])
self.assertEqual('test value1=20i,value2=40i 1590310800000000000', points[1])

def test_serialization_for_nan_in_columns_starting_with_digits(self):
from influxdb_client.extras import pd
from influxdb_client.extras import np
data_frame = pd.DataFrame(data={
'1value': [np.nan, 30.0, np.nan, 30.0, np.nan],
'2value': [30.0, np.nan, np.nan, np.nan, np.nan],
'3value': [30.0, 30.0, 30.0, np.nan, np.nan],
'avalue': [30.0, 30.0, 30.0, 30.0, 30.0]
}, index=pd.period_range('2020-05-24 10:00', freq='H', periods=5))

points = data_frame_to_list_of_points(data_frame,
PointSettings(),
data_frame_measurement_name='test')

self.assertEqual(5, len(points))
self.assertEqual('test 2value=30.0,3value=30.0,avalue=30.0 1590314400000000000', points[0])
self.assertEqual('test 1value=30.0,3value=30.0,avalue=30.0 1590318000000000000', points[1])
self.assertEqual('test 3value=30.0,avalue=30.0 1590321600000000000', points[2])
self.assertEqual('test 1value=30.0,avalue=30.0 1590325200000000000', points[3])
self.assertEqual('test avalue=30.0 1590328800000000000', points[4])

data_frame = pd.DataFrame(data={
'1value': [np.nan],
'avalue': [30.0],
'bvalue': [30.0]
}, index=pd.period_range('2020-05-24 10:00', freq='H', periods=1))

points = data_frame_to_list_of_points(data_frame,
PointSettings(),
data_frame_measurement_name='test')
self.assertEqual(1, len(points))
self.assertEqual('test avalue=30.0,bvalue=30.0 1590314400000000000', points[0])


class DataSerializerChunksTest(unittest.TestCase):
def test_chunks(self):
Expand Down