def serialize(df, measurement, tag_columns=None, **extra_tags) -> bytes:
<EOL>if measurement is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(df.index, pd.DatetimeIndex):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>tag_columns = set(tag_columns or [])<EOL>isnull = df.isnull().any(axis=<NUM_LIT:1>)<EOL>tags = []<EOL>fields = []<EOL>for k, v in extra_tags.items():<EOL><INDENT>tags.append(f"<STR_LIT>")<EOL><DEDENT>for i, (k, v) in enumerate(df.dtypes.items()):<EOL><INDENT>k = k.translate(key_escape)<EOL>if k in tag_columns:<EOL><INDENT>tags.append(f"<STR_LIT>")<EOL><DEDENT>elif issubclass(v.type, np.integer):<EOL><INDENT>fields.append(f"<STR_LIT>")<EOL><DEDENT>elif issubclass(v.type, (np.float, np.bool_)):<EOL><INDENT>fields.append(f"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>fields.append(f"<STR_LIT>")<EOL><DEDENT><DEDENT>fmt = (f'<STR_LIT>', f'<STR_LIT>', '<STR_LIT:U+002C>'.join(tags),<EOL>'<STR_LIT:U+0020>', '<STR_LIT:U+002C>'.join(fields), '<STR_LIT>')<EOL>f = eval("<STR_LIT>".format('<STR_LIT>'.join(fmt)))<EOL>if isnull.any():<EOL><INDENT>lp = map(f, _itertuples(df[~isnull]))<EOL>rep = _replace(df)<EOL>lp_nan = (reduce(lambda a, b: re.sub(*b, a), rep, f(p))<EOL>for p in _itertuples(df[isnull]))<EOL>return '<STR_LIT:\n>'.join(chain(lp, lp_nan)).encode('<STR_LIT:utf-8>')<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT:\n>'.join(map(f, _itertuples(df))).encode('<STR_LIT:utf-8>')<EOL><DEDENT>
Converts a Pandas DataFrame into line protocol format