Package: pinform
Pinform: An InfluxDB ORM (OTSM) for PythonPInfORM (Python InfluxDB ORM) is an Object/TimeSeries Mapping layer for connecting toInfluxDBin python.Tested with:Python 3.6+InfluxDB 1.7.3 - 1.7.9Use the following command to install using pip:pip install pinformUsage exampleCreate Measurement ModelsFirst, create your measurement model infrompinformimportMeasurementfrompinform.fieldsimportFloatFieldfrompinform.tagsimportTagclassOHLC(Measurement):classMeta:measurement_name='ohlc'symbol=Tag(null=False)open=FloatField(null=False)high=FloatField(null=False)low=FloatField(null=False)close=FloatField(null=False)Create Measurement InstanceFirst, create your measurement model intoday_ohlc=OHLC(time_point=datetime.datetime.now(),symbol='AAPL',open=80.2,high=86.0,low=78.9,close=81.25)Create InfluxClientThen you must create an instance ofInfluxClientto connect to database:frompinform.clientimportInfluxClientcli=InfluxClient(host="localhost",port=8086,database_name="defaultdb")If the database needs authentication, use:cli=InfluxClient(host="localhost",port=8086,database_name="defaultdb",username='your db username',password='your db password')Save and Retrieve PointsTo save data in database, usesave_pointsorsave_dataframefunctions of InfluxClient:ohlc=OHLC(time_point=datetime.datetime.now(),symbol='AAPL',open=100.6,high=102.5,low=90.4,close=94.2)cli.save_points([ohlc])To retrieve data from database, useload_pointsorload_points_as_dataframefunctions of InfluxClient:ohlc_points=cli.load_points(OHLC,{'symbol':'AAPL'})Get Distinct Tag ValuesTo get distinct tag values from all measurements, useget_distinct_existing_tag_valuesfunction from InfluxClient:tag_values=cli.get_distinct_existing_tag_values('symbol')To get distinct tag values from an specific measurements,pass measurement to the previous function:tag_values=cli.get_distinct_existing_tag_values('symbol',measurement=OHLC)FieldsIt's possible to useIntegerField,FloatField,BooleanFieldandStringFieldto save field values in InfluxDB.
There are four other types of fields which help with storing fields with specific integer or string values. To create a field with multiple choice integer values, useMultipleChoiceIntegerFieldorEnumIntegerFieldclasses. To create a field with multiple choice string values, useMultipleChoiceStringFieldorEnumStringFieldclasses.Example for MultipleChoiceStringField:frompinform.fieldsimportMultipleChoiceStringFieldclassWeatherInfo(Measurement):classMeta:measurement_name='weather_info'condition=MultipleChoiceStringField(options=['sunny','cloudy','rainy'],null=False)Example for EnumStringField:fromenumimportEnumfrompinform.fieldsimportEnumStringFieldclassWeatherCondition(Enum):SUNNY='sunny'CLOUDY='cloudy'RAINY='rainy'classWeatherInfo(Measurement):classMeta:measurement_name='weather_info'condition=EnumStringField(enum=WeatherCondition,null=False)Advanced usageDynamic measurement namesIt is possible to use 'MeasurementNameComponent's in measurement name wrapped in parenthesis, their value is replaced in the measurement name at runtime.frompinformimportMeasurementNameComponentclassOHLC(Measurement):classMeta:measurement_name='ohlc_(symbol)'symbol=MeasurementNameComponent(name='symbol')...Query Field and Pandas SeriesUseget_fields_as_seriesfunction from InfluxClient to get fields of specific measurement class as Pandas Series. It's also possible to aggregate data and group by time. This function returnes adictwith aggregated field names as keys and pandas series as values.frompinform.clientimportAggregationModeseries_dict=cli.get_fields_as_series(OHLC,field_aggregations={'close':[AggregationMode.MEAN,AggregationMode.STDDEV]},tags={'symbol':'AAPL'},time_range=(start_datetime,end_datetime),group_by_time_interval='10d')mean_close_series=series_dict['mean_close']stddev_close_series=series_dict['stddev_close']