U vii7@spdZddlZddlZddlZddlmZddlmZmZm Z m Z e dZ ddZ Gdd d Ze fd d ZdS) z Functions for serialize Pandas DataFrame. Much of the code here is inspired by that in the aioinflux packet found here: https://github.com/gusutabopb/aioinflux N)WritePrecision) _ESCAPE_KEY_ESCAPE_STRING_ESCAPE_MEASUREMENTDEFAULT_WRITE_PRECISIONz1influxdb_client.client.write.dataframe_serializercs,fddttjD}tjf|S)Ncsg|]}jdd|fqSN)iloc).0k data_frameb/opt/alt/python38/lib/python3.8/site-packages/influxdb_client/client/write/dataframe_serializer.py sz_itertuples..)rangelencolumnszipindex)r colsr r r _itertuplessrc@s>eZdZdZedfeddddZd edddZd d ZdS) DataframeSerializerz'Serialize DataFrame into LineProtocols.N) chunk_sizereturnc# sddlmm}t|js.tdt||d}|dkrHtd|dd}|dd} |j d d }|dkrz|j n||} t| j r| } nj | |d } | rt| jr| | } n | j| } t| d r| jdkr| d } |dkr| |_ n| ||<|d} t| pg} g} g} g}g}|jrh|jD](\}}||jkr>|||<| |q>tt|jddd}|}d}|D]\}\}}t|}dt| d}| | t!|d}d|d}|| kr8|j"|rd|d|d|d|d }nd|d|d}| |qn|dk rT||krT|}qt|dkrfdnd}t#|j|j$st#|j|j%st#|j|j&rt#|j|j$rd nd}|j"|rd!|d"||d#|d$|d% }n||d#|d$|}n>|j"|r&d&|d'||d(|d)}n||d(|d*}||||qt| t'}d(| } d(|}d+|}|t)j*krd,|}n*|t)j+krd-|}n|t)j,krd.|}t-d/| d0|d0|d1|t!t.| d2}t/|jD]&\} }!| | kr|0| di|j1}qfd3d4}"||_2||_3||_4|j"|dd|_5|"|_6|dk rt7t89t|t:||_;||_`_ - ``DataFrame`` :key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame`` )pdnpz%Must be DataFrame, but type was: {0}.data_frame_measurement_nameNz4"data_frame_measurement_name" is a Required ArgumentZdata_frame_timestamp_columnZdata_frame_timestamp_timezoneF)deep)unittzinfoUTCdata_frame_tag_columnscSs |ddS)Nrr )colr r rz.DataframeSerializer.__init__..)keyrz{keys[z]}r#zp[]z${ '' if z == '' or pd.isna(z&) else f',z={str(z7).translate(_ESCAPE_STRING)}' },z).translate(_ESCAPE_KEY)}iz{"" if pd.isna(z ) else f"z={}z"}z,{ '' if pd.isna(z%) else f'z="{str(z8).translate(_ESCAPE_STRING)}"' }z).translate(_ESCAPE_STRING)}"z {p[%s].value}z{int(p[%s].value / 1e3)}z{int(p[%s].value / 1e6)}z{int(p[%s].value / 1e9)}z lambda p: f"""{measurement_name} z""")measurement_namerrkeysrcsttfdd|S)Ncs| Sr)Zisnax)prr rr%r&zDDataframeSerializer.__init__.._any_not_nan..)anymap)r2Zindexesr)r2r _any_not_nansz2DataframeSerializer.__init__.._any_not_nan)=extrasrr isinstanceZ DataFrame TypeErrorformattypegetcopyrZ PeriodIndexZ to_timestamp to_datetimeZ DatetimeIndexZ tz_localizedthasattrr setZ defaultTagsitemsraddsorted enumerateZdtypesZisnullr3strrappend translaterr issubclassintegerZfloatingZbool_rjoinrZUSZMSSevalrdictreplacenanr f field_indexesfirst_field_maybe_nullr6intmathceilfloatnumber_of_chunksr)#selfr point_settings precisionrkwargsrrZtimestamp_columnZtimestamp_timezoneZdata_frame_timestampr"r/tagsfieldsrRr'valuerZ null_columnsZtimestamp_indexrZ key_formatZ field_indexZ val_format key_valuesepsuffixZ field_valuer. timestamprQr vr6r r5r__init__s9                   0 $             zDataframeSerializer.__init__) chunk_idxcs|dkrj}n2td|djj|j|dj}jrtfddtfddt|D}t|Stt j t|SdS)z Serialize chunk into LineProtocols. :param chunk_idx: The index of chunk to serialize. If `None` then serialize whole dataframe. NzSerialize chunk %s/%s ...r#c3s"|]}tdd|VqdS)z^(( |[^ ])* ),([a-zA-Z0-9])(.*)z\1\3\4N)resubrQ)r r2rYr r sz0DataframeSerializer.serialize..cs|jSr)r6rRr0rir rr%r&z/DataframeSerializer.serialize..) r loggerdebugrXrrSfilterrlistr4rQ)rYrfchunkZlpr rir serializes zDataframeSerializer.serializecCs|jS)zy Return the number of chunks. :return: number of chunks or None if chunk_size is not specified. )rXrir r rrX sz$DataframeSerializer.number_of_chunks)N) __name__ __module__ __qualname____doc__rrTrerprXr r r rrs ^rcKst|||f|S)a Serialize DataFrame into LineProtocols. :param data_frame: Pandas DataFrame to serialize :param point_settings: Default Tags :param precision: The precision for the unix timestamps within the body line-protocol. :key data_frame_measurement_name: name of measurement for writing Pandas DataFrame :key data_frame_tag_columns: list of DataFrame columns which are tags, rest columns will be fields :key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00` or other formats and types supported by `pandas.to_datetime `_ - ``DataFrame`` :key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame`` )rrp)r rZr[r\r r rdata_frame_to_list_of_pointssru)rtloggingrUrgZinfluxdb_clientrZ"influxdb_client.client.write.pointrrrr getLoggerrkrrrur r r rs